Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

119 lines
5.8KB

  1. <?php
  2. namespace Lc\CaracoleBundle\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. $this->transport = $mailjetTransport;
  39. $this->templating = $templating;
  40. $this->parameterBag = $parameterBag;
  41. $this->merchantResolver = $merchantResolver;
  42. $this->settingSolver = $settingSolver;
  43. }
  44. public function send($params = [])
  45. {
  46. $merchantCurrent = $this->merchantResolver->getCurrent();
  47. $merchantConfigEmailFrom = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_FROM);
  48. $emailFrom = (isset($params[self::FROM_EMAIL]) && $params[self::FROM_EMAIL] && strlen($params[self::FROM_EMAIL])) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom;
  49. $merchantConfigEmailFromName = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_FROM_NAME);
  50. $emailFromName = isset($params[self::FROM_NAME]) ?? $merchantConfigEmailFromName;
  51. $merchantConfigEmailSubjectPrefix = $this->settingSolver->getSettingValue($merchantCurrent, MerchantSettingDefinition::SETTING_EMAIL_SUBJECT_PREFIX);
  52. $emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ?? $merchantConfigEmailSubjectPrefix;
  53. if ($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
  54. $emailSubjectPrefix .= ' ';
  55. }
  56. $message = new \Swift_Message($emailSubjectPrefix . $params[self::SUBJECT]);
  57. if ($this->parameterBag->get('mailjet.dev.redirect.active')==1) {
  58. $message->addTo($this->parameterBag->get('mailjet.dev.redirect.email'),
  59. isset($params[self::TO_NAME]) ?? null);
  60. } else {
  61. $message->addTo(
  62. $params[self::TO_EMAIL],
  63. isset($params[self::TO_NAME]) ?? null);
  64. }
  65. $contentData = [] ;
  66. if(isset($params[self::CONTENT_DATA])) {
  67. $contentData = $params[self::CONTENT_DATA] ;
  68. }
  69. $message->addFrom($emailFrom, $emailFromName)
  70. ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE] . '-html.html.twig', $contentData), 'text/html')
  71. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE] . '-text.html.twig', $contentData));
  72. if(isset($params[self::COPY_TO]) && strlen($params[self::COPY_TO])) {
  73. $message->addCc($params[self::COPY_TO]);
  74. }
  75. if(isset($params[self::COPY_HIDDEN_TO]) && strlen($params[self::COPY_HIDDEN_TO])) {
  76. $message->addBcc($params[self::COPY_HIDDEN_TO]);
  77. }
  78. if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  79. $message->addReplyTo($params[self::REPLY_TO]);
  80. }
  81. if(isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
  82. $message->attach(\Swift_Attachment::newInstance(
  83. $params[self::ATTACHMENT_DATA],
  84. $params[self::ATTACHMENT_FILENAME],
  85. $params[self::ATTACHMENT_CONTENT_TYPE]
  86. ));
  87. }
  88. /*if(isset($params[self::DISPOSITION_NOTIFICATION_TO]) && $params[self::DISPOSITION_NOTIFICATION_TO]) {
  89. $emailFromDispositionNotificationTo = $emailFrom ;
  90. if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  91. $emailFromDispositionNotificationTo = $params[self::REPLY_TO] ;
  92. }
  93. $message->getHeaders()->addTextHeader('Disposition-Notification-To', $emailFromDispositionNotificationTo) ;
  94. $message->getHeaders()->addMailboxHeader('Disposition-Notification-To', $emailFromDispositionNotificationTo);
  95. }*/
  96. return $this->transport->send($message);
  97. }
  98. }