Skip to content

Instantly share code, notes, and snippets.

@nojimage
Last active December 17, 2015 20:39
Show Gist options
  • Save nojimage/5669383 to your computer and use it in GitHub Desktop.
Save nojimage/5669383 to your computer and use it in GitHub Desktop.
腐ったEmailアドレスに対応するためのCakeEmail Hack.
<?php
App::uses('CakeEmail', 'Network/Email');
class AppCakeEmail extends CakeEmail {
protected $_emailPattern = null;
/**
*
* @param string $regex
* @return string|AppCakeEmail
*/
public function emailPattern($regex = null) {
if (is_null($regex)) {
return $this->_emailPattern;
}
$this->_emailPattern = $regex;
return $this;
}
/**
* Set email
*
* @param string $varName
* @param string|array $email
* @param string $name
* @return CakeEmail $this
* @throws SocketException
*/
protected function _setEmail($varName, $email, $name) {
if (!is_array($email)) {
if (!Validation::email($email, false, $this->_emailPattern)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
if ($name === null) {
$name = $email;
}
$this->{$varName} = array($email => $name);
return $this;
}
$list = array();
foreach ($email as $key => $value) {
if (is_int($key)) {
$key = $value;
}
if (!Validation::email($key, false, $this->_emailPattern)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
}
$list[$key] = $value;
}
$this->{$varName} = $list;
return $this;
}
/**
* Add email
*
* @param string $varName
* @param string|array $email
* @param string $name
* @return CakeEmail $this
* @throws SocketException
*/
protected function _addEmail($varName, $email, $name) {
if (!is_array($email)) {
if (!Validation::email($email, false, $this->_emailPattern)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
if ($name === null) {
$name = $email;
}
$this->{$varName}[$email] = $name;
return $this;
}
$list = array();
foreach ($email as $key => $value) {
if (is_int($key)) {
$key = $value;
}
if (!Validation::email($key, false, $this->_emailPattern)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
}
$list[$key] = $value;
}
$this->{$varName} = array_merge($this->{$varName}, $list);
return $this;
}
/**
* Reset all EmailComponent internal variables to be able to send out a new email.
*
* @return CakeEmail $this
*/
public function reset() {
$this->_emailPattern = null;
return parent::reset();
}
}
<?php
App::uses('AppCakeEmail', 'Lib');
/*
* EmailConfig class
*
*/
class AppEmailConfig {
/**
* test config
*
* @var string
*/
public $test = array(
'from' => array('[email protected]' => 'My website'),
'to' => array('[email protected]' => 'Testname'),
'subject' => 'Test mail subject',
'transport' => 'Debug',
'theme' => 'TestTheme',
'helpers' => array('Html', 'Form'),
);
}
/**
* CakeEmailTest class
*
* @param AppCakeEmail $CakeEmail
*/
class AppCakeEmailTest extends CakeTestCase {
/**
* setUp
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->CakeEmail = new AppCakeEmail();
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
App::build();
}
public function testEmailPattern() {
$regex = '/.+@.+\..+/i';
$this->assertNull($this->CakeEmail->emailPattern());
$this->assertSame($regex, $this->CakeEmail->emailPattern($regex)->emailPattern());
}
public function testCustomEmailValidation() {
$regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i';
$email = '[email protected]';
$additionalEmail = '[email protected]';
$emails = array('[email protected]', '[email protected]');
$additionalEmails = array(
'[email protected]',
'[email protected]',
);
$this->CakeEmail->emailPattern($regex)->to($email);
$this->assertSame(array(
'[email protected]' => '[email protected]',
), $this->CakeEmail->to());
$this->CakeEmail->addTo($additionalEmail);
$this->assertSame(array(
'[email protected]' => '[email protected]',
'[email protected]' => '[email protected]',
), $this->CakeEmail->to());
$this->CakeEmail->emailPattern($regex)->to($emails);
$this->assertSame(array(
'[email protected]' => '[email protected]',
'[email protected]' => '[email protected]',
), $this->CakeEmail->to());
$this->CakeEmail->addTo($additionalEmails);
$this->assertSame(array(
'[email protected]' => '[email protected]',
'[email protected]' => '[email protected]',
'[email protected]' => '[email protected]',
'[email protected]' => '[email protected]',
), $this->CakeEmail->to());
}
public function testPassedEmail() {
$regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i';
$check = "yuri-rico/[email protected]";
$this->CakeEmail->to("include-dash/[email protected]");
$this->CakeEmail->emailPattern($regex)->to($check);
}
}
@nojimage
Copy link
Author

fu_k d_como!

コアに取り込んで欲しいけど、なんで腐ったメールアドレスを考慮する必要があるのか説明してチケット書くの面倒 orz

@nojimage
Copy link
Author

PRが通りましたので、CakePHP 2.4.0以降ではハックの必要が無くなります。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment