1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-23 01:26:11 +02:00

Add ability to use formatter in email subject lines

This commit is contained in:
Jordi Boggiano
2016-04-12 18:05:41 +01:00
parent 1fa91efc3b
commit a754edc64c
5 changed files with 63 additions and 1 deletions

View File

@@ -52,4 +52,16 @@ abstract class MailHandler extends AbstractProcessingHandler
{
$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;
}
}

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
/**
* 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:')) {
$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);
foreach ($this->to as $to) {
mail($to, $this->subject, $content, $headers, $parameters);
mail($to, $subject, $content, $headers, $parameters);
}
}

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
/**
* 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');
}
if ($records) {
$subjectFormatter = new LineFormatter($message->getSubject());
$message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
}
$message->setBody($content);
$message->setDate(time());