View source
<?php
namespace Drupal\sendinblue\Plugin\Mail;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\sendinblue\SendinblueManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SendInBlueEmail implements MailInterface, ContainerFactoryPluginInterface {
private $sendinblueManager;
protected $loggerFactory;
public function __construct(SendinblueManager $sendinblueManager, LoggerChannelFactoryInterface $logger_factory) {
$this->sendinblueManager = $sendinblueManager;
$this->loggerFactory = $logger_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('sendinblue.manager'), $container
->get('logger.factory'));
}
public function format(array $message) {
$message['body'] = implode("\n\n", $message['body']);
$message['body'] = MailFormatHelper::htmlToText($message['body']);
$message['body'] = MailFormatHelper::wrapMail($message['body']);
return $message;
}
public function mail(array $message) {
try {
$to = [
'email' => $message['to'],
];
$from = [
'email' => $message['from'],
];
$message['reply-to'] = !empty($message['reply-to']) ? $message['reply-to'] : $message['from'];
$replyTo = [
'email' => $message['reply-to'],
];
unset($message['headers']['Content-Type']);
$result = $this->sendinblueManager
->getSendinblueMailin()
->sendEmail($to, $message['subject'], nl2br($message['body']), $message['body'], $from, $replyTo, [], [], [], $message['headers']);
if (empty($result
->getMessageId())) {
$this->loggerFactory
->get('mail')
->error('[SENDINBLUE] - Error sending email (from %from to %to with reply-to %reply).', [
'%from' => $message['from'],
'%to' => $message['to'],
'%reply' => $message['reply-to'] ? $message['reply-to'] : 'not set',
]);
return FALSE;
}
$this->loggerFactory
->get('mail')
->info('[SENDINBLUE] - Sending email %messageId (from %from to %to).', [
'%from' => $message['from'],
'%to' => $message['to'],
'%messageId' => $result
->getMessageId(),
]);
return TRUE;
} catch (\Exception $e) {
$this->loggerFactory
->get('mail')
->error('[SENDINBLUE] - Error sending email (from %from to %to with reply-to %reply) [%error].', [
'%from' => $message['from'],
'%to' => $message['to'],
'%reply' => $message['reply-to'] ? $message['reply-to'] : 'not set',
'%error' => $e
->getMessage(),
]);
return FALSE;
}
}
}