From 175280ad79d8204425da8e6587c2bf855261545f Mon Sep 17 00:00:00 2001 From: nojimage Date: Sat, 29 Jun 2013 02:55:16 +0900 Subject: [PATCH] CakeEmail be able to setting custom email validation rule --- lib/Cake/Network/Email/CakeEmail.php | 8 ++-- .../Test/Case/Network/Email/CakeEmailTest.php | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 0bcbcd165..1c6f5baba 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -554,7 +554,7 @@ class CakeEmail { */ protected function _setEmail($varName, $email, $name) { if (!is_array($email)) { - if (!Validation::email($email)) { + if (!Validation::email($email, false, $this->_emailRegex)) { throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email)); } if ($name === null) { @@ -568,7 +568,7 @@ class CakeEmail { if (is_int($key)) { $key = $value; } - if (!Validation::email($key)) { + if (!Validation::email($key, false, $this->_emailRegex)) { throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key)); } $list[$key] = $value; @@ -608,7 +608,7 @@ class CakeEmail { */ protected function _addEmail($varName, $email, $name) { if (!is_array($email)) { - if (!Validation::email($email)) { + if (!Validation::email($email, false, $this->_emailRegex)) { throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email)); } if ($name === null) { @@ -622,7 +622,7 @@ class CakeEmail { if (is_int($key)) { $key = $value; } - if (!Validation::email($key)) { + if (!Validation::email($key, false, $this->_emailRegex)) { throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key)); } $list[$key] = $value; diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index 1020ff855..ad9634725 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -286,6 +286,47 @@ class CakeEmailTest extends CakeTestCase { $this->assertSame($regex, $email->emailRegex()); } +/** + * Tests that it is possible set custom email validation + */ + public function testCustomEmailValidation() { + $regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i'; + + $this->CakeEmail->emailRegex($regex)->to('pass.@example.com'); + $this->assertSame(array( + 'pass.@example.com' => 'pass.@example.com', + ), $this->CakeEmail->to()); + + $this->CakeEmail->addTo('pass..old.docomo@example.com'); + $this->assertSame(array( + 'pass.@example.com' => 'pass.@example.com', + 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com', + ), $this->CakeEmail->to()); + + $this->CakeEmail->reset(); + $emails = array( + 'pass.@example.com', + 'pass..old.docomo@example.com' + ); + $additionalEmails = array( + '.extend.@example.com', + '.docomo@example.com' + ); + $this->CakeEmail->emailRegex($regex)->to($emails); + $this->assertSame(array( + 'pass.@example.com' => 'pass.@example.com', + 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com', + ), $this->CakeEmail->to()); + + $this->CakeEmail->addTo($additionalEmails); + $this->assertSame(array( + 'pass.@example.com' => 'pass.@example.com', + 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com', + '.extend.@example.com' => '.extend.@example.com', + '.docomo@example.com' => '.docomo@example.com', + ), $this->CakeEmail->to()); + } + /** * testFormatAddress method *