91 lines
4.1KB

  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 TO_NAME = 'to-name';
  14. const FROM_EMAIL = 'from-email';
  15. const FROM_NAME = 'from-name';
  16. const REPLY_TO = 'reply-to';
  17. const CONTENT_TEMPLATE = 'content-template';
  18. const CONTENT_DATA = 'content-data';
  19. const ATTACHMENT_DATA = 'attachment-data';
  20. const ATTACHMENT_FILENAME = 'attachment-filename';
  21. const ATTACHMENT_CONTENT_TYPE = 'attachment-content-type';
  22. protected $transport;
  23. protected $templating;
  24. protected $parameterBag;
  25. protected $merchantUtils;
  26. public function __construct(MailjetTransport $mailjetTransport, Environment $templating, ParameterBagInterface $parameterBag, MerchantUtilsInterface $merchantUtils)
  27. {
  28. $this->transport = $mailjetTransport;
  29. $this->templating = $templating;
  30. $this->parameterBag = $parameterBag;
  31. $this->merchantUtils = $merchantUtils;
  32. }
  33. public function send($params = [])
  34. {
  35. $merchantCurrent = $this->merchantUtils->getMerchantCurrent();
  36. $merchantConfigEmailFrom = $merchantCurrent->getMerchantConfig('email-from');
  37. $emailFrom = isset($params[self::FROM_EMAIL]) ? $params[self::FROM_EMAIL] : $merchantConfigEmailFrom;
  38. $merchantConfigEmailFromName = $merchantCurrent->getMerchantConfig('email-from-name');
  39. $emailFromName = isset($params[self::FROM_NAME]) ? $params[self::FROM_NAME] : $merchantConfigEmailFromName;
  40. $merchantConfigEmailSubjectPrefix = $merchantCurrent->getMerchantConfig('email-subject-prefix');
  41. $emailSubjectPrefix = isset($params[self::SUBJECT_PREFIX]) ? $params[self::SUBJECT_PREFIX] : $merchantConfigEmailSubjectPrefix;
  42. if ($emailSubjectPrefix && strlen($emailSubjectPrefix)) {
  43. $emailSubjectPrefix .= ' ';
  44. }
  45. $message = new \Swift_Message($emailSubjectPrefix . $params[self::SUBJECT]);
  46. if ($this->parameterBag->get('mailjet.dev.redirect.active')==1) {
  47. $message->addTo($this->parameterBag->get('mailjet.dev.redirect.email'),
  48. isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null);
  49. } else {
  50. $message->addTo(
  51. $params[self::TO_EMAIL],
  52. isset($params[self::TO_NAME]) ? $params[self::TO_NAME] : null);
  53. }
  54. $contentData = [] ;
  55. if(isset($params[self::CONTENT_DATA])) {
  56. $contentData = $params[self::CONTENT_DATA] ;
  57. }
  58. $message->addFrom($emailFrom, $emailFromName)
  59. ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE] . '-html.html.twig', $contentData), 'text/html')
  60. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE] . '-text.html.twig', $contentData));
  61. if (isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  62. $message->addReplyTo($params[self::REPLY_TO]);
  63. }
  64. if (isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
  65. $message->attach(\Swift_Attachment::newInstance(
  66. $params[self::ATTACHMENT_DATA],
  67. $params[self::ATTACHMENT_FILENAME],
  68. $params[self::ATTACHMENT_CONTENT_TYPE]
  69. ));
  70. }
  71. $this->transport->send($message);
  72. }
  73. }