Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit ba4afcc074a53239476bbe8471cd06c010dcf583 1 parent ba016c2
@markstory markstory authored
View
11 lib/Cake/Network/Email/CakeEmail.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;
View
23 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -179,6 +179,25 @@ public function testFrom() {
}
/**
+ * 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
*
* @return void
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.