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.

111 satır
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(MailjetTransport $mailjetTransport, Environment $templating, ParameterBagInterface $parameterBag, MerchantUtilsInterface $merchantUtils)
  30. {
  31. $this->transport = $mailjetTransport;
  32. $this->templating = $templating;
  33. $this->parameterBag = $parameterBag;
  34. $this->merchantUtils = $merchantUtils;
  35. }
  36. public function send($params = [])
  37. {
  38. $merchantCurrent = $this->merchantUtils->getMerchantCurrent();
  39. $merchantConfigEmailFrom = $merchantCurrent->getMerchantConfig('email-from');
  40. $emailFrom = (isset($params[self::FROM_EMAIL]) && $params[self::FROM_EMAIL] && strlen($params[self::FROM_EMAIL])) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom;
  41. $merchantConfigEmailFromName = $merchantCurrent->getMerchantConfig('email-from-name');
  42. $emailFromName = isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $merchantConfigEmailFromName;
  43. $merchantConfigEmailSubjectPrefix = $merchantCurrent->getMerchantConfig('email-subject-prefix');
  44. $emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ? $params[self::SUBJECT_PREFIX] : $merchantConfigEmailSubjectPrefix;
  45. if ($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
  46. $emailSubjectPrefix .= ' ';
  47. }
  48. $message = new \Swift_Message($emailSubjectPrefix . $params[self::SUBJECT]);
  49. if ($this->parameterBag->get('mailjet.dev.redirect.active')==1) {
  50. $message->addTo($this->parameterBag->get('mailjet.dev.redirect.email'),
  51. isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null);
  52. } else {
  53. $message->addTo(
  54. $params[self::TO_EMAIL],
  55. isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null);
  56. }
  57. $contentData = [] ;
  58. if(isset($params[self::CONTENT_DATA])) {
  59. $contentData = $params[self::CONTENT_DATA] ;
  60. }
  61. $message->addFrom($emailFrom, $emailFromName)
  62. ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE] . '-html.html.twig', $contentData), 'text/html')
  63. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE] . '-text.html.twig', $contentData));
  64. if(isset($params[self::COPY_TO]) && strlen($params[self::COPY_TO])) {
  65. $message->addCc($params[self::COPY_TO]);
  66. }
  67. if(isset($params[self::COPY_HIDDEN_TO]) && strlen($params[self::COPY_HIDDEN_TO])) {
  68. $message->addBcc($params[self::COPY_HIDDEN_TO]);
  69. }
  70. if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  71. $message->addReplyTo($params[self::REPLY_TO]);
  72. }
  73. if(isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
  74. $message->attach(\Swift_Attachment::newInstance(
  75. $params[self::ATTACHMENT_DATA],
  76. $params[self::ATTACHMENT_FILENAME],
  77. $params[self::ATTACHMENT_CONTENT_TYPE]
  78. ));
  79. }
  80. /*if(isset($params[self::DISPOSITION_NOTIFICATION_TO]) && $params[self::DISPOSITION_NOTIFICATION_TO]) {
  81. $emailFromDispositionNotificationTo = $emailFrom ;
  82. if(isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  83. $emailFromDispositionNotificationTo = $params[self::REPLY_TO] ;
  84. }
  85. $message->getHeaders()->addTextHeader('Disposition-Notification-To', $emailFromDispositionNotificationTo) ;
  86. $message->getHeaders()->addMailboxHeader('Disposition-Notification-To', $emailFromDispositionNotificationTo);
  87. }*/
  88. $this->transport->send($message);
  89. }
  90. }