1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-22 00:56:08 +02:00
Files
php-monolog/src/Monolog/Handler/SwiftMailerHandler.php
John Kary f5eaeeb144 Swift_Message creation is now lazy when using callback
Swift_Message object is not created until right before the message is
sent, if it is sent at all. This prevents the expensive Swift_Mailer
autoloader from being invoked.

Swift_Mailer is now also required to run test suite
2015-03-01 20:03:39 -06:00

73 lines
2.0 KiB
PHP

<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\Logger;
/**
* SwiftMailerHandler uses Swift_Mailer to send the emails
*
* @author Gyula Sallai
*/
class SwiftMailerHandler extends MailHandler
{
protected $mailer;
protected $message;
/**
* @param \Swift_Mailer $mailer The mailer to use
* @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced
* @param integer $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true)
{
parent::__construct($level, $bubble);
$this->mailer = $mailer;
$this->message = $message;
}
/**
* {@inheritdoc}
*/
protected function send($content, array $records)
{
$this->mailer->send($this->buildMessage($content));
}
/**
* Creates instance of Swift_Message to be sent
*
* @param string $content
* @return \Swift_Message
*/
protected function buildMessage($content)
{
$message = null;
if ($this->message instanceof \Swift_Message) {
$message = clone $this->message;
} else if (is_callable($this->message)) {
$message = call_user_func($this->message);
}
if (!$message instanceof \Swift_Message) {
throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
}
$message->setBody($content);
$message->setDate(time());
return $message;
}
}