Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

86 rindas
4.0KB

  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. $message->addFrom($emailFrom, $emailFromName)
  55. ->setBody($this->templating->render($params[self::CONTENT_TEMPLATE] . '-html.html.twig', $params[self::CONTENT_DATA]), 'text/html')
  56. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE] . '-text.html.twig', $params[self::CONTENT_DATA]));
  57. if (isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  58. $message->addReplyTo($params[self::REPLY_TO]);
  59. }
  60. if (isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
  61. $message->attach(\Swift_Attachment::newInstance(
  62. $params[self::ATTACHMENT_DATA],
  63. $params[self::ATTACHMENT_FILENAME],
  64. $params[self::ATTACHMENT_CONTENT_TYPE]
  65. ));
  66. }
  67. $this->transport->send($message);
  68. }
  69. }