mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-07 21:56:31 +02:00
Add ability to use formatter in email subject lines
This commit is contained in:
@@ -52,4 +52,16 @@ abstract class MailHandler extends AbstractProcessingHandler
|
|||||||
{
|
{
|
||||||
$this->send((string) $record['formatted'], array($record));
|
$this->send((string) $record['formatted'], array($record));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getHighestRecord(array $records)
|
||||||
|
{
|
||||||
|
$highestRecord = null;
|
||||||
|
foreach ($records as $record) {
|
||||||
|
if ($highestRecord === null || $highestRecord['level'] < $record['level']) {
|
||||||
|
$highestRecord = $record;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $highestRecord;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,6 +12,7 @@
|
|||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NativeMailerHandler uses the mail() function to send the emails
|
* NativeMailerHandler uses the mail() function to send the emails
|
||||||
@@ -122,9 +123,16 @@ class NativeMailerHandler extends MailHandler
|
|||||||
if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) {
|
if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) {
|
||||||
$headers .= 'MIME-Version: 1.0' . "\r\n";
|
$headers .= 'MIME-Version: 1.0' . "\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$subject = $this->subject;
|
||||||
|
if ($records) {
|
||||||
|
$subjectFormatter = new LineFormatter($this->subject);
|
||||||
|
$subject = $subjectFormatter->format($this->getHighestRecord($records));
|
||||||
|
}
|
||||||
|
|
||||||
$parameters = implode(' ', $this->parameters);
|
$parameters = implode(' ', $this->parameters);
|
||||||
foreach ($this->to as $to) {
|
foreach ($this->to as $to) {
|
||||||
mail($to, $this->subject, $content, $headers, $parameters);
|
mail($to, $subject, $content, $headers, $parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,6 +12,7 @@
|
|||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SwiftMailerHandler uses Swift_Mailer to send the emails
|
* SwiftMailerHandler uses Swift_Mailer to send the emails
|
||||||
@@ -66,6 +67,11 @@ class SwiftMailerHandler extends MailHandler
|
|||||||
throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
|
throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($records) {
|
||||||
|
$subjectFormatter = new LineFormatter($message->getSubject());
|
||||||
|
$message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
|
||||||
|
}
|
||||||
|
|
||||||
$message->setBody($content);
|
$message->setBody($content);
|
||||||
$message->setDate(time());
|
$message->setDate(time());
|
||||||
|
|
||||||
|
@@ -96,4 +96,16 @@ class NativeMailerHandlerTest extends TestCase
|
|||||||
$this->assertSame("From: $from\r\nContent-type: text/plain; charset=utf-8\r\n", $params[3]);
|
$this->assertSame("From: $from\r\nContent-type: text/plain; charset=utf-8\r\n", $params[3]);
|
||||||
$this->assertSame('', $params[4]);
|
$this->assertSame('', $params[4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMessageSubjectFormatting()
|
||||||
|
{
|
||||||
|
$mailer = new NativeMailerHandler('to@example.org', 'Alert: %level_name% %message%', 'from@example.org');
|
||||||
|
$mailer->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
|
||||||
|
$this->assertNotEmpty($GLOBALS['mail']);
|
||||||
|
$this->assertInternalType('array', $GLOBALS['mail']);
|
||||||
|
$this->assertArrayHasKey('0', $GLOBALS['mail']);
|
||||||
|
$params = $GLOBALS['mail'][0];
|
||||||
|
$this->assertCount(5, $params);
|
||||||
|
$this->assertSame('Alert: ERROR Foo Bar Baz', $params[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -72,6 +72,30 @@ class SwiftMailerHandlerTest extends TestCase
|
|||||||
$handler->handleBatch($records);
|
$handler->handleBatch($records);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMessageSubjectFormatting()
|
||||||
|
{
|
||||||
|
// Wire Mailer to expect a specific Swift_Message with a customized Subject
|
||||||
|
$messageTemplate = new \Swift_Message();
|
||||||
|
$messageTemplate->setSubject('Alert: %level_name% %message%');
|
||||||
|
$receivedMessage = null;
|
||||||
|
|
||||||
|
$this->mailer->expects($this->once())
|
||||||
|
->method('send')
|
||||||
|
->with($this->callback(function ($value) use (&$receivedMessage) {
|
||||||
|
$receivedMessage = $value;
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
|
|
||||||
|
$handler = new SwiftMailerHandler($this->mailer, $messageTemplate);
|
||||||
|
|
||||||
|
$records = array(
|
||||||
|
$this->getRecord(Logger::EMERGENCY),
|
||||||
|
);
|
||||||
|
$handler->handleBatch($records);
|
||||||
|
|
||||||
|
$this->assertEquals('Alert: EMERGENCY test', $receivedMessage->getSubject());
|
||||||
|
}
|
||||||
|
|
||||||
public function testMessageHaveUniqueId()
|
public function testMessageHaveUniqueId()
|
||||||
{
|
{
|
||||||
$messageTemplate = \Swift_Message::newInstance();
|
$messageTemplate = \Swift_Message::newInstance();
|
||||||
|
Reference in New Issue
Block a user