Skip to content
This repository
Browse code

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
Mark Story authored
11  lib/Cake/Network/Email/CakeEmail.php
@@ -762,6 +762,10 @@ public function getHeaders($include = array()) {
762 762
 /**
763 763
  * Format addresses
764 764
  *
  765
+ * If the address contains non alphanumeric/whitespace characters, it will
  766
+ * be quoted as characters like `:` and `,` are known to cause issues
  767
+ * in address header fields.
  768
+ *
765 769
  * @param array $address
766 770
  * @return array
767 771
  */
@@ -771,10 +775,11 @@ protected function _formatAddress($address) {
771 775
 			if ($email === $alias) {
772 776
 				$return[] = $email;
773 777
 			} else {
774  
-				if (strpos($alias, ',') !== false) {
775  
-					$alias = '"' . $alias . '"';
  778
+				$encoded = $this->_encode($alias);
  779
+				if ($encoded === $alias && preg_match('/[^a-z0-9 ]/i', $encoded)) {
  780
+					$encoded = '"' . str_replace('"', '\"', $encoded) . '"';
776 781
 				}
777  
-				$return[] = sprintf('%s <%s>', $this->_encode($alias), $email);
  782
+				$return[] = sprintf('%s <%s>', $encoded, $email);
778 783
 			}
779 784
 		}
780 785
 		return $return;
23  lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -179,6 +179,25 @@ public function testFrom() {
179 179
 	}
180 180
 
181 181
 /**
  182
+ * Test that from addresses using colons work.
  183
+ *
  184
+ * @return void
  185
+ */
  186
+	public function testFromWithColonsAndQuotes() {
  187
+		$address = array(
  188
+			'info@example.com' => '70:20:00 " Forum'
  189
+		);
  190
+		$this->CakeEmail->from($address);
  191
+		$this->assertEquals($address, $this->CakeEmail->from());
  192
+		$this->CakeEmail->to('info@example.com')
  193
+			->subject('Test email')
  194
+			->transport('Debug');
  195
+
  196
+		$result = $this->CakeEmail->send();
  197
+		$this->assertContains('From: "70:20:00 \" Forum" <info@example.com>', $result['headers']);
  198
+	}
  199
+
  200
+/**
182 201
  * testSender method
183 202
  *
184 203
  * @return void
@@ -371,6 +390,10 @@ public function testFormatAddress() {
371 390
 		$expected = array('"Last, First" <me@example.com>');
372 391
 		$this->assertSame($expected, $result);
373 392
 
  393
+		$result = $this->CakeEmail->formatAddress(array('me@example.com' => '"Last" First'));
  394
+		$expected = array('"\"Last\" First" <me@example.com>');
  395
+		$this->assertSame($expected, $result);
  396
+
374 397
 		$result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last First'));
375 398
 		$expected = array('Last First <me@example.com>');
376 399
 		$this->assertSame($expected, $result);

0 notes on commit ba4afcc

Please sign in to comment.
Something went wrong with that request. Please try again.