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ů.

115 lines
5.6KB

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