Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MailUtils.php 4.6KB

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