From e9d194822d99ef2031cfe59451f9f9b156015522 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 12 Sep 2010 11:48:09 -0400 Subject: [PATCH] Adding more tests for SMTP address formatting. Fixes #1100 --- cake/libs/controller/components/email.php | 11 ++++++++--- .../cases/libs/controller/components/email.test.php | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index 53d642b0f..399600402 100755 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -743,9 +743,14 @@ class EmailComponent extends Object{ * @access private */ function _formatAddress($string, $smtp = false) { - $hasAlias = preg_match('/(.+)\s<(.+)>/', $string, $matches); - if ($hasAlias) { - return $this->_strip($matches[1] . ' <' . $matches[2] . '>'); + $hasAlias = preg_match('/((.*)\s)?<(.+)>/', $string, $matches); + if ($smtp && $hasAlias) { + return $this->_strip(' <' . $matches[3] . '>'); + } elseif ($smtp) { + return $this->_strip(' <' . $string . '>'); + } + if ($hasAlias && !empty($matches[2])) { + return $this->_strip($matches[2] . ' <' . $matches[3] . '>'); } return $this->_strip($string); } diff --git a/cake/tests/cases/libs/controller/components/email.test.php b/cake/tests/cases/libs/controller/components/email.test.php index 4684d88df..666dfb381 100755 --- a/cake/tests/cases/libs/controller/components/email.test.php +++ b/cake/tests/cases/libs/controller/components/email.test.php @@ -1178,5 +1178,14 @@ HTMLBLOC; $result = $this->Controller->EmailTest->formatAddress(''); $this->assertEqual($result, ''); + + $result = $this->Controller->EmailTest->formatAddress('email@example.com', true); + $this->assertEqual($result, ' '); + + $result = $this->Controller->EmailTest->formatAddress('', true); + $this->assertEqual($result, ' '); + + $result = $this->Controller->EmailTest->formatAddress('alias name ', true); + $this->assertEqual($result, ' '); } }