Skip to content

Commit

Permalink
Add mail header constants, fixes PHPMailer#1428 (PHPMailer#1429)
Browse files Browse the repository at this point in the history
* add mail header constants, fixes PHPMailer#1428
  • Loading branch information
maxbeckers authored and Synchro committed Apr 27, 2018
1 parent ce9b49b commit bbdfa96
Showing 1 changed file with 68 additions and 52 deletions.
120 changes: 68 additions & 52 deletions src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@
*/
class PHPMailer
{
const CHARSET_ISO88591 = 'iso-8859-1';
const CHARSET_UTF8 = 'utf-8';

const CONTENT_TYPE_PLAINTEXT = 'text/plain';
const CONTENT_TYPE_TEXT_CALENDAR = 'text/calendar';
const CONTENT_TYPE_TEXT_HTML = 'text/html';
const CONTENT_TYPE_MULTIPART_ALTERNATIVE = 'multipart/alternative';
const CONTENT_TYPE_MULTIPART_MIXED = 'multipart/mixed';
const CONTENT_TYPE_MULTIPART_RELATED = 'multipart/related';

const ENCODING_7BIT = '7bit';
const ENCODING_8BIT = '8bit';
const ENCODING_BASE64 = 'base64';
const ENCODING_BINARY = 'binary';
const ENCODING_QUOTED_PRINTABLE = 'quoted-printable';

/**
* Email priority.
* Options: null (default), 1 = High, 3 = Normal, 5 = low.
Expand All @@ -44,22 +60,22 @@ class PHPMailer
*
* @var string
*/
public $CharSet = 'iso-8859-1';
public $CharSet = self::CHARSET_ISO88591;

/**
* The MIME Content-type of the message.
*
* @var string
*/
public $ContentType = 'text/plain';
public $ContentType = self::CONTENT_TYPE_PLAINTEXT;

/**
* The message encoding.
* Options: "8bit", "7bit", "binary", "base64", and "quoted-printable".
*
* @var string
*/
public $Encoding = '8bit';
public $Encoding = self::ENCODING_8BIT;

/**
* Holds the most recent mailer error message.
Expand Down Expand Up @@ -837,9 +853,9 @@ protected function edebug($str)
public function isHTML($isHtml = true)
{
if ($isHtml) {
$this->ContentType = 'text/html';
$this->ContentType = static::CONTENT_TYPE_TEXT_HTML;
} else {
$this->ContentType = 'text/plain';
$this->ContentType = static::CONTENT_TYPE_PLAINTEXT;
}
}

Expand Down Expand Up @@ -1402,7 +1418,7 @@ public function preSend()

// Set whether the message is multipart/alternative
if ($this->alternativeExists()) {
$this->ContentType = 'multipart/alternative';
$this->ContentType = static::CONTENT_TYPE_MULTIPART_ALTERNATIVE;
}

$this->setMessageType();
Expand Down Expand Up @@ -2349,19 +2365,19 @@ public function getMailMIME()
$ismultipart = true;
switch ($this->message_type) {
case 'inline':
$result .= $this->headerLine('Content-Type', 'multipart/related;');
$result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
$result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
break;
case 'attach':
case 'inline_attach':
case 'alt_attach':
case 'alt_inline_attach':
$result .= $this->headerLine('Content-Type', 'multipart/mixed;');
$result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_MIXED . ';');
$result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
break;
case 'alt':
case 'alt_inline':
$result .= $this->headerLine('Content-Type', 'multipart/alternative;');
$result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';');
$result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
break;
default:
Expand All @@ -2371,11 +2387,11 @@ public function getMailMIME()
break;
}
// RFC1341 part 5 says 7bit is assumed if not specified
if ('7bit' != $this->Encoding) {
if (static::ENCODING_7BIT != $this->Encoding) {
// RFC 2045 section 6.4 says multipart MIME parts may only use 7bit, 8bit or binary CTE
if ($ismultipart) {
if ('8bit' == $this->Encoding) {
$result .= $this->headerLine('Content-Transfer-Encoding', '8bit');
if (static::ENCODING_8BIT == $this->Encoding) {
$result .= $this->headerLine('Content-Transfer-Encoding', static::ENCODING_8BIT);
}
// The only remaining alternatives are quoted-printable and base64, which are both 7bit compatible
} else {
Expand Down Expand Up @@ -2451,29 +2467,29 @@ public function createBody()
$bodyEncoding = $this->Encoding;
$bodyCharSet = $this->CharSet;
//Can we do a 7-bit downgrade?
if ('8bit' == $bodyEncoding and !$this->has8bitChars($this->Body)) {
$bodyEncoding = '7bit';
if (static::ENCODING_8BIT == $bodyEncoding and !$this->has8bitChars($this->Body)) {
$bodyEncoding = static::ENCODING_7BIT;
//All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit
$bodyCharSet = 'us-ascii';
}
//If lines are too long, and we're not already using an encoding that will shorten them,
//change to quoted-printable transfer encoding for the body part only
if ('base64' != $this->Encoding and static::hasLineLongerThanMax($this->Body)) {
$bodyEncoding = 'quoted-printable';
if (static::ENCODING_BASE64 != $this->Encoding and static::hasLineLongerThanMax($this->Body)) {
$bodyEncoding = static::ENCODING_QUOTED_PRINTABLE;
}

$altBodyEncoding = $this->Encoding;
$altBodyCharSet = $this->CharSet;
//Can we do a 7-bit downgrade?
if ('8bit' == $altBodyEncoding and !$this->has8bitChars($this->AltBody)) {
$altBodyEncoding = '7bit';
if (static::ENCODING_8BIT == $altBodyEncoding and !$this->has8bitChars($this->AltBody)) {
$altBodyEncoding = static::ENCODING_7BIT;
//All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit
$altBodyCharSet = 'us-ascii';
}
//If lines are too long, and we're not already using an encoding that will shorten them,
//change to quoted-printable transfer encoding for the alt body part only
if ('base64' != $altBodyEncoding and static::hasLineLongerThanMax($this->AltBody)) {
$altBodyEncoding = 'quoted-printable';
if (static::ENCODING_BASE64 != $altBodyEncoding and static::hasLineLongerThanMax($this->AltBody)) {
$altBodyEncoding = static::ENCODING_QUOTED_PRINTABLE;
}
//Use this as a preamble in all multipart message types
$mimepre = 'This is a multi-part message in MIME format.' . static::$LE;
Expand All @@ -2495,7 +2511,7 @@ public function createBody()
case 'inline_attach':
$body .= $mimepre;
$body .= $this->textLine('--' . $this->boundary[1]);
$body .= $this->headerLine('Content-Type', 'multipart/related;');
$body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
$body .= static::$LE;
$body .= $this->getBoundary($this->boundary[2], $bodyCharSet, '', $bodyEncoding);
Expand All @@ -2507,29 +2523,29 @@ public function createBody()
break;
case 'alt':
$body .= $mimepre;
$body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, 'text/plain', $altBodyEncoding);
$body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, static::CONTENT_TYPE_PLAINTEXT, $altBodyEncoding);
$body .= $this->encodeString($this->AltBody, $altBodyEncoding);
$body .= static::$LE;
$body .= $this->getBoundary($this->boundary[1], $bodyCharSet, 'text/html', $bodyEncoding);
$body .= $this->getBoundary($this->boundary[1], $bodyCharSet, static::CONTENT_TYPE_TEXT_HTML, $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= static::$LE;
if (!empty($this->Ical)) {
$body .= $this->getBoundary($this->boundary[1], '', 'text/calendar; method=REQUEST', '');
$body .= $this->getBoundary($this->boundary[1], '', static::CONTENT_TYPE_TEXT_CALENDAR . '; method=REQUEST', '');
$body .= $this->encodeString($this->Ical, $this->Encoding);
$body .= static::$LE;
}
$body .= $this->endBoundary($this->boundary[1]);
break;
case 'alt_inline':
$body .= $mimepre;
$body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, 'text/plain', $altBodyEncoding);
$body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, static::CONTENT_TYPE_PLAINTEXT, $altBodyEncoding);
$body .= $this->encodeString($this->AltBody, $altBodyEncoding);
$body .= static::$LE;
$body .= $this->textLine('--' . $this->boundary[1]);
$body .= $this->headerLine('Content-Type', 'multipart/related;');
$body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
$body .= static::$LE;
$body .= $this->getBoundary($this->boundary[2], $bodyCharSet, 'text/html', $bodyEncoding);
$body .= $this->getBoundary($this->boundary[2], $bodyCharSet, static::CONTENT_TYPE_TEXT_HTML, $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= static::$LE;
$body .= $this->attachAll('inline', $this->boundary[2]);
Expand All @@ -2539,17 +2555,17 @@ public function createBody()
case 'alt_attach':
$body .= $mimepre;
$body .= $this->textLine('--' . $this->boundary[1]);
$body .= $this->headerLine('Content-Type', 'multipart/alternative;');
$body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
$body .= static::$LE;
$body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, 'text/plain', $altBodyEncoding);
$body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, static::CONTENT_TYPE_PLAINTEXT, $altBodyEncoding);
$body .= $this->encodeString($this->AltBody, $altBodyEncoding);
$body .= static::$LE;
$body .= $this->getBoundary($this->boundary[2], $bodyCharSet, 'text/html', $bodyEncoding);
$body .= $this->getBoundary($this->boundary[2], $bodyCharSet, static::CONTENT_TYPE_TEXT_HTML, $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= static::$LE;
if (!empty($this->Ical)) {
$body .= $this->getBoundary($this->boundary[2], '', 'text/calendar; method=REQUEST', '');
$body .= $this->getBoundary($this->boundary[2], '', static::CONTENT_TYPE_TEXT_CALENDAR . '; method=REQUEST', '');
$body .= $this->encodeString($this->Ical, $this->Encoding);
}
$body .= $this->endBoundary($this->boundary[2]);
Expand All @@ -2559,17 +2575,17 @@ public function createBody()
case 'alt_inline_attach':
$body .= $mimepre;
$body .= $this->textLine('--' . $this->boundary[1]);
$body .= $this->headerLine('Content-Type', 'multipart/alternative;');
$body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
$body .= static::$LE;
$body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, 'text/plain', $altBodyEncoding);
$body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, static::CONTENT_TYPE_PLAINTEXT, $altBodyEncoding);
$body .= $this->encodeString($this->AltBody, $altBodyEncoding);
$body .= static::$LE;
$body .= $this->textLine('--' . $this->boundary[2]);
$body .= $this->headerLine('Content-Type', 'multipart/related;');
$body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[3] . '"');
$body .= static::$LE;
$body .= $this->getBoundary($this->boundary[3], $bodyCharSet, 'text/html', $bodyEncoding);
$body .= $this->getBoundary($this->boundary[3], $bodyCharSet, static::CONTENT_TYPE_TEXT_HTML, $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= static::$LE;
$body .= $this->attachAll('inline', $this->boundary[3]);
Expand Down Expand Up @@ -2671,7 +2687,7 @@ protected function getBoundary($boundary, $charSet, $contentType, $encoding)
$result .= sprintf('Content-Type: %s; charset=%s', $contentType, $charSet);
$result .= static::$LE;
// RFC1341 part 5 says 7bit is assumed if not specified
if ('7bit' != $encoding) {
if (static::ENCODING_7BIT != $encoding) {
$result .= $this->headerLine('Content-Transfer-Encoding', $encoding);
}
$result .= static::$LE;
Expand Down Expand Up @@ -2754,7 +2770,7 @@ public function textLine($value)
*
* @return bool
*/
public function addAttachment($path, $name = '', $encoding = 'base64', $type = '', $disposition = 'attachment')
public function addAttachment($path, $name = '', $encoding = self::ENCODING_BASE64, $type = '', $disposition = 'attachment')
{
try {
if (!@is_file($path)) {
Expand Down Expand Up @@ -2866,7 +2882,7 @@ protected function attachAll($disposition_type, $boundary)
);
}
// RFC1341 part 5 says 7bit is assumed if not specified
if ('7bit' != $encoding) {
if (static::ENCODING_7BIT != $encoding) {
$mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, static::$LE);
}

Expand Down Expand Up @@ -2936,7 +2952,7 @@ protected function attachAll($disposition_type, $boundary)
*
* @return string
*/
protected function encodeFile($path, $encoding = 'base64')
protected function encodeFile($path, $encoding = self::ENCODING_BASE64)
{
try {
if (!file_exists($path)) {
Expand All @@ -2961,33 +2977,33 @@ protected function encodeFile($path, $encoding = 'base64')
* Returns an empty string on failure.
*
* @param string $str The text to encode
* @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable
* @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
*
* @return string
*/
public function encodeString($str, $encoding = 'base64')
public function encodeString($str, $encoding = self::ENCODING_BASE64)
{
$encoded = '';
switch (strtolower($encoding)) {
case 'base64':
case static::ENCODING_BASE64:
$encoded = chunk_split(
base64_encode($str),
static::STD_LINE_LENGTH,
static::$LE
);
break;
case '7bit':
case '8bit':
case static::ENCODING_7BIT:
case static::ENCODING_8BIT:
$encoded = static::normalizeBreaks($str);
// Make sure it ends with a line break
if (substr($encoded, -(strlen(static::$LE))) != static::$LE) {
$encoded .= static::$LE;
}
break;
case 'binary':
case static::ENCODING_BINARY:
$encoded = $str;
break;
case 'quoted-printable':
case static::ENCODING_QUOTED_PRINTABLE:
$encoded = $this->encodeQP($str);
break;
default:
Expand Down Expand Up @@ -3238,7 +3254,7 @@ public function encodeQ($str, $position = 'text')
public function addStringAttachment(
$string,
$filename,
$encoding = 'base64',
$encoding = self::ENCODING_BASE64,
$type = '',
$disposition = 'attachment'
) {
Expand Down Expand Up @@ -3278,7 +3294,7 @@ public function addStringAttachment(
*
* @return bool True on successfully adding an attachment
*/
public function addEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = '', $disposition = 'inline')
public function addEmbeddedImage($path, $cid, $name = '', $encoding = self::ENCODING_BASE64, $type = '', $disposition = 'inline')
{
if (!@is_file($path)) {
$this->setError($this->lang('file_access') . $path);
Expand Down Expand Up @@ -3332,7 +3348,7 @@ public function addStringEmbeddedImage(
$string,
$cid,
$name = '',
$encoding = 'base64',
$encoding = self::ENCODING_BASE64,
$type = '',
$disposition = 'inline'
) {
Expand Down Expand Up @@ -3700,7 +3716,7 @@ public function msgHTML($message, $basedir = '', $advanced = false)
// Convert data URIs into embedded images
//e.g. "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
if (preg_match('#^data:(image/(?:jpe?g|gif|png));?(base64)?,(.+)#', $url, $match)) {
if (count($match) == 4 and 'base64' == $match[2]) {
if (count($match) == 4 and static::ENCODING_BASE64 == $match[2]) {
$data = base64_decode($match[3]);
} elseif ('' == $match[2]) {
$data = rawurldecode($match[3]);
Expand All @@ -3713,7 +3729,7 @@ public function msgHTML($message, $basedir = '', $advanced = false)
$cid = hash('sha256', $data) . '@phpmailer.0'; // RFC2392 S 2

if (!$this->cidExists($cid)) {
$this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, 'base64', $match[1]);
$this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, static::ENCODING_BASE64, $match[1]);
}
$message = str_replace(
$images[0][$imgindex],
Expand Down Expand Up @@ -3747,7 +3763,7 @@ public function msgHTML($message, $basedir = '', $advanced = false)
$basedir . $directory . $filename,
$cid,
$filename,
'base64',
static::ENCODING_BASE64,
static::_mime_types((string) static::mb_pathinfo($filename, PATHINFO_EXTENSION))
)
) {
Expand Down

0 comments on commit bbdfa96

Please sign in to comment.