You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
5.2KB

  1. <?php
  2. namespace Lc\SovBundle\Notification;
  3. use Lc\CaracoleBundle\Definition\MerchantSettingDefinition;
  4. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  5. use Lc\SovBundle\Solver\Setting\SettingSolver;
  6. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Twig\Environment;
  9. class MailMailjetNotification
  10. {
  11. const SUBJECT = 'subject';
  12. const SUBJECT_PREFIX = 'subject-prefix';
  13. const TO_EMAIL = 'to-email';
  14. const COPY_TO = 'copy-to';
  15. const COPY_HIDDEN_TO = 'copy-hidden-to';
  16. const TO_NAME = 'to-name';
  17. const FROM_EMAIL = 'from-email';
  18. const FROM_NAME = 'from-name';
  19. const REPLY_TO = 'reply-to';
  20. const CONTENT_TEMPLATE = 'content-template';
  21. const CONTENT_DATA = 'content-data';
  22. const ATTACHMENT_DATA = 'attachment-data';
  23. const ATTACHMENT_FILENAME = 'attachment-filename';
  24. const ATTACHMENT_CONTENT_TYPE = 'attachment-content-type';
  25. //const DISPOSITION_NOTIFICATION_TO = 'disposition-notification-to' ;
  26. protected MailjetTransport $transport;
  27. protected Environment $templating;
  28. protected ParameterBagInterface $parameterBag;
  29. protected MerchantResolver $merchantResolver;
  30. protected SettingSolver $settingSolver;
  31. public function __construct(
  32. MailjetTransport $mailjetTransport,
  33. Environment $templating,
  34. ParameterBagInterface $parameterBag,
  35. MerchantResolver $merchantResolver,
  36. SettingSolver $settingSolver
  37. )
  38. {
  39. $this->transport = $mailjetTransport;
  40. $this->templating = $templating;
  41. $this->parameterBag = $parameterBag;
  42. $this->merchantResolver = $merchantResolver;
  43. $this->settingSolver = $settingSolver;
  44. }
  45. public function send($params = [])
  46. {
  47. $merchantCurrent = $this->merchantResolver->getCurrent();
  48. $merchantConfigEmailFrom = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_FROM);
  49. $emailFrom = (isset($params[self::FROM_EMAIL]) && $params[self::FROM_EMAIL] && strlen($params[self::FROM_EMAIL])) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom;
  50. $merchantConfigEmailFromName = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_FROM_NAME);
  51. $emailFromName = isset($params[self::FROM_NAME]) ?? $merchantConfigEmailFromName;
  52. $merchantConfigEmailSubjectPrefix = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_SUBJECT_PREFIX);
  53. $emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ?? $merchantConfigEmailSubjectPrefix;
  54. if ($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
  55. $emailSubjectPrefix .= ' ';
  56. }
  57. $message = new \Swift_Message($emailSubjectPrefix . $params[self::SUBJECT]);
  58. if ($this->parameterBag->get('mailjet.dev.redirect.active') == 1) {
  59. $message->addTo($this->parameterBag->get('mailjet.dev.redirect.email'),
  60. isset($params[self::TO_NAME]) ?? null);
  61. } else {
  62. $message->addTo(
  63. $params[self::TO_EMAIL],
  64. isset($params[self::TO_NAME]) ?? null);
  65. }
  66. $contentData = [];
  67. if (isset($params[self::CONTENT_DATA])) {
  68. $contentData = $params[self::CONTENT_DATA];
  69. }
  70. $message->addFrom($emailFrom, $emailFromName)
  71. ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE] . '-html.html.twig', $contentData), 'text/html')
  72. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE] . '-text.html.twig', $contentData));
  73. if (isset($params[self::COPY_TO]) && strlen($params[self::COPY_TO])) {
  74. $message->addCc($params[self::COPY_TO]);
  75. }
  76. if (isset($params[self::COPY_HIDDEN_TO]) && strlen($params[self::COPY_HIDDEN_TO])) {
  77. $message->addBcc($params[self::COPY_HIDDEN_TO]);
  78. }
  79. if (isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  80. $message->addReplyTo($params[self::REPLY_TO]);
  81. }
  82. if (isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
  83. $message->attach(\Swift_Attachment::newInstance(
  84. $params[self::ATTACHMENT_DATA],
  85. $params[self::ATTACHMENT_FILENAME],
  86. $params[self::ATTACHMENT_CONTENT_TYPE]
  87. ));
  88. }
  89. /*if(isset($params[self::DISPOSITION_NOTIFICATION_TO]) && $params[self::DISPOSITION_NOTIFICATION_TO]) {
  90. $emailFromDispositionNotificationTo = $emailFrom ;
  91. if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  92. $emailFromDispositionNotificationTo = $params[self::REPLY_TO] ;
  93. }
  94. $message->getHeaders()->addTextHeader('Disposition-Notification-To', $emailFromDispositionNotificationTo) ;
  95. $message->getHeaders()->addMailboxHeader('Disposition-Notification-To', $emailFromDispositionNotificationTo);
  96. }*/
  97. return $this->transport->send($message);
  98. }
  99. }