您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

64 行
2.1KB

  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->params['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->params['adminEmail'] => \Yii::$app->params['siteName']]);
  37. $message->setSubject($this->buildSubject(\Yii::$app->params['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. }