SwiftMailerHandler.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. /**
  13. * SwiftMailerHandler uses Swift_Mailer to send the emails
  14. *
  15. * @author Gyula Sallai
  16. */
  17. class SwiftMailerHandler extends MailHandler
  18. {
  19. protected $mailer;
  20. protected $message;
  21. /**
  22. * @param \Swift_Mailer $mailer The mailer to use
  23. * @param callback|\Swift_Message $message An example message for real messages, only the body will be replaced
  24. * @param integer $level The minimum logging level at which this handler will be triggered
  25. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  26. */
  27. public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true)
  28. {
  29. parent::__construct($level, $bubble);
  30. $this->mailer = $mailer;
  31. if (!$message instanceof \Swift_Message && is_callable($message)) {
  32. $message = call_user_func($message);
  33. }
  34. if (!$message instanceof \Swift_Message) {
  35. throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callback returning it');
  36. }
  37. $this->message = $message;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function send($content)
  43. {
  44. $message = clone $this->message;
  45. $message->setBody($content);
  46. $this->mailer->send($message);
  47. }
  48. }