vendor/monolog/monolog/src/Monolog/Handler/SwiftMailerHandler.php line 42

Open in your IDE?
  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\Utils;
  13. use Monolog\Formatter\FormatterInterface;
  14. use Monolog\Formatter\LineFormatter;
  15. use Swift_Message;
  16. use Swift;
  17. /**
  18. * SwiftMailerHandler uses Swift_Mailer to send the emails
  19. *
  20. * @author Gyula Sallai
  21. *
  22. * @phpstan-import-type Record from \Monolog\Logger
  23. * @deprecated Since Monolog 2.6. Use SymfonyMailerHandler instead.
  24. */
  25. class SwiftMailerHandler extends MailHandler
  26. {
  27. /** @var \Swift_Mailer */
  28. protected $mailer;
  29. /** @var Swift_Message|callable(string, Record[]): Swift_Message */
  30. private $messageTemplate;
  31. /**
  32. * @psalm-param Swift_Message|callable(string, Record[]): Swift_Message $message
  33. *
  34. * @param \Swift_Mailer $mailer The mailer to use
  35. * @param callable|Swift_Message $message An example message for real messages, only the body will be replaced
  36. */
  37. public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, bool $bubble = true)
  38. {
  39. parent::__construct($level, $bubble);
  40. @trigger_error('The SwiftMailerHandler is deprecated since Monolog 2.6. Use SymfonyMailerHandler instead.', E_USER_DEPRECATED);
  41. $this->mailer = $mailer;
  42. $this->messageTemplate = $message;
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. protected function send(string $content, array $records): void
  48. {
  49. $this->mailer->send($this->buildMessage($content, $records));
  50. }
  51. /**
  52. * Gets the formatter for the Swift_Message subject.
  53. *
  54. * @param string|null $format The format of the subject
  55. */
  56. protected function getSubjectFormatter(?string $format): FormatterInterface
  57. {
  58. return new LineFormatter($format);
  59. }
  60. /**
  61. * Creates instance of Swift_Message to be sent
  62. *
  63. * @param string $content formatted email body to be sent
  64. * @param array $records Log records that formed the content
  65. * @return Swift_Message
  66. *
  67. * @phpstan-param Record[] $records
  68. */
  69. protected function buildMessage(string $content, array $records): Swift_Message
  70. {
  71. $message = null;
  72. if ($this->messageTemplate instanceof Swift_Message) {
  73. $message = clone $this->messageTemplate;
  74. $message->generateId();
  75. } elseif (is_callable($this->messageTemplate)) {
  76. $message = ($this->messageTemplate)($content, $records);
  77. }
  78. if (!$message instanceof Swift_Message) {
  79. $record = reset($records);
  80. throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it' . ($record ? Utils::getRecordMessageForException($record) : ''));
  81. }
  82. if ($records) {
  83. $subjectFormatter = $this->getSubjectFormatter($message->getSubject());
  84. $message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
  85. }
  86. $mime = 'text/plain';
  87. if ($this->isHtmlBody($content)) {
  88. $mime = 'text/html';
  89. }
  90. $message->setBody($content, $mime);
  91. /** @phpstan-ignore-next-line */
  92. if (version_compare(Swift::VERSION, '6.0.0', '>=')) {
  93. $message->setDate(new \DateTimeImmutable());
  94. } else {
  95. /** @phpstan-ignore-next-line */
  96. $message->setDate(time());
  97. }
  98. return $message;
  99. }
  100. }