Permalink
Browse files

Fix incorrectly encoded headers in MailTransport.

When encoded values are present in header fields, they need to have
newlines removed as leaving the newlines in place and interfere with
delivery on Qmail based SMTP servers.

Fixes #2209
  • Loading branch information...
markstory committed Nov 5, 2013
1 parent d8acf53 commit 145dbacb78e15730ca49762f64181be57916b3ee
Showing with 17 additions and 6 deletions.
  1. +3 −0 lib/Cake/Network/Email/MailTransport.php
  2. +14 −6 lib/Cake/Test/Case/Network/Email/MailTransportTest.php
@@ -40,6 +40,9 @@ public function send(CakeEmail $email) {
$headers = $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'));
$to = $headers['To'];
unset($headers['To']);
+ foreach ($headers as $key => $header) {
+ $headers[$key] = str_replace(array("\r", "\n"), '', $header);
+ }
$headers = $this->_headersToString($headers, $eol);
$message = implode($eol, $email->message());
$subject = str_replace(array("\r", "\n"), '', $email->subject());
@@ -52,28 +52,36 @@ public function testSendData() {
$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
$email->bcc('phpnut@cakephp.org');
$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
- $email->subject('Foø Bår Béz Foø Bår Béz Foø Bår Béz Foø Bår Béz');
+ $longNonAscii = 'Foø Bår Béz Foø Bår Béz Foø Bår Béz Foø Bår Béz';
+ $email->subject($longNonAscii);
$date = date(DATE_RFC2822);
- $email->setHeaders(array('X-Mailer' => 'CakePHP Email', 'Date' => $date));
- $email->expects($this->any())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
+ $email->setHeaders(array(
+ 'X-Mailer' => 'CakePHP Email',
+ 'Date' => $date,
+ 'X-add' => mb_encode_mimeheader($longNonAscii, 'utf8', 'B'),
+ ));
+ $email->expects($this->any())->method('message')
+ ->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
+
+ $encoded = '=?UTF-8?B?Rm/DuCBCw6VyIELDqXogRm/DuCBCw6VyIELDqXogRm/DuCBCw6VyIELDqXog?=';
+ $encoded .= ' =?UTF-8?B?Rm/DuCBCw6VyIELDqXo=?=';
$data = "From: CakePHP Test <noreply@cakephp.org>" . PHP_EOL;
$data .= "Return-Path: CakePHP Return <pleasereply@cakephp.org>" . PHP_EOL;
$data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>" . PHP_EOL;
$data .= "Bcc: phpnut@cakephp.org" . PHP_EOL;
$data .= "X-Mailer: CakePHP Email" . PHP_EOL;
$data .= "Date: " . $date . PHP_EOL;
+ $data .= "X-add: " . $encoded . PHP_EOL;
$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>" . PHP_EOL;
$data .= "MIME-Version: 1.0" . PHP_EOL;
$data .= "Content-Type: text/plain; charset=UTF-8" . PHP_EOL;
$data .= "Content-Transfer-Encoding: 8bit";
- $subject = '=?UTF-8?B?Rm/DuCBCw6VyIELDqXogRm/DuCBCw6VyIELDqXogRm/DuCBCw6VyIELDqXog?=';
- $subject .= ' =?UTF-8?B?Rm/DuCBCw6VyIELDqXo=?=';
$this->MailTransport->expects($this->once())->method('_mail')
->with(
'CakePHP <cake@cakephp.org>',
- $subject,
+ $encoded,
implode(PHP_EOL, array('First Line', 'Second Line', '.Third Line', '')),
$data,
'-f'

0 comments on commit 145dbac

Please sign in to comment.