diff --git a/src/Monolog/Handler/MailHandler.php b/src/Monolog/Handler/MailHandler.php index 921879ae..c56bac40 100644 --- a/src/Monolog/Handler/MailHandler.php +++ b/src/Monolog/Handler/MailHandler.php @@ -53,7 +53,9 @@ abstract class MailHandler extends AbstractHandler $messages[] = $record['message']; } - $this->send($this->createMessage($messages)); + if (!empty($messages)) { + $this->send($this->createMessage($messages)); + } } /** diff --git a/src/Monolog/Handler/SwiftMailerHandler.php b/src/Monolog/Handler/SwiftMailerHandler.php index 598d7041..6f862180 100644 --- a/src/Monolog/Handler/SwiftMailerHandler.php +++ b/src/Monolog/Handler/SwiftMailerHandler.php @@ -11,6 +11,8 @@ namespace Monolog\Handler; +use Monolog\Logger; + /** * SwiftMailerHandler uses Swift_Mailer to send the emails * @@ -26,11 +28,15 @@ class SwiftMailerHandler extends MailHandler * @param \Swift_Mailer $mailer The mailer to use * @param \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, \Swift_Message $message) + public function __construct(\Swift_Mailer $mailer, \Swift_Message $message, $level = Logger::ERROR, $bubble = false) { $this->mailer = $mailer; $this->message = $message; + $this->level = $level; + $this->bubble = $bubble; } /** diff --git a/tests/Monolog/Handler/MailHandlerTest.php b/tests/Monolog/Handler/MailHandlerTest.php index 9af57049..2d9a6a84 100644 --- a/tests/Monolog/Handler/MailHandlerTest.php +++ b/tests/Monolog/Handler/MailHandlerTest.php @@ -37,6 +37,22 @@ class MailHandlerTest extends TestCase $handler->handleBatch($records); } + public function testHandleBatchNotSendsMailIfMessagesAreBelowLevel() + { + $records = array( + $this->getRecord(Logger::DEBUG, 'debug message 1'), + $this->getRecord(Logger::DEBUG, 'debug message 2'), + $this->getRecord(Logger::INFO, 'information'), + ); + + $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler'); + $handler->expects($this->never()) + ->method('send'); + $handler->setLevel(Logger::ERROR); + + $handler->handleBatch($records); + } + public function testHandle() { $record = $this->getRecord();