|
- <?php
-
- namespace Lc\CaracoleBundle\Notification;
-
- use Lc\CaracoleBundle\Definition\MerchantSettingDefinition;
- use Lc\CaracoleBundle\Resolver\MerchantResolver;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Twig\Environment;
-
- class MailMailjetNotification
- {
- const SUBJECT = 'subject';
- const SUBJECT_PREFIX = 'subject-prefix';
- const TO_EMAIL = 'to-email';
- const COPY_TO = 'copy-to';
- const COPY_HIDDEN_TO = 'copy-hidden-to';
- const TO_NAME = 'to-name';
- const FROM_EMAIL = 'from-email';
- const FROM_NAME = 'from-name';
- const REPLY_TO = 'reply-to';
- const CONTENT_TEMPLATE = 'content-template';
- const CONTENT_DATA = 'content-data';
- const ATTACHMENT_DATA = 'attachment-data';
- const ATTACHMENT_FILENAME = 'attachment-filename';
- const ATTACHMENT_CONTENT_TYPE = 'attachment-content-type';
- //const DISPOSITION_NOTIFICATION_TO = 'disposition-notification-to' ;
-
- protected MailjetTransport $transport;
- protected Environment $templating;
- protected ParameterBagInterface $parameterBag;
- protected MerchantResolver $merchantResolver;
- protected SettingSolver $settingSolver;
-
- public function __construct(
- MailjetTransport $mailjetTransport,
- Environment $templating,
- ParameterBagInterface $parameterBag,
- MerchantResolver $merchantResolver,
- SettingSolver $settingSolver
- ) {
- $this->transport = $mailjetTransport;
- $this->templating = $templating;
- $this->parameterBag = $parameterBag;
- $this->merchantResolver = $merchantResolver;
- $this->settingSolver = $settingSolver;
- }
-
- public function send($params = [])
- {
- $merchantCurrent = $this->merchantResolver->getCurrent();
-
- $merchantConfigEmailFrom = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_FROM);
- $emailFrom = (isset($params[self::FROM_EMAIL]) && $params[self::FROM_EMAIL] && strlen($params[self::FROM_EMAIL])) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom;
-
- $merchantConfigEmailFromName = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_FROM_NAME);
- $emailFromName = isset($params[self::FROM_NAME]) ?? $merchantConfigEmailFromName;
-
- $merchantConfigEmailSubjectPrefix = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_SUBJECT_PREFIX);
- $emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ?? $merchantConfigEmailSubjectPrefix;
- if ($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
- $emailSubjectPrefix .= ' ';
- }
-
- $message = new \Swift_Message($emailSubjectPrefix . $params[self::SUBJECT]);
-
-
- if ($this->parameterBag->get('mailjet.dev.redirect.active')==1) {
- $message->addTo($this->parameterBag->get('mailjet.dev.redirect.email'),
- isset($params[self::TO_NAME]) ?? null);
- } else {
- $message->addTo(
- $params[self::TO_EMAIL],
- isset($params[self::TO_NAME]) ?? null);
- }
-
- $contentData = [] ;
- if(isset($params[self::CONTENT_DATA])) {
- $contentData = $params[self::CONTENT_DATA] ;
- }
-
- $message->addFrom($emailFrom, $emailFromName)
- ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE] . '-html.html.twig', $contentData), 'text/html')
- ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE] . '-text.html.twig', $contentData));
-
- if(isset($params[self::COPY_TO]) && strlen($params[self::COPY_TO])) {
- $message->addCc($params[self::COPY_TO]);
- }
-
- if(isset($params[self::COPY_HIDDEN_TO]) && strlen($params[self::COPY_HIDDEN_TO])) {
- $message->addBcc($params[self::COPY_HIDDEN_TO]);
- }
-
- if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
- $message->addReplyTo($params[self::REPLY_TO]);
- }
-
- if(isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
- $message->attach(\Swift_Attachment::newInstance(
- $params[self::ATTACHMENT_DATA],
- $params[self::ATTACHMENT_FILENAME],
- $params[self::ATTACHMENT_CONTENT_TYPE]
- ));
- }
-
- /*if(isset($params[self::DISPOSITION_NOTIFICATION_TO]) && $params[self::DISPOSITION_NOTIFICATION_TO]) {
- $emailFromDispositionNotificationTo = $emailFrom ;
- if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
- $emailFromDispositionNotificationTo = $params[self::REPLY_TO] ;
- }
- $message->getHeaders()->addTextHeader('Disposition-Notification-To', $emailFromDispositionNotificationTo) ;
- $message->getHeaders()->addMailboxHeader('Disposition-Notification-To', $emailFromDispositionNotificationTo);
- }*/
-
- return $this->transport->send($message);
- }
- }
|