PostmarkMail.php in Postmark 8
File
src/Plugin/Mail/PostmarkMail.php
View source
<?php
namespace Drupal\postmark\Plugin\Mail;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\postmark\PostmarkHandler;
use Html2Text\Html2Text;
use Drupal\Component\Utility\Html;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PostmarkMail implements MailInterface, ContainerFactoryPluginInterface {
protected $config;
protected $logger;
protected $postmarkHandler;
public function __construct(ImmutableConfig $settings, LoggerInterface $logger, PostmarkHandler $postmark_handler) {
$this->config = $settings;
$this->logger = $logger;
$this->postmarkHandler = $postmark_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('config.factory')
->get('postmark.settings'), $container
->get('logger.factory')
->get('postmark'), $container
->get('postmark.mail_handler'));
}
public function format(array $message) {
if (is_array($message['body'])) {
$message['body'] = implode("\n\n", $message['body']);
}
$format = $this->config
->get('format_filter');
if (!empty($format)) {
$message['body'] = check_markup($message['body'], $format, $message['langcode']);
}
return $message;
}
public function mail(array $message) {
$postmark_message = [
'from' => $message['from'],
'to' => $message['to'],
'subject' => $message['subject'],
'html' => $message['body'],
];
if (isset($message['plain'])) {
$postmark_message['text'] = $message['plain'];
}
else {
$converter = new Html2Text($message['body']);
$postmark_message['text'] = $converter
->getText();
}
if (!empty($message['headers']['Cc'])) {
$postmark_message['cc'] = $message['headers']['Cc'];
}
if (!empty($message['headers']['Bcc'])) {
$postmark_message['bcc'] = $message['headers']['Bcc'];
}
if (!empty($message['reply-to'])) {
$postmark_message['reply-to'] = $message['reply-to'];
}
if (!empty($message['params']['attachments'])) {
$attachments = [];
foreach ($message['params']['attachments'] as $attachment) {
if (file_exists($attachment)) {
$attachments[] = [
'filePath' => $attachment,
];
}
}
if (count($attachments) > 0) {
$postmark_message['attachment'] = $attachments;
}
}
return $this->postmarkHandler
->sendMail($postmark_message);
}
}
Classes
Name |
Description |
PostmarkMail |
Modify the Drupal mail system to use Postmark when sending emails. |