SwiftMailerHandler.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. * @param \Swift_Mailer $mailer The mailer to use
  27. * @param callable|Swift_Message $message An example message for real messages, only the body will be replaced
  28. * @param string|int $level The minimum logging level at which this handler will be triggered
  29. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  30. */
  31. public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, bool $bubble = true)
  32. {
  33. parent::__construct($level, $bubble);
  34. $this->mailer = $mailer;
  35. $this->messageTemplate = $message;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function send(string $content, array $records): void
  41. {
  42. $this->mailer->send($this->buildMessage($content, $records));
  43. }
  44. /**
  45. * Gets the formatter for the Swift_Message subject.
  46. *
  47. * @param string|null $format The format of the subject
  48. */
  49. protected function getSubjectFormatter(?string $format): FormatterInterface
  50. {
  51. return new LineFormatter($format);
  52. }
  53. /**
  54. * Creates instance of Swift_Message to be sent
  55. *
  56. * @param string $content formatted email body to be sent
  57. * @param array $records Log records that formed the content
  58. * @return Swift_Message
  59. */
  60. protected function buildMessage(string $content, array $records): Swift_Message
  61. {
  62. $message = null;
  63. if ($this->messageTemplate instanceof Swift_Message) {
  64. $message = clone $this->messageTemplate;
  65. $message->generateId();
  66. } elseif (is_callable($this->messageTemplate)) {
  67. $message = call_user_func($this->messageTemplate, $content, $records);
  68. }
  69. if (!$message instanceof Swift_Message) {
  70. throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
  71. }
  72. if ($records) {
  73. $subjectFormatter = $this->getSubjectFormatter($message->getSubject());
  74. $message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
  75. }
  76. $mime = 'text/plain';
  77. if ($this->isHtmlBody($content)) {
  78. $mime = 'text/html';
  79. }
  80. $message->setBody($content, $mime);
  81. if (version_compare(Swift::VERSION, '6.0.0', '>=')) {
  82. $message->setDate(new \DateTimeImmutable());
  83. } else {
  84. $message->setDate(time());
  85. }
  86. return $message;
  87. }
  88. }