Skip to content
Permalink
Browse files

Fix incorrectly encoded address fields.

Address fields cannot contain : or " it causes a number of
mail servers/clients to be confused.

Fixes #4058
  • Loading branch information...
markstory committed Sep 13, 2013
1 parent ba016c2 commit ba4afcc074a53239476bbe8471cd06c010dcf583
Showing with 31 additions and 3 deletions.
  1. +8 −3 lib/Cake/Network/Email/CakeEmail.php
  2. +23 −0 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -762,6 +762,10 @@ public function getHeaders($include = array()) {
/**
* Format addresses
*
* If the address contains non alphanumeric/whitespace characters, it will
* be quoted as characters like `:` and `,` are known to cause issues
* in address header fields.
*
* @param array $address
* @return array
*/
@@ -771,10 +775,11 @@ protected function _formatAddress($address) {
if ($email === $alias) {
$return[] = $email;
} else {
if (strpos($alias, ',') !== false) {
$alias = '"' . $alias . '"';
$encoded = $this->_encode($alias);
if ($encoded === $alias && preg_match('/[^a-z0-9 ]/i', $encoded)) {
$encoded = '"' . str_replace('"', '\"', $encoded) . '"';
}
$return[] = sprintf('%s <%s>', $this->_encode($alias), $email);
$return[] = sprintf('%s <%s>', $encoded, $email);
}
}
return $return;
@@ -178,6 +178,25 @@ public function testFrom() {
$result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
}
/**
* Test that from addresses using colons work.
*
* @return void
*/
public function testFromWithColonsAndQuotes() {
$address = array(
'info@example.com' => '70:20:00 " Forum'
);
$this->CakeEmail->from($address);
$this->assertEquals($address, $this->CakeEmail->from());
$this->CakeEmail->to('info@example.com')
->subject('Test email')
->transport('Debug');
$result = $this->CakeEmail->send();
$this->assertContains('From: "70:20:00 \" Forum" <info@example.com>', $result['headers']);
}
/**
* testSender method
*
@@ -371,6 +390,10 @@ public function testFormatAddress() {
$expected = array('"Last, First" <me@example.com>');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('me@example.com' => '"Last" First'));
$expected = array('"\"Last\" First" <me@example.com>');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last First'));
$expected = array('Last First <me@example.com>');
$this->assertSame($expected, $result);

0 comments on commit ba4afcc

Please sign in to comment.
You can’t perform that action at this time.