Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14133202
D19026.id45622.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D19026.id45622.diff
View Options
diff --git a/src/parser/PhutilEmailAddress.php b/src/parser/PhutilEmailAddress.php
--- a/src/parser/PhutilEmailAddress.php
+++ b/src/parser/PhutilEmailAddress.php
@@ -12,7 +12,7 @@
private $localPart;
private $domainName;
- public function __construct($email_address) {
+ public function __construct($email_address = null) {
$email_address = trim($email_address);
$matches = null;
@@ -41,8 +41,9 @@
public function __toString() {
$address = $this->getAddress();
- if ($this->displayName) {
- return $this->displayName.' <'.$address.'>';
+ if (strlen($this->displayName)) {
+ $display_name = $this->encodeDisplayName($this->displayName);
+ return $display_name.' <'.$address.'>';
} else {
return $address;
}
@@ -75,12 +76,39 @@
return $this->domainName;
}
+ public function setAddress($address) {
+ $parts = explode('@', $address, 2);
+
+ $this->localPart = $parts[0];
+ if (isset($parts[1])) {
+ $this->domainName = $parts[1];
+ }
+
+ return $this;
+ }
+
public function getAddress() {
$address = $this->localPart;
- if ($this->domainName) {
+ if (strlen($this->domainName)) {
$address .= '@'.$this->domainName;
}
return $address;
}
+ private function encodeDisplayName($name) {
+ // NOTE: This is a reasonable effort based on a cursory reading of
+ // RFC2822, but may be significantly misguided.
+
+ // Newlines are not permitted, even when escaped. Discard them.
+ $name = preg_replace("/\s*[\r\n]+\s*/", ' ', $name);
+
+ // Escape double quotes and backslashes.
+ $name = addcslashes($name, '\\"');
+
+ // Quote the string.
+ $name = '"'.$name.'"';
+
+ return $name;
+ }
+
}
diff --git a/src/parser/__tests__/PhutilEmailAddressTestCase.php b/src/parser/__tests__/PhutilEmailAddressTestCase.php
--- a/src/parser/__tests__/PhutilEmailAddressTestCase.php
+++ b/src/parser/__tests__/PhutilEmailAddressTestCase.php
@@ -91,4 +91,40 @@
$email->getAddress());
}
+ public function testEmailEncoding() {
+ $cases = array(
+ array(
+ 'Tangerine Q. Hawthorne',
+ 'thawthorne@blackspire.bunker',
+ '"Tangerine Q. Hawthorne" <thawthorne@blackspire.bunker>',
+ ),
+ array(
+ 'Hector "\\" Backslash',
+ 'hector@backslash',
+ '"Hector \\"\\\\\\" Backslash" <hector@backslash>',
+ ),
+ array(
+ 'My Middle Name "<name@domain>" Is My Email',
+ 'name@domain',
+ '"My Middle Name \\"<name@domain>\\" Is My Email" <name@domain>',
+ ),
+ array(
+ "My Legal Name\nContains A Newline",
+ 'newline@example',
+ '"My Legal Name Contains A Newline" <newline@example>',
+ ),
+ );
+
+ foreach ($cases as $case) {
+ list($name, $address, $expect) = $case;
+ $actual = (string)id(new PhutilEmailAddress())
+ ->setDisplayName($name)
+ ->setAddress($address);
+ $this->assertEqual(
+ $expect,
+ $actual,
+ pht('Email: %s + %s -> %s', $name, $address, $expect));
+ }
+ }
+
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Dec 3, 3:43 AM (20 h, 3 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6817932
Default Alt Text
D19026.id45622.diff (3 KB)
Attached To
Mode
D19026: Let PhutilEmailAddress encode addresses with complex display names somewhat properly
Attached
Detach File
Event Timeline
Log In to Comment