SwiftMailerHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  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\LineFormatter;
  13. use Swift;
  14. /**
  15. * SwiftMailerHandler uses Swift_Mailer to send the emails
  16. *
  17. * @author Gyula Sallai
  18. */
  19. class SwiftMailerHandler extends MailHandler
  20. {
  21. protected $mailer;
  22. private $messageTemplate;
  23. /**
  24. * @param \Swift_Mailer $mailer The mailer to use
  25. * @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced
  26. * @param int $level The minimum logging level at which this handler will be triggered
  27. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  28. */
  29. public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true)
  30. {
  31. parent::__construct($level, $bubble);
  32. $this->mailer = $mailer;
  33. $this->messageTemplate = $message;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function send($content, array $records)
  39. {
  40. $this->mailer->send($this->buildMessage($content, $records));
  41. }
  42. /**
  43. * Creates instance of Swift_Message to be sent
  44. *
  45. * @param string $content formatted email body to be sent
  46. * @param array $records Log records that formed the content
  47. * @return \Swift_Message
  48. */
  49. protected function buildMessage($content, array $records)
  50. {
  51. $message = null;
  52. if ($this->messageTemplate instanceof \Swift_Message) {
  53. $message = clone $this->messageTemplate;
  54. $message->generateId();
  55. } elseif (is_callable($this->messageTemplate)) {
  56. $message = call_user_func($this->messageTemplate, $content, $records);
  57. }
  58. if (!$message instanceof \Swift_Message) {
  59. throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
  60. }
  61. if ($records) {
  62. $subjectFormatter = new LineFormatter($message->getSubject());
  63. $message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
  64. }
  65. $message->setBody($content);
  66. if (version_compare(Swift::VERSION, '6.0.0', '>=')) {
  67. $message->setDate(new \DateTimeImmutable());
  68. } else {
  69. $message->setDate(time());
  70. }
  71. return $message;
  72. }
  73. /**
  74. * BC getter, to be removed in 2.0
  75. */
  76. public function __get($name)
  77. {
  78. if ($name === 'message') {
  79. trigger_error('SwiftMailerHandler->message is deprecated, use ->buildMessage() instead to retrieve the message', E_USER_DEPRECATED);
  80. return $this->buildMessage(null, array());
  81. }
  82. throw new \InvalidArgumentException('Invalid property '.$name);
  83. }
  84. }