Simplify validation options, default to filter_var, see #804
This commit is contained in:
parent
1e53c07286
commit
6931f31028
|
|
@ -84,8 +84,10 @@ class PHPMailer
|
|||
public $FromName = 'Root User';
|
||||
|
||||
/**
|
||||
* The Sender email (Return-Path) of the message.
|
||||
* If not empty, will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
|
||||
* The envelope sender of the message.
|
||||
* This will usually be turned into a Return-Path header by the receiver,
|
||||
* and is the address that bounces will be sent to.
|
||||
* If not empty, will be passed via `-f` to sendmail or as the 'MAIL FROM' value over SMTP.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
|
|
@ -119,7 +121,7 @@ class PHPMailer
|
|||
/**
|
||||
* An iCal message part body.
|
||||
* Only supported in simple alt or alt_inline message types
|
||||
* To generate iCal events, use the bundled extras/EasyPeasyICS.php class or iCalcreator
|
||||
* To generate iCal event structures, use classes like EasyPeasyICS or iCalcreator
|
||||
*
|
||||
* @see http://sprain.ch/blog/downloads/php-class-easypeasyics-create-ical-files-with-php/
|
||||
* @see http://kigkonsult.se/iCalcreator/
|
||||
|
|
@ -467,11 +469,12 @@ class PHPMailer
|
|||
/**
|
||||
* Which validator to use by default when validating email addresses.
|
||||
* May be a callable to inject your own validator, but there are several built-in validators.
|
||||
* The default validator uses PHP's FILTER_VALIDATE_EMAIL filter_var option.
|
||||
*
|
||||
* @see PHPMailer::validateAddress()
|
||||
* @var string|callable
|
||||
*/
|
||||
public static $validator = 'auto';
|
||||
public static $validator = 'php';
|
||||
|
||||
/**
|
||||
* An instance of the SMTP sender class.
|
||||
|
|
@ -1117,26 +1120,20 @@ class PHPMailer
|
|||
if (strpos($address, "\n") !== false or strpos($address, "\r") !== false) {
|
||||
return false;
|
||||
}
|
||||
if (!$patternselect or 'auto' == $patternselect) {
|
||||
//Check this constant first so it works when extension_loaded() is disabled by safe mode
|
||||
if (defined('PCRE_VERSION')) {
|
||||
//This pattern can get stuck in a recursive loop in PCRE <= 8.0.2
|
||||
if (version_compare(PCRE_VERSION, '8.0.3') >= 0) {
|
||||
$patternselect = 'pcre8';
|
||||
} else {
|
||||
$patternselect = 'pcre';
|
||||
}
|
||||
} elseif (function_exists('extension_loaded') and extension_loaded('pcre')) {
|
||||
//Fall back to older PCRE
|
||||
$patternselect = 'pcre';
|
||||
} else {
|
||||
$patternselect = 'php';
|
||||
}
|
||||
}
|
||||
switch ($patternselect) {
|
||||
case 'pcre': //Kept for BC
|
||||
case 'pcre8':
|
||||
/**
|
||||
* Uses the same RFC5322 regex on which FILTER_VALIDATE_EMAIL is based, but allows dotless domains.
|
||||
* 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
|
||||
|
|
@ -1154,21 +1151,6 @@ class PHPMailer
|
|||
'|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
|
||||
$address
|
||||
);
|
||||
case 'pcre':
|
||||
//An older regex that doesn't need a recent PCRE
|
||||
return (boolean)preg_match(
|
||||
'/^(?!(?>"?(?>\\\[ -~]|[^"])"?){255,})(?!(?>"?(?>\\\[ -~]|[^"])"?){65,}@)(?>' .
|
||||
'[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")' .
|
||||
'(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*' .
|
||||
'@(?>(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64,})' .
|
||||
'(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)){0,126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1,4})(?>:' .
|
||||
'[a-f0-9]{1,4}){7}|(?!(?:.*[a-f0-9][:\]]){8,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?' .
|
||||
'::(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?))|(?>(?>IPv6:(?>[a-f0-9]{1,4}(?>:' .
|
||||
'[a-f0-9]{1,4}){5}:|(?!(?:.*[a-f0-9]:){6,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4})?' .
|
||||
'::(?>(?:[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
|
||||
'|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}))\])$/isD',
|
||||
$address
|
||||
);
|
||||
case 'html5':
|
||||
/**
|
||||
* This is the pattern used in the HTML5 spec for validation of 'email' type form input elements.
|
||||
|
|
@ -1180,12 +1162,7 @@ class PHPMailer
|
|||
'[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD',
|
||||
$address
|
||||
);
|
||||
case 'noregex':
|
||||
//No PCRE! Do something _very_ approximate!
|
||||
//Check the address is 3 chars or longer and contains an @ that's not the first or last char
|
||||
return (strlen($address) >= 3
|
||||
and strpos($address, '@') >= 1
|
||||
and strpos($address, '@') != strlen($address) - 1);
|
||||
case 'noregex': //Kept for BC
|
||||
case 'php':
|
||||
default:
|
||||
return (boolean)filter_var($address, FILTER_VALIDATE_EMAIL);
|
||||
|
|
|
|||
|
|
@ -328,116 +328,119 @@ class PHPMailerTest extends \PHPUnit_Framework_TestCase
|
|||
public function testValidate()
|
||||
{
|
||||
$validaddresses = [
|
||||
'first@iana.org',
|
||||
'first.last@iana.org',
|
||||
'1234567890123456789012345678901234567890123456789012345678901234@iana.org',
|
||||
'"first\"last"@iana.org',
|
||||
'"first@last"@iana.org',
|
||||
'"first\last"@iana.org',
|
||||
'first@example.org',
|
||||
'first.last@example.org',
|
||||
'1234567890123456789012345678901234567890123456789012345678901234@example.org',
|
||||
'"first\"last"@example.org',
|
||||
'"first@last"@example.org',
|
||||
'"first\last"@example.org',
|
||||
'first.last@[12.34.56.78]',
|
||||
'first.last@[IPv6:::12.34.56.78]',
|
||||
'first.last@[IPv6:1111:2222:3333::4444:12.34.56.78]',
|
||||
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.56.78]',
|
||||
'first.last@[IPv6:::1111:2222:3333:4444:5555:6666]',
|
||||
'first.last@[IPv6:1111:2222:3333::4444:5555:6666]',
|
||||
'first.last@[IPv6:1111:2222:3333:4444:5555:6666::]',
|
||||
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888]',
|
||||
'first.last@x23456789012345678901234567890123456789012345678901234567890123.iana.org',
|
||||
'first.last@3com.com',
|
||||
'first.last@123.iana.org',
|
||||
'"first\last"@iana.org',
|
||||
'first.last@[IPv6:1111:2222:3333::4444:5555:12.34.56.78]',
|
||||
'first.last@[IPv6:1111:2222:3333::4444:5555:6666:7777]',
|
||||
'first.last@example.123',
|
||||
'first.last@com',
|
||||
'"Abc\@def"@iana.org',
|
||||
'"Fred\ Bloggs"@iana.org',
|
||||
'"Joe.\Blow"@iana.org',
|
||||
'"Abc@def"@iana.org',
|
||||
'user+mailbox@iana.org',
|
||||
'customer/department=shipping@iana.org',
|
||||
'$A12345@iana.org',
|
||||
'!def!xyz%abc@iana.org',
|
||||
'_somename@iana.org',
|
||||
'dclo@us.ibm.com',
|
||||
'peter.piper@iana.org',
|
||||
'"Doug \"Ace\" L."@iana.org',
|
||||
'test@iana.org',
|
||||
'TEST@iana.org',
|
||||
'1234567890@iana.org',
|
||||
'test+test@iana.org',
|
||||
'test-test@iana.org',
|
||||
't*est@iana.org',
|
||||
'+1~1+@iana.org',
|
||||
'{_test_}@iana.org',
|
||||
'"[[ test ]]"@iana.org',
|
||||
'test.test@iana.org',
|
||||
'"test.test"@iana.org',
|
||||
'test."test"@iana.org',
|
||||
'"test@test"@iana.org',
|
||||
'first.last@x23456789012345678901234567890123456789012345678901234567890123.example.org',
|
||||
'first.last@123.example.org',
|
||||
'"first\last"@example.org',
|
||||
'"Abc\@def"@example.org',
|
||||
'"Fred\ Bloggs"@example.org',
|
||||
'"Joe.\Blow"@example.org',
|
||||
'"Abc@def"@example.org',
|
||||
'user+mailbox@example.org',
|
||||
'customer/department=shipping@example.org',
|
||||
'$A12345@example.org',
|
||||
'!def!xyz%abc@example.org',
|
||||
'_somename@example.org',
|
||||
'dclo@us.example.com',
|
||||
'peter.piper@example.org',
|
||||
'test@example.org',
|
||||
'TEST@example.org',
|
||||
'1234567890@example.org',
|
||||
'test+test@example.org',
|
||||
'test-test@example.org',
|
||||
't*est@example.org',
|
||||
'+1~1+@example.org',
|
||||
'{_test_}@example.org',
|
||||
'test.test@example.org',
|
||||
'"test.test"@example.org',
|
||||
'test."test"@example.org',
|
||||
'"test@test"@example.org',
|
||||
'test@123.123.123.x123',
|
||||
'test@123.123.123.123',
|
||||
'test@[123.123.123.123]',
|
||||
'test@example.iana.org',
|
||||
'test@example.example.iana.org',
|
||||
'"test\test"@iana.org',
|
||||
'test@example',
|
||||
'"test\blah"@iana.org',
|
||||
'"test\blah"@iana.org',
|
||||
'"test\"blah"@iana.org',
|
||||
'customer/department@iana.org',
|
||||
'_Yosemite.Sam@iana.org',
|
||||
'~@iana.org',
|
||||
'"Austin@Powers"@iana.org',
|
||||
'Ima.Fool@iana.org',
|
||||
'"Ima.Fool"@iana.org',
|
||||
'"Ima Fool"@iana.org',
|
||||
'"first"."last"@iana.org',
|
||||
'"first".middle."last"@iana.org',
|
||||
'"first".last@iana.org',
|
||||
'first."last"@iana.org',
|
||||
'"first"."middle"."last"@iana.org',
|
||||
'"first.middle"."last"@iana.org',
|
||||
'"first.middle.last"@iana.org',
|
||||
'"first..last"@iana.org',
|
||||
'"first\"last"@iana.org',
|
||||
'first."mid\dle"."last"@iana.org',
|
||||
'"test blah"@iana.org',
|
||||
'(foo)cal(bar)@(baz)iamcal.com(quux)',
|
||||
'cal@iamcal(woo).(yay)com',
|
||||
'cal(woo(yay)hoopla)@iamcal.com',
|
||||
'cal(foo\@bar)@iamcal.com',
|
||||
'cal(foo\)bar)@iamcal.com',
|
||||
'first().last@iana.org',
|
||||
'test@example.example.org',
|
||||
'test@example.example.example.org',
|
||||
'"test\test"@example.org',
|
||||
'"test\blah"@example.org',
|
||||
'"test\blah"@example.org',
|
||||
'"test\"blah"@example.org',
|
||||
'customer/department@example.org',
|
||||
'_Yosemite.Sam@example.org',
|
||||
'~@example.org',
|
||||
'"Austin@Powers"@example.org',
|
||||
'Ima.Fool@example.org',
|
||||
'"Ima.Fool"@example.org',
|
||||
'"first"."last"@example.org',
|
||||
'"first".middle."last"@example.org',
|
||||
'"first".last@example.org',
|
||||
'first."last"@example.org',
|
||||
'"first"."middle"."last"@example.org',
|
||||
'"first.middle"."last"@example.org',
|
||||
'"first.middle.last"@example.org',
|
||||
'"first..last"@example.org',
|
||||
'"first\"last"@example.org',
|
||||
'first."mid\dle"."last"@example.org',
|
||||
'name.lastname@example.com',
|
||||
'a@example.com',
|
||||
'aaa@[123.123.123.123]',
|
||||
'a-b@example.com',
|
||||
'+@b.c',
|
||||
'+@b.com',
|
||||
'a@b.co-foo.uk',
|
||||
'valid@about.museum',
|
||||
'shaitan@my-domain.thisisminekthx',
|
||||
'"Joe\Blow"@example.org',
|
||||
'user%uucp!path@example.edu',
|
||||
'cdburgess+!#$%&\'*-/=?+_{}|~test@example.com',
|
||||
'test@test.com',
|
||||
'test@xn--example.com',
|
||||
'test@example.com'
|
||||
];
|
||||
//These are invalid according to PHP's filter_var
|
||||
//which doesn't allow dotless domains, numeric TLDs or unbracketed IPv4 literals
|
||||
$invalidphp = [
|
||||
'a@b',
|
||||
'a@bar',
|
||||
'first.last@com',
|
||||
'test@123.123.123.123',
|
||||
'foobar@192.168.0.1',
|
||||
'first.last@example.123',
|
||||
];
|
||||
//Valid RFC 5322 addresses using quoting and comments
|
||||
//Note that these are *not* all valid for RFC5321
|
||||
$validqandc = [
|
||||
'HM2Kinsists@(that comments are allowed)this.is.ok',
|
||||
'"Doug \"Ace\" L."@example.org',
|
||||
'"[[ test ]]"@example.org',
|
||||
'"Ima Fool"@example.org',
|
||||
'"test blah"@example.org',
|
||||
'(foo)cal(bar)@(baz)example.com(quux)',
|
||||
'cal@example(woo).(yay)com',
|
||||
'cal(woo(yay)hoopla)@example.com',
|
||||
'cal(foo\@bar)@example.com',
|
||||
'cal(foo\)bar)@example.com',
|
||||
'first().last@example.org',
|
||||
'pete(his account)@silly.test(his host)',
|
||||
'c@(Chris\'s host.)public.example',
|
||||
'jdoe@machine(comment). example',
|
||||
'1234 @ local(blah) .machine .example',
|
||||
'first(abc.def).last@iana.org',
|
||||
'first(a"bc.def).last@iana.org',
|
||||
'first.(")middle.last(")@iana.org',
|
||||
'first(abc\(def)@iana.org',
|
||||
'first(abc.def).last@example.org',
|
||||
'first(a"bc.def).last@example.org',
|
||||
'first.(")middle.last(")@example.org',
|
||||
'first(abc\(def)@example.org',
|
||||
'first.last@x(1234567890123456789012345678901234567890123456789012345678901234567890).com',
|
||||
'a(a(b(c)d(e(f))g)h(i)j)@iana.org',
|
||||
'name.lastname@domain.com',
|
||||
'a@b',
|
||||
'a@bar.com',
|
||||
'aaa@[123.123.123.123]',
|
||||
'a@bar',
|
||||
'a-b@bar.com',
|
||||
'+@b.c',
|
||||
'+@b.com',
|
||||
'a@b.co-foo.uk',
|
||||
'"hello my name is"@stutter.com',
|
||||
'"Test \"Fail\" Ing"@iana.org',
|
||||
'valid@about.museum',
|
||||
'shaitan@my-domain.thisisminekthx',
|
||||
'foobar@192.168.0.1',
|
||||
'"Joe\Blow"@iana.org',
|
||||
'HM2Kinsists@(that comments are allowed)this.is.ok',
|
||||
'user%uucp!path@berkeley.edu',
|
||||
'first.last @iana.org',
|
||||
'cdburgess+!#$%&\'*-/=?+_{}|~test@gmail.com',
|
||||
'a(a(b(c)d(e(f))g)h(i)j)@example.org',
|
||||
'"hello my name is"@example.com',
|
||||
'"Test \"Fail\" Ing"@example.org',
|
||||
'first.last @example.org',
|
||||
];
|
||||
//Valid explicit IPv6 numeric addresses
|
||||
$validipv6 = [
|
||||
'first.last@[IPv6:::a2:a3:a4:b1:b2:b3:b4]',
|
||||
'first.last@[IPv6:a1:a2:a3:a4:b1:b2:b3::]',
|
||||
'first.last@[IPv6:::]',
|
||||
|
|
@ -457,9 +460,15 @@ class PHPMailerTest extends \PHPUnit_Framework_TestCase
|
|||
'first.last@[IPv6:0123:4567:89ab:cdef::11.22.33.44]',
|
||||
'first.last@[IPv6:0123:4567:89ab:CDEF::11.22.33.44]',
|
||||
'first.last@[IPv6:a1::b2:11.22.33.44]',
|
||||
'test@test.com',
|
||||
'test@xn--example.com',
|
||||
'test@example.com'
|
||||
'first.last@[IPv6:::12.34.56.78]',
|
||||
'first.last@[IPv6:1111:2222:3333::4444:12.34.56.78]',
|
||||
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:12.34.56.78]',
|
||||
'first.last@[IPv6:::1111:2222:3333:4444:5555:6666]',
|
||||
'first.last@[IPv6:1111:2222:3333::4444:5555:6666]',
|
||||
'first.last@[IPv6:1111:2222:3333:4444:5555:6666::]',
|
||||
'first.last@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888]',
|
||||
'first.last@[IPv6:1111:2222:3333::4444:5555:12.34.56.78]',
|
||||
'first.last@[IPv6:1111:2222:3333::4444:5555:6666:7777]',
|
||||
];
|
||||
$invalidaddresses = [
|
||||
'first.last@sub.do,com',
|
||||
|
|
|
|||
Loading…
Reference in New Issue