diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 174301415..ee4da73d9 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -589,7 +589,7 @@ class CakeEmail { */ protected function _setEmail($varName, $email, $name) { if (!is_array($email)) { - $this->_validateEmail($email); + $this->_validateEmail($email, $varName); if ($name === null) { $name = $email; } @@ -601,7 +601,7 @@ class CakeEmail { if (is_int($key)) { $key = $value; } - $this->_validateEmail($key); + $this->_validateEmail($key, $varName); $list[$key] = $value; } $this->{$varName} = $list; @@ -611,11 +611,12 @@ class CakeEmail { /** * Validate email address * - * @param string $email Email + * @param string $email Email address to validate + * @param string $context Which property was set * @return void * @throws SocketException If email address does not validate */ - protected function _validateEmail($email) { + protected function _validateEmail($email, $context) { if ($this->_emailPattern === null) { if (filter_var($email, FILTER_VALIDATE_EMAIL)) { return; @@ -623,7 +624,10 @@ class CakeEmail { } elseif (preg_match($this->_emailPattern, $email)) { return; } - throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email)); + if ($email == '') { + throw new SocketException(__d('cake_dev', 'The email set for "%s" is empty.', $context)); + } + throw new SocketException(__d('cake_dev', 'Invalid email set for "%s". You passed "%s".', $context, $email)); } /** @@ -659,7 +663,7 @@ class CakeEmail { */ protected function _addEmail($varName, $email, $name) { if (!is_array($email)) { - $this->_validateEmail($email); + $this->_validateEmail($email, $varName); if ($name === null) { $name = $email; } @@ -671,7 +675,7 @@ class CakeEmail { if (is_int($key)) { $key = $value; } - $this->_validateEmail($key); + $this->_validateEmail($key, $varName); $list[$key] = $value; } $this->{$varName} = array_merge($this->{$varName}, $list); diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index a0d15bec8..ca7ae2a38 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -334,23 +334,34 @@ class CakeEmailTest extends CakeTestCase { /** * testBuildInvalidData * - * @dataProvider invalidEmails * @expectedException SocketException + * @expectedExceptionMessage The email set for "_to" is empty. * @return void */ - public function testInvalidEmail($value) { - $this->CakeEmail->to($value); + public function testInvalidEmail() { + $this->CakeEmail->to(''); } /** * testBuildInvalidData * - * @dataProvider invalidEmails * @expectedException SocketException + * @expectedExceptionMessage Invalid email set for "_from". You passed "cake.@" * @return void */ - public function testInvalidEmailAdd($value) { - $this->CakeEmail->addTo($value); + public function testInvalidFrom() { + $this->CakeEmail->from('cake.@'); + } + +/** + * testBuildInvalidData + * + * @expectedException SocketException + * @expectedExceptionMessage Invalid email set for "_to". You passed "1" + * @return void + */ + public function testInvalidEmailAdd() { + $this->CakeEmail->addTo('1'); } /** @@ -423,7 +434,7 @@ class CakeEmailTest extends CakeTestCase { * @return void * * @expectedException SocketException - * @expectedExceptionMessage Invalid email: "fail.@example.com" + * @expectedExceptionMessage Invalid email set for "_to". You passed "fail.@example.com" */ public function testUnsetEmailPattern() { $email = new CakeEmail();