Skip to content

Instantly share code, notes, and snippets.

@basuke
Created April 28, 2015 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basuke/16ad5c07fba3f029e729 to your computer and use it in GitHub Desktop.
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
Swift::init(function() {
Swift_DependencyContainer::getInstance()
->register('mime.grammar')
->asSharedInstanceOf('InvalidMimeGrammar');
// ... other settings
});
<?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);
}
}
}
<?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('basuke@mac.com', true),
array('basuke...omg@docomo.ne.jp', true),
array('basuke...omg@not-docomo.jp', false),
array('bas..uke.omg.@docomo.ne.jp', true),
array('basuke.@not-docomo.jp', false),
array('.basuke@docomo.ne.jp', true),
array('.basuke@not-docomo.jp', false),
array('bas..uke.omg.@ezweb.ne.jp', true),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment