View source
<?php
namespace Drupal\mailgun\Plugin\Mail;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Render\RendererInterface;
use Drupal\mailgun\MailgunHandlerInterface;
use Html2Text\Html2Text;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MailgunMail implements MailInterface, ContainerFactoryPluginInterface {
protected $mailgunConfig;
protected $logger;
protected $renderer;
protected $queueFactory;
protected $mailgunHandler;
public function __construct(ImmutableConfig $settings, LoggerInterface $logger, RendererInterface $renderer, QueueFactory $queueFactory, MailgunHandlerInterface $mailgunHandler) {
$this->mailgunConfig = $settings;
$this->logger = $logger;
$this->renderer = $renderer;
$this->queueFactory = $queueFactory;
$this->mailgunHandler = $mailgunHandler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('config.factory')
->get(MailgunHandlerInterface::CONFIG_NAME), $container
->get('logger.factory')
->get('mailgun'), $container
->get('renderer'), $container
->get('queue'), $container
->get('mailgun.mail_handler'));
}
public function format(array $message) {
if (is_array($message['body'])) {
$message['body'] = implode("\n\n", $message['body']);
}
$format = $this->mailgunConfig
->get('format_filter');
if (!empty($format)) {
$message['body'] = check_markup($message['body'], $format, $message['langcode']);
}
if (isset($message['params']['html']) && !$message['params']['html']) {
return $message;
}
if ($this->mailgunConfig
->get('use_theme')) {
$render = [
'#theme' => isset($message['params']['theme']) ? $message['params']['theme'] : 'mailgun',
'#message' => $message,
];
$message['body'] = $this->renderer
->renderPlain($render);
}
return $message;
}
public function mail(array $message) {
$mailgun_message = $this
->buildMessage($message);
if ($this->mailgunConfig
->get('use_queue')) {
return $this
->queueMessage($mailgun_message);
}
return $this->mailgunHandler
->sendMail($mailgun_message);
}
public function queueMessage(array $message) {
$queue = $this->queueFactory
->get('mailgun_send_mail');
$item = new \stdClass();
$item->message = $message;
$result = $queue
->createItem($item);
if ($result !== FALSE) {
if ($this->mailgunConfig
->get('debug_mode')) {
$this->logger
->notice('Successfully queued message from %from to %to.', [
'%from' => $message['from'],
'%to' => $message['to'],
]);
}
}
else {
$this->logger
->error('Unable to queue message from %from to %to.', [
'%from' => $message['from'],
'%to' => $message['to'],
]);
}
return !empty($result);
}
protected function buildMessage(array $message) {
$mailgun_message = [
'from' => $message['headers']['From'],
'to' => $message['to'],
'subject' => $message['subject'],
'html' => $message['body'],
];
if (isset($message['params']['html']) && !$message['params']['html']) {
unset($mailgun_message['html']);
}
if (isset($message['plain'])) {
$mailgun_message['text'] = $message['plain'];
}
else {
$converter = new Html2Text($message['body'], [
'width' => 0,
]);
$mailgun_message['text'] = $converter
->getText();
}
if (!empty($message['headers']['Cc'])) {
$mailgun_message['cc'] = $message['headers']['Cc'];
}
if (!empty($message['headers']['Bcc'])) {
$mailgun_message['bcc'] = $message['headers']['Bcc'];
}
if (!empty($message['reply-to'])) {
$mailgun_message['h:Reply-To'] = $message['reply-to'];
}
$allowed_params = [
'o:tag',
'o:campaign',
'o:deliverytime',
'o:dkim',
'o:testmode',
'o:tracking',
'o:tracking-clicks',
'o:tracking-opens',
];
foreach ($message['params'] as $key => $value) {
$allowed = in_array($key, $allowed_params) ? TRUE : FALSE;
if ($allowed) {
$mailgun_message[$key] = $value;
}
if (substr($key, 0, 2) == 'h:' || substr($key, 0, 2) == 'v:') {
$mailgun_message[$key] = $value;
}
}
if ($this->mailgunConfig
->get('test_mode')) {
$mailgun_message['o:testmode'] = 'yes';
}
if ($this->mailgunConfig
->get('tagging_mailkey')) {
$mailgun_message['o:tag'][] = $message['id'];
}
if (!empty($message['params']['attachments'])) {
$attachments = [];
foreach ($message['params']['attachments'] as $attachment) {
if (!empty($attachment['filepath']) && file_exists($attachment['filepath'])) {
$attachments[] = [
'filePath' => $attachment['filepath'],
];
}
elseif (!empty($attachment['filecontent']) && !empty($attachment['filename'])) {
$attachments[] = [
'fileContent' => $attachment['filecontent'],
'filename' => $attachment['filename'],
];
}
}
if (count($attachments) > 0) {
$mailgun_message['attachment'] = $attachments;
}
}
if ($this
->checkTracking($message)) {
$track_opens = $this->mailgunConfig
->get('tracking_opens');
if (!empty($track_opens)) {
$mailgun_message['o:tracking-opens'] = $track_opens;
}
$track_clicks = $this->mailgunConfig
->get('tracking_clicks');
if (!empty($track_clicks)) {
$mailgun_message['o:tracking-clicks'] = $track_opens;
}
}
else {
$mailgun_message['o:tracking'] = 'no';
}
return $mailgun_message;
}
protected function checkTracking(array $message) {
$tracking = TRUE;
$exceptions = $this->mailgunConfig
->get('tracking_exception');
if (!empty($exceptions)) {
$exceptions = str_replace([
"\r\n",
"\r",
], "\n", $exceptions);
$tracking = !in_array($message['module'] . ':' . $message['key'], explode("\n", $exceptions));
}
return $tracking;
}
}