Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

67 lines
2.3KB

  1. <?php
  2. namespace common\components;
  3. use common\logic\Producer\Producer\Model\Producer;
  4. use yii\mail\MessageInterface;
  5. class MailerService
  6. {
  7. public function sendFromProducer(string $subject, string $view, array $params, string $toEmail, Producer $producer)
  8. {
  9. $message = $this->composeBase($toEmail, $view, $params);
  10. $this->initMessageFromProducer($message, $subject, $producer);
  11. $message->send();
  12. }
  13. public function sendFromSite(string $subject, string $view, array $params, string $toEmail)
  14. {
  15. $message = $this->composeBase($toEmail, $view, $params);
  16. $this->initMessageFromSite($message, $subject);
  17. $message->send();
  18. }
  19. public function sendAdmin(string $subject, string $view, array $params = [], string $replyTo = null)
  20. {
  21. $message = $this->composeBase(\Yii::$app->parameterBag->get('adminEmail'), $view, $params);
  22. if($replyTo) {
  23. $message->setReplyTo($replyTo);
  24. }
  25. $this->initMessageFromSite($message, $subject);
  26. $message->send();
  27. }
  28. private function composeBase(string $toEmail, string $view, array $params = [])
  29. {
  30. return \Yii::$app->mailer
  31. ->compose([
  32. 'html' => '@common/mail/'.$view.'-html.php',
  33. 'text' => '@common/mail/'.$view.'-text.php'
  34. ], $params)
  35. ->setTo($toEmail);
  36. }
  37. private function initMessageFromSite(MessageInterface $message, string $subject)
  38. {
  39. $message->setFrom([\Yii::$app->parameterBag->get('adminEmail') => \Yii::$app->parameterBag->get('siteName')]);
  40. $message->setSubject($this->buildSubject(\Yii::$app->parameterBag->get('siteName'), $subject));
  41. return $message;
  42. }
  43. private function initMessageFromProducer(MessageInterface $message, string $subject, Producer $producer)
  44. {
  45. $message->setFrom([$this->getEmailOpendistrib($producer) => $producer->name]);
  46. $message->setSubject($this->buildSubject($producer->name, $subject));
  47. return $message;
  48. }
  49. private function getEmailOpendistrib(Producer $producer): string
  50. {
  51. return $producer->slug . '@opendistrib.net';
  52. }
  53. private function buildSubject(string $prefix, string $subject)
  54. {
  55. return '['.$prefix.'] '.$subject;
  56. }
  57. }