diff --git a/src/Monolog/Handler/MailHandler.php b/src/Monolog/Handler/MailHandler.php index 640af29e..2b9d74c8 100644 --- a/src/Monolog/Handler/MailHandler.php +++ b/src/Monolog/Handler/MailHandler.php @@ -11,6 +11,9 @@ namespace Monolog\Handler; +use Monolog\Formatter\FormatterInterface; +use Monolog\Formatter\HtmlFormatter; + /** * Base class for all mail handlers * @@ -64,4 +67,19 @@ abstract class MailHandler extends AbstractProcessingHandler return $highestRecord; } + + protected function isHtmlBody($body) + { + return substr($body, 0, 1) === '<'; + } + + /** + * Gets the default formatter. + * + * @return FormatterInterface + */ + protected function getDefaultFormatter(): FormatterInterface + { + return new HtmlFormatter(); + } } diff --git a/src/Monolog/Handler/MandrillHandler.php b/src/Monolog/Handler/MandrillHandler.php index e30f603a..1fb9dc6a 100644 --- a/src/Monolog/Handler/MandrillHandler.php +++ b/src/Monolog/Handler/MandrillHandler.php @@ -48,8 +48,13 @@ class MandrillHandler extends MailHandler */ protected function send($content, array $records) { + $mime = null; + if ($this->isHtmlBody($content)) { + $mime = 'text/html'; + } + $message = clone $this->message; - $message->setBody($content); + $message->setBody($content, $mime); $message->setDate(time()); $ch = curl_init(); diff --git a/src/Monolog/Handler/NativeMailerHandler.php b/src/Monolog/Handler/NativeMailerHandler.php index f6fb0430..4289e35e 100644 --- a/src/Monolog/Handler/NativeMailerHandler.php +++ b/src/Monolog/Handler/NativeMailerHandler.php @@ -56,7 +56,7 @@ class NativeMailerHandler extends MailHandler * The Content-type for the message * @var string */ - protected $contentType = 'text/plain'; + protected $contentType; /** * The encoding for the message @@ -117,10 +117,15 @@ class NativeMailerHandler extends MailHandler */ protected function send($content, array $records) { - $content = wordwrap($content, $this->maxColumnWidth); + $contentType = $this->getContentType() ?: ($this->isHtmlBody($content) ? 'text/html' : 'text/plain'); + + if ($contentType !== 'text/html') { + $content = wordwrap($content, $this->maxColumnWidth); + } + $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n"); - $headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n"; - if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) { + $headers .= 'Content-type: ' . $contentType . '; charset=' . $this->getEncoding() . "\r\n"; + if ($contentType === 'text/html' && false === strpos($headers, 'MIME-Version:')) { $headers .= 'MIME-Version: 1.0' . "\r\n"; } diff --git a/src/Monolog/Handler/SendGridHandler.php b/src/Monolog/Handler/SendGridHandler.php index 14395509..cee69861 100644 --- a/src/Monolog/Handler/SendGridHandler.php +++ b/src/Monolog/Handler/SendGridHandler.php @@ -82,9 +82,14 @@ class SendGridHandler extends MailHandler $message['to[]'] = $recipient; } $message['subject'] = $this->subject; - $message['text'] = $content; $message['date'] = date('r'); + if ($this->isHtmlBody($content)) { + $message['html'] = $content; + } else { + $message['text'] = $content; + } + $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json'); curl_setopt($ch, CURLOPT_POST, 1); diff --git a/src/Monolog/Handler/SwiftMailerHandler.php b/src/Monolog/Handler/SwiftMailerHandler.php index 92e3e939..44026fc3 100644 --- a/src/Monolog/Handler/SwiftMailerHandler.php +++ b/src/Monolog/Handler/SwiftMailerHandler.php @@ -72,7 +72,12 @@ class SwiftMailerHandler extends MailHandler $message->setSubject($subjectFormatter->format($this->getHighestRecord($records))); } - $message->setBody($content); + $mime = null; + if ($this->isHtmlBody($content)) { + $mime = 'text/html'; + } + + $message->setBody($content, $mime); $message->setDate(time()); return $message; diff --git a/tests/Monolog/Handler/MailHandlerTest.php b/tests/Monolog/Handler/MailHandlerTest.php index e6169325..c489e263 100644 --- a/tests/Monolog/Handler/MailHandlerTest.php +++ b/tests/Monolog/Handler/MailHandlerTest.php @@ -61,6 +61,7 @@ class MailHandlerTest extends TestCase public function testHandle() { $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler'); + $handler->setFormatter(new \Monolog\Formatter\LineFormatter); $record = $this->getRecord(); $records = [$record]; diff --git a/tests/Monolog/Handler/NativeMailerHandlerTest.php b/tests/Monolog/Handler/NativeMailerHandlerTest.php index 6d0fc487..9ac08be6 100644 --- a/tests/Monolog/Handler/NativeMailerHandlerTest.php +++ b/tests/Monolog/Handler/NativeMailerHandlerTest.php @@ -78,6 +78,7 @@ class NativeMailerHandlerTest extends TestCase $from = 'receiver@example.org'; $mailer = new NativeMailerHandler($to, $subject, $from); + $mailer->setFormatter(new \Monolog\Formatter\LineFormatter); $mailer->handleBatch([]); // batch is empty, nothing sent