1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-11 07:34:12 +02:00

Allow for different Content-type and character encodings; Insert a MIME-Version header for HTML emails if there isn't one already; More documentation;

This commit is contained in:
Mark Garrett
2014-03-04 11:34:35 -06:00
parent 0ac27c8089
commit e490186a7c

View File

@@ -1,5 +1,4 @@
<?php <?php
/* /*
* This file is part of the Monolog package. * This file is part of the Monolog package.
* *
@@ -17,16 +16,46 @@ use Monolog\Logger;
* NativeMailerHandler uses the mail() function to send the emails * NativeMailerHandler uses the mail() function to send the emails
* *
* @author Christophe Coevoet <stof@notk.org> * @author Christophe Coevoet <stof@notk.org>
* @author Mark Garrett <mark@moderndeveloperllc.com>
*/ */
class NativeMailerHandler extends MailHandler class NativeMailerHandler extends MailHandler
{ {
/**
* The email address to which the message is delivered
* @var string
*/
protected $to; protected $to;
/**
* The subject of the email
* @var string
*/
protected $subject; protected $subject;
protected $headers = array(
'Content-type: text/plain; charset=utf-8' /**
); * Optional headers for the message
* @var array
*/
protected $headers = array();
/**
* The wordwrap length for the message
* @var integer
*/
protected $maxColumnWidth; protected $maxColumnWidth;
/**
* The Content-type for the message
* @var string
*/
protected $contentType = 'text/plain';
/**
* The encoding for the message
* @var string
*/
protected $encoding = 'utf-8';
/** /**
* @param string|array $to The receiver of the mail * @param string|array $to The receiver of the mail
* @param string $subject The subject of the mail * @param string $subject The subject of the mail
@@ -45,7 +74,10 @@ class NativeMailerHandler extends MailHandler
} }
/** /**
* Add headers to the message
*
* @param string|array $headers Custom added headers * @param string|array $headers Custom added headers
* @return null
*/ */
public function addHeader($headers) public function addHeader($headers)
{ {
@@ -64,8 +96,52 @@ class NativeMailerHandler extends MailHandler
{ {
$content = wordwrap($content, $this->maxColumnWidth); $content = wordwrap($content, $this->maxColumnWidth);
$headers = implode("\r\n", $this->headers) . "\r\n"; $headers = implode("\r\n", $this->headers) . "\r\n";
$headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n";
if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) {
$headers .= 'MIME-Version: 1.0' . "\r\n";
}
foreach ($this->to as $to) { foreach ($this->to as $to) {
mail($to, $this->subject, $content, $headers); mail($to, $this->subject, $content, $headers);
} }
} }
/**
* @return string $contentType
*/
public function getContentType()
{
return $this->contentType;
}
/**
* @return string $encoding
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML
* messages.
* @return self
*/
public function setContentType($contentType)
{
$this->contentType = $contentType;
return $this;
}
/**
* @param string $encoding
* @return self
*/
public function setEncoding($encoding)
{
$this->encoding = $encoding;
return $this;
}
} }