Created
April 28, 2015 07:56
-
-
Save basuke/16ad5c07fba3f029e729 to your computer and use it in GitHub Desktop.
How to use RFC-invalid email addresses used by Japanese Cell carriers, DoCoMo and EZWeb using SwiftMailer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Swift::init(function() { | |
Swift_DependencyContainer::getInstance() | |
->register('mime.grammar') | |
->asSharedInstanceOf('InvalidMimeGrammar'); | |
// ... other settings | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class InvalidMimeGrammar extends \Swift_Mime_Grammar { | |
/** | |
* Get the grammar defined for $name token. | |
* @param string $name exactly as written in the RFC | |
* @return string | |
*/ | |
public function getDefinition($name) | |
{ | |
switch ($name) { | |
case 'addr-spec': | |
$spec = parent::getDefinition($name); | |
$fuzzy_domains = array( | |
'docomo\.ne\.jp', | |
'ezweb\.ne\.jp', | |
); | |
$atext = $this->getDefinition('atext'); | |
$CFWS = $this->getDefinition('CFWS'); | |
$invalid_dot_atom_text = '(?:\.*'. $atext. '+'. '(\.+'. $atext. '+)*\.*)'; | |
$invalid_dot_atom = '(?:'. $CFWS. '?'. $invalid_dot_atom_text. '+'. $CFWS. '?)'; | |
$quoted_string = $this->getDefinition('quoted-string'); | |
$local_part = '(?:'. $invalid_dot_atom. '|'. $quoted_string. ')'; | |
$domain_part = '(?:'. implode('|', $fuzzy_domains). ')'; | |
$spec = '(?:'. $spec. '|'. $local_part. '@'. $domain_part. ')'; | |
return $spec; | |
default: | |
return parent::getDefinition($name); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class InvalidMimeGrammarTest extends \PHPUnit_Framework_TestCase { | |
/** | |
* @dataProvider invalidJapaneseKetaiAddresses | |
*/ | |
public function testMimeGrammar($address, $expected) | |
{ | |
$grammar = \Swift_DependencyContainer::getInstance()->lookup('mime.grammar'); | |
$result = preg_match('/^'.$grammar->getDefinition('addr-spec').'$/D', $address); | |
$this->assertEquals($expected, $result); | |
} | |
public function invalidJapaneseKetaiAddresses() { | |
return array( | |
array('[email protected]', true), | |
array('[email protected]', true), | |
array('[email protected]', false), | |
array('[email protected]', true), | |
array('[email protected]', false), | |
array('[email protected]', true), | |
array('[email protected]', false), | |
array('[email protected]', true), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment