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.

94 satır
3.2KB

  1. <?php
  2. namespace common\components;
  3. use domain\Producer\Producer\Producer;
  4. use domain\Setting\AdminSettingBag;
  5. use yii\base\ErrorException;
  6. use yii\mail\MessageInterface;
  7. class MailerService
  8. {
  9. public function sendFromProducer(string $subject, string $view, array $params, string $toEmail, Producer $producer)
  10. {
  11. $message = $this->composeBase($toEmail, $view, $params);
  12. $this->initMessageFromProducer($message, $subject, $producer);
  13. return $message->send();
  14. }
  15. public function sendFromSite(string $subject, string $view, array $params, string $toEmail)
  16. {
  17. $message = $this->composeBase($toEmail, $view, $params);
  18. $this->initMessageFromSite($message, $subject);
  19. return $message->send();
  20. }
  21. public function sendProducer(Producer $producer, string $subject, string $view, array $params = [], string $replyTo = null)
  22. {
  23. $message = $this->composeBase($producer->contact_email, $view, $params);
  24. if($replyTo) {
  25. $message->setReplyTo($replyTo);
  26. }
  27. $this->initMessageFromSite($message, $subject);
  28. return $message->send();
  29. }
  30. public function sendAdmin(string $subject, string $view, array $params = [], string $replyTo = null)
  31. {
  32. $message = $this->composeBase(\Yii::$app->parameterBag->get('adminEmail'), $view, $params);
  33. if($replyTo) {
  34. $message->setReplyTo($replyTo);
  35. }
  36. $this->initMessageFromSite($message, $subject);
  37. return $message->send();
  38. }
  39. public function getMailer()
  40. {
  41. $mailerSetting = AdminSettingBag::getInstance()->get('mailer');
  42. if(!$mailerSetting) {
  43. throw new ErrorException("Aucun mailer défini pour les emails courants.");
  44. }
  45. if(isset(\Yii::$app->$mailerSetting)) {
  46. return \Yii::$app->$mailerSetting;
  47. }
  48. else {
  49. throw new ErrorException("La configuration pour le mailer $mailerSetting est introuvable dans main-local.php");
  50. }
  51. }
  52. private function composeBase(string $toEmail, string $view, array $params = [])
  53. {
  54. return $this->getMailer()
  55. ->compose([
  56. 'html' => '@common/mail/'.$view.'-html.php',
  57. 'text' => '@common/mail/'.$view.'-text.php'
  58. ], $params)
  59. ->setTo($toEmail);
  60. }
  61. private function initMessageFromSite(MessageInterface $message, string $subject)
  62. {
  63. $message->setFrom([\Yii::$app->parameterBag->get('adminEmail') => \Yii::$app->parameterBag->get('siteName')]);
  64. $message->setSubject($this->buildSubject(\Yii::$app->parameterBag->get('siteName'), $subject));
  65. return $message;
  66. }
  67. private function initMessageFromProducer(MessageInterface $message, string $subject, Producer $producer)
  68. {
  69. $message->setFrom([$this->getProducerEmailPlatform($producer) => $producer->name]);
  70. $message->setSubject($this->buildSubject($producer->name, $subject));
  71. return $message;
  72. }
  73. private function getProducerEmailPlatform(Producer $producer): string
  74. {
  75. return $producer->slug . '@souke.fr';
  76. }
  77. private function buildSubject(string $prefix, string $subject)
  78. {
  79. return '['.$prefix.'] '.$subject;
  80. }
  81. }