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.

134 lines
5.1KB

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