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.

64 lines
2.2KB

  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 = [])
  20. {
  21. $message = $this->composeBase(\Yii::$app->parameterBag->get('adminEmail'), $view, $params);
  22. $this->initMessageFromSite($message, $subject);
  23. $message->send();
  24. }
  25. private function composeBase(string $toEmail, string $view, array $params = [])
  26. {
  27. return \Yii::$app->mailer
  28. ->compose([
  29. 'html' => '@common/mail/'.$view.'-html.php',
  30. 'text' => '@common/mail/'.$view.'-text.php'
  31. ], $params)
  32. ->setTo($toEmail);
  33. }
  34. private function initMessageFromSite(MessageInterface $message, string $subject)
  35. {
  36. $message->setFrom([\Yii::$app->parameterBag->get('adminEmail') => \Yii::$app->parameterBag->get('siteName')]);
  37. $message->setSubject($this->buildSubject(\Yii::$app->parameterBag->get('siteName'), $subject));
  38. return $message;
  39. }
  40. private function initMessageFromProducer(MessageInterface $message, string $subject, Producer $producer)
  41. {
  42. $message->setFrom([$this->getEmailOpendistrib($producer) => $producer->name]);
  43. $message->setSubject($this->buildSubject($producer->name, $subject));
  44. return $message;
  45. }
  46. private function getEmailOpendistrib(Producer $producer): string
  47. {
  48. return $producer->slug . '@opendistrib.net';
  49. }
  50. private function buildSubject(string $prefix, string $subject)
  51. {
  52. return '['.$prefix.'] '.$subject;
  53. }
  54. }