Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

115 linhas
5.4KB

  1. <?php
  2. namespace Lc\ShopBundle\Services;
  3. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  4. use Lc\ShopBundle\Model\Merchant;
  5. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Twig\Environment;
  8. class MailUtils
  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 $merchantUtils;
  29. public function __construct(
  30. MailjetTransport $mailjetTransport,
  31. Environment $templating,
  32. ParameterBagInterface $parameterBag,
  33. MerchantUtilsInterface $merchantUtils
  34. ) {
  35. $this->transport = $mailjetTransport;
  36. $this->templating = $templating;
  37. $this->parameterBag = $parameterBag;
  38. $this->merchantUtils = $merchantUtils;
  39. }
  40. public function send($params = [])
  41. {
  42. $merchantCurrent = $this->merchantUtils->getMerchantCurrent();
  43. $merchantConfigEmailFrom = $merchantCurrent->getMerchantConfig('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->getMerchantConfig('email-from-name');
  46. $emailFromName = isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $merchantConfigEmailFromName;
  47. $merchantConfigEmailSubjectPrefix = $merchantCurrent->getMerchantConfig('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. $this->transport->send($message);
  93. }
  94. }