Skip to content

Commit

Permalink
Implement automatic 7bit downgrade, fixes PHPMailer#176
Browse files Browse the repository at this point in the history
  • Loading branch information
Synchro committed Feb 27, 2014
1 parent 8161579 commit 342e08c
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions class.phpmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1775,16 +1775,28 @@ public function createBody()

$this->setWordWrap();

$bodyEncoding = $this->Encoding;
$bodyCharSet = $this->CharSet;
if (!$this->has8bitChars($this->Body)) {
$bodyEncoding = '7bit';
$bodyCharSet = 'us-ascii';
}
$altBodyEncoding = $this->Encoding;
$altBodyCharSet = $this->CharSet;
if (!$this->has8bitChars($this->AltBody)) {
$altBodyEncoding = '7bit';
$altBodyCharSet = 'us-ascii';
}
switch ($this->message_type) {
case 'inline':
$body .= $this->getBoundary($this->boundary[1], '', '', '');
$body .= $this->encodeString($this->Body, $this->Encoding);
$body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->attachAll('inline', $this->boundary[1]);
break;
case 'attach':
$body .= $this->getBoundary($this->boundary[1], '', '', '');
$body .= $this->encodeString($this->Body, $this->Encoding);
$body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->attachAll('attachment', $this->boundary[1]);
break;
Expand All @@ -1793,19 +1805,19 @@ public function createBody()
$body .= $this->headerLine('Content-Type', 'multipart/related;');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
$body .= $this->LE;
$body .= $this->getBoundary($this->boundary[2], '', '', '');
$body .= $this->encodeString($this->Body, $this->Encoding);
$body .= $this->getBoundary($this->boundary[2], $bodyCharSet, '', $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->attachAll('inline', $this->boundary[2]);
$body .= $this->LE;
$body .= $this->attachAll('attachment', $this->boundary[1]);
break;
case 'alt':
$body .= $this->getBoundary($this->boundary[1], '', 'text/plain', '');
$body .= $this->encodeString($this->AltBody, $this->Encoding);
$body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, 'text/plain', $altBodyEncoding);
$body .= $this->encodeString($this->AltBody, $altBodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->getBoundary($this->boundary[1], '', 'text/html', '');
$body .= $this->encodeString($this->Body, $this->Encoding);
$body .= $this->getBoundary($this->boundary[1], $bodyCharSet, 'text/html', $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= $this->LE . $this->LE;
if (!empty($this->Ical)) {
$body .= $this->getBoundary($this->boundary[1], '', 'text/calendar; method=REQUEST', '');
Expand All @@ -1815,15 +1827,15 @@ public function createBody()
$body .= $this->endBoundary($this->boundary[1]);
break;
case 'alt_inline':
$body .= $this->getBoundary($this->boundary[1], '', 'text/plain', '');
$body .= $this->encodeString($this->AltBody, $this->Encoding);
$body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, 'text/plain', $altBodyEncoding);
$body .= $this->encodeString($this->AltBody, $altBodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->textLine('--' . $this->boundary[1]);
$body .= $this->headerLine('Content-Type', 'multipart/related;');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
$body .= $this->LE;
$body .= $this->getBoundary($this->boundary[2], '', 'text/html', '');
$body .= $this->encodeString($this->Body, $this->Encoding);
$body .= $this->getBoundary($this->boundary[2], $bodyCharSet, 'text/html', $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->attachAll('inline', $this->boundary[2]);
$body .= $this->LE;
Expand All @@ -1834,11 +1846,11 @@ public function createBody()
$body .= $this->headerLine('Content-Type', 'multipart/alternative;');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
$body .= $this->LE;
$body .= $this->getBoundary($this->boundary[2], '', 'text/plain', '');
$body .= $this->encodeString($this->AltBody, $this->Encoding);
$body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, 'text/plain', $altBodyEncoding);
$body .= $this->encodeString($this->AltBody, $altBodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->getBoundary($this->boundary[2], '', 'text/html', '');
$body .= $this->encodeString($this->Body, $this->Encoding);
$body .= $this->getBoundary($this->boundary[2], $bodyCharSet, 'text/html', $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->endBoundary($this->boundary[2]);
$body .= $this->LE;
Expand All @@ -1849,15 +1861,15 @@ public function createBody()
$body .= $this->headerLine('Content-Type', 'multipart/alternative;');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
$body .= $this->LE;
$body .= $this->getBoundary($this->boundary[2], '', 'text/plain', '');
$body .= $this->encodeString($this->AltBody, $this->Encoding);
$body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, 'text/plain', $altBodyEncoding);
$body .= $this->encodeString($this->AltBody, $altBodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->textLine('--' . $this->boundary[2]);
$body .= $this->headerLine('Content-Type', 'multipart/related;');
$body .= $this->textLine("\tboundary=\"" . $this->boundary[3] . '"');
$body .= $this->LE;
$body .= $this->getBoundary($this->boundary[3], '', 'text/html', '');
$body .= $this->encodeString($this->Body, $this->Encoding);
$body .= $this->getBoundary($this->boundary[3], $bodyCharSet, 'text/html', $bodyEncoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
$body .= $this->LE . $this->LE;
$body .= $this->attachAll('inline', $this->boundary[3]);
$body .= $this->LE;
Expand All @@ -1867,7 +1879,7 @@ public function createBody()
break;
default:
// catch case 'plain' and case ''
$body .= $this->encodeString($this->Body, $this->Encoding);
$body .= $this->encodeString($this->Body, $bodyEncoding);
break;
}

Expand Down Expand Up @@ -2321,6 +2333,15 @@ public function hasMultiBytes($str)
}
}

/**
* Does a string contain any 8-bit chars (in any charset)?
* @param string $text
* @return bool
*/
public function has8bitChars($text) {
return (bool)preg_match('/[\x80-\xFF]/', $text);
}

/**
* Encode and wrap long multibyte strings for mail headers
* without breaking lines within a character.
Expand Down

0 comments on commit 342e08c

Please sign in to comment.