環境
- PHP7.1
- PHPMailer 6.0.1
おきた問題
運用しているサービスのメール機能にPHPMailer
を使っていたが、あるときにメール送信が出来ない事象が発生した。logを確認したところ、「hoge...@docomo.ne.jp」のように「...」ドットが連続しているメールアドレスがフォームに入力された痕跡があった。
ちょっと調べたところ「...」ドットが複数あるのは、RFCの規格に沿わないとのこと(こちらを参照)。
ユーザー側に問題があったのだけど、なんとか対応出来ないかと思って試行錯誤した。
以下のURLを参考にして解決出来なかった方は同様に解決できるのではないかと思う。
解決方法
src/PHPMailer.php
を書き換える。
composer経由だとvendor/phpmailer/phpmailer/src/PHPMailer.php
のような感じだった。
以下の処理を書き換えていく。
自分の環境では1183行目らへんでした。
public static function validateAddress($address, $patternselect = null){
//処理
}
自分の環境下では、以上のようになっていた。それを以下のようにする。
public static function validateAddress($address, $patternselect = 'noregex'){
//処理
}
$patternselect = null
となっていたところを$patternselect = 'noregex'
とする。
さらに以下を追記する。
public static function validateAddress($address, $patternselect = 'noregex')
{
if (null === $patternselect) {
$patternselect = static::$validator;
}
if (is_callable($patternselect)) {
return call_user_func($patternselect, $address);
}
//Reject line breaks in addresses; it's valid RFC5322, but not RFC5321
if (strpos($address, "\n") !== false or strpos($address, "\r") !== false) {
return false;
}
switch ($patternselect) {
case 'pcre': //Kept for BC
case 'pcre8':
/*
* A more complex and more permissive version of the RFC5322 regex on which FILTER_VALIDATE_EMAIL
* is based.
* In addition to the addresses allowed by filter_var, also permits:
* * dotless domains: `a@b`
* * comments: `1234 @ local(blah) .machine .example`
* * quoted elements: `'"test blah"@example.org'`
* * numeric TLDs: `a@b.123`
* * unbracketed IPv4 literals: `a@192.168.0.1`
* * IPv6 literals: 'first.last@[IPv6:a1::]'
* Not all of these will necessarily work for sending!
*
* @see http://squiloople.com/2009/12/20/email-address-validation/
* @copyright 2009-2010 Michael Rushton
* Feel free to use and redistribute this code. But please keep this copyright notice.
*/
return (bool) preg_match(
'/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
'((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
'(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
'([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
'(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
'(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
'|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
'|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
'|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
$address
);
case 'html5':
/*
* This is the pattern used in the HTML5 spec for validation of 'email' type form input elements.
*
* @see http://www.whatwg.org/specs/web-apps/current-work/#e-mail-state-(type=email)
*/
return (bool) preg_match(
'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}' .
'[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD',
$address
);
// ここから
case 'noregex':
return (strlen($address) >= 3
and strpos($address, '@') >= 1
and strpos($address, '@') != strlen($address) - 1);
// ここまで追記
case 'php':
default:
return (bool) filter_var($address, FILTER_VALIDATE_EMAIL);
}
}
public static function validateAddress
のswitch文にccase 'noregex'
を追記した。
以上でRFCの規格に沿わないメールアドレスでもメール送信が可能になります。