SwiftMailerHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. use Monolog\Formatter\FormatterInterface;
  13. use Monolog\Formatter\LineFormatter;
  14. use Swift_Message;
  15. use Swift;
  16. /**
  17. * SwiftMailerHandler uses Swift_Mailer to send the emails
  18. *
  19. * @author Gyula Sallai
  20. */
  21. class SwiftMailerHandler extends MailHandler
  22. {
  23. protected $mailer;
  24. private $messageTemplate;
  25. /**
  26. * @psalm-param Swift_Message|callable(string, array): Swift_Message $message
  27. *
  28. * @param \Swift_Mailer $mailer The mailer to use
  29. * @param callable|Swift_Message $message An example message for real messages, only the body will be replaced
  30. * @param string|int $level The minimum logging level at which this handler will be triggered
  31. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  32. */
  33. public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, bool $bubble = true)
  34. {
  35. parent::__construct($level, $bubble);
  36. $this->mailer = $mailer;
  37. $this->messageTemplate = $message;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function send(string $content, array $records): void
  43. {
  44. $this->mailer->send($this->buildMessage($content, $records));
  45. }
  46. /**
  47. * Gets the formatter for the Swift_Message subject.
  48. *
  49. * @param string|null $format The format of the subject
  50. */
  51. protected function getSubjectFormatter(?string $format): FormatterInterface
  52. {
  53. return new LineFormatter($format);
  54. }
  55. /**
  56. * Creates instance of Swift_Message to be sent
  57. *
  58. * @param string $content formatted email body to be sent
  59. * @param array $records Log records that formed the content
  60. * @return Swift_Message
  61. */
  62. protected function buildMessage(string $content, array $records): Swift_Message
  63. {
  64. $message = null;
  65. if ($this->messageTemplate instanceof Swift_Message) {
  66. $message = clone $this->messageTemplate;
  67. $message->generateId();
  68. } elseif (is_callable($this->messageTemplate)) {
  69. $message = ($this->messageTemplate)($content, $records);
  70. }
  71. if (!$message instanceof Swift_Message) {
  72. throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
  73. }
  74. if ($records) {
  75. $subjectFormatter = $this->getSubjectFormatter($message->getSubject());
  76. $message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
  77. }
  78. $mime = 'text/plain';
  79. if ($this->isHtmlBody($content)) {
  80. $mime = 'text/html';
  81. }
  82. $message->setBody($content, $mime);
  83. /** @phpstan-ignore-next-line */
  84. if (version_compare(Swift::VERSION, '6.0.0', '>=')) {
  85. $message->setDate(new \DateTimeImmutable());
  86. } else {
  87. /** @phpstan-ignore-next-line */
  88. $message->setDate(time());
  89. }
  90. return $message;
  91. }
  92. }