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.

125 lines
4.8KB

  1. <?php
  2. namespace Lc\SovBundle\Notification;
  3. use Lc\SovBundle\Definition\SiteSettingDefinition;
  4. use Lc\SovBundle\Repository\Site\SiteStore;
  5. use Lc\SovBundle\Solver\Setting\SettingSolver;
  6. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Twig\Environment;
  9. class MailMailjetNotification
  10. {
  11. const SUBJECT = 'subject';
  12. const SUBJECT_PREFIX = 'subject-prefix';
  13. const TO_EMAIL = 'to-email';
  14. const COPY_TO = 'copy-to';
  15. const COPY_HIDDEN_TO = 'copy-hidden-to';
  16. const TO_NAME = 'to-name';
  17. const FROM_EMAIL = 'from-email';
  18. const FROM_NAME = 'from-name';
  19. const REPLY_TO = 'reply-to';
  20. const CONTENT_TEMPLATE = 'content-template';
  21. const CONTENT_DATA = 'content-data';
  22. const ATTACHMENT_DATA = 'attachment-data';
  23. const ATTACHMENT_FILENAME = 'attachment-filename';
  24. const ATTACHMENT_CONTENT_TYPE = 'attachment-content-type';
  25. protected MailjetTransport $transport;
  26. protected Environment $templating;
  27. protected ParameterBagInterface $parameterBag;
  28. protected SettingSolver $settingSolver;
  29. protected SiteStore $siteStore;
  30. public function __construct(
  31. MailjetTransport $mailjetTransport,
  32. Environment $templating,
  33. ParameterBagInterface $parameterBag,
  34. SettingSolver $settingSolver,
  35. SiteStore $siteStore
  36. ) {
  37. $this->transport = $mailjetTransport;
  38. $this->templating = $templating;
  39. $this->parameterBag = $parameterBag;
  40. $this->settingSolver = $settingSolver;
  41. $this->siteStore = $siteStore;
  42. }
  43. public function send($params = [])
  44. {
  45. $siteDefault = $this->siteStore->getOneDefault();
  46. $emailSubjectPrefix = (isset($params[self::SUBJECT_PREFIX]) && $params[self::SUBJECT_PREFIX] && strlen($params[self::SUBJECT_PREFIX]) > 0)
  47. ? $params[self::SUBJECT_PREFIX]
  48. : $this->settingSolver->getSettingValue(
  49. $siteDefault,
  50. SiteSettingDefinition::SETTING_EMAIL_SUBJECT_PREFIX
  51. );
  52. $emailFrom = (isset($params[self::FROM_EMAIL]) && $params[self::FROM_EMAIL] && strlen($params[self::FROM_EMAIL]) > 0)
  53. ? $params[self::FROM_EMAIL]
  54. : $this->settingSolver->getSettingValue(
  55. $siteDefault,
  56. SiteSettingDefinition::SETTING_EMAIL_FROM
  57. );
  58. $emailFromName = (isset($params[self::FROM_NAME]) && $params[self::FROM_NAME] && strlen($params[self::FROM_NAME]) > 0)
  59. ? $params[self::FROM_NAME]
  60. : $this->settingSolver->getSettingValue(
  61. $siteDefault,
  62. SiteSettingDefinition::SETTING_EMAIL_FROM_NAME
  63. );
  64. $message = new \Swift_Message($emailSubjectPrefix . $params[self::SUBJECT]);
  65. if ($this->parameterBag->get('mailjet.dev.redirect.active') == 1) {
  66. $message->addTo(
  67. $this->parameterBag->get('mailjet.dev.redirect.email'),
  68. isset($params[self::TO_NAME]) ?? null
  69. );
  70. } else {
  71. $message->addTo(
  72. $params[self::TO_EMAIL],
  73. isset($params[self::TO_NAME]) ?? null
  74. );
  75. }
  76. $contentData = [];
  77. if (isset($params[self::CONTENT_DATA])) {
  78. $contentData = $params[self::CONTENT_DATA];
  79. }
  80. $message->addFrom($emailFrom, $emailFromName)
  81. ->setBody(
  82. $this->templating->render($params[self::CONTENT_TEMPLATE] . '-html.html.twig', $contentData),
  83. 'text/html'
  84. )
  85. ->addPart($this->templating->render($params[self::CONTENT_TEMPLATE] . '-text.html.twig', $contentData));
  86. if (isset($params[self::COPY_TO]) && strlen($params[self::COPY_TO])) {
  87. $message->addCc($params[self::COPY_TO]);
  88. }
  89. if (isset($params[self::COPY_HIDDEN_TO]) && strlen($params[self::COPY_HIDDEN_TO])) {
  90. $message->addBcc($params[self::COPY_HIDDEN_TO]);
  91. }
  92. if (isset($params[self::REPLY_TO]) && strlen($params[self::REPLY_TO])) {
  93. $message->addReplyTo($params[self::REPLY_TO]);
  94. }
  95. if (isset($params[self::ATTACHMENT_DATA]) && isset($params[self::ATTACHMENT_FILENAME]) && isset($params[self::ATTACHMENT_CONTENT_TYPE])) {
  96. $message->attach(
  97. \Swift_Attachment::newInstance(
  98. $params[self::ATTACHMENT_DATA],
  99. $params[self::ATTACHMENT_FILENAME],
  100. $params[self::ATTACHMENT_CONTENT_TYPE]
  101. )
  102. );
  103. }
  104. return $this->transport->send($message);
  105. }
  106. }