Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into sherryl4george-xo…
Browse files Browse the repository at this point in the history
…auth

# Conflicts:
#	changelog.md
  • Loading branch information
Synchro committed May 21, 2015
2 parents d24a921 + 199bd96 commit 0d1f35f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -3,6 +3,7 @@
* Don't switch to quoted-printable for long lines if already using base64
* Fixed Travis-CI config when run on PHP 7
* Added Google XOAUTH2 authentication mechanism, thanks to @sherryl4george
* Add address parser for RFC822-format addresses

## Version 5.2.10 (May 4th 2015)
* Add custom header getter
Expand Down
55 changes: 55 additions & 0 deletions class.phpmailer.php
Expand Up @@ -863,6 +863,61 @@ protected function addAnAddress($kind, $address, $name = '')
return false;
}

/**
* Parse and validate a string containing one or more RFC822-style comma-separated email addresses
* of the form "display name <address>" into an array of name/address pairs.
* Uses the imap_rfc822_parse_adrlist function if the IMAP extension is available.
* Note that quotes in the name part are removed.
* @param string $addrstr The address list string
* @param bool $useimap Whether to use the IMAP extension to parse the list
* @return array
* @link http://www.andrew.cmu.edu/user/agreen1/testing/mrbs/web/Mail/RFC822.php A more careful implementation
*/
public function parseAddresses($addrstr, $useimap = true)
{
$addresses = array();
if ($useimap and function_exists('imap_rfc822_parse_adrlist')) {
//Use this built-in parser if it's available
$list = imap_rfc822_parse_adrlist($addrstr, '');
foreach ($list as $address) {
if ($address->host != '.SYNTAX-ERROR.') {
if ($this->validateAddress($address->mailbox . '@' . $address->host)) {
$addresses[] = array(
'name' => (property_exists($address, 'personal') ? $address->personal : ''),
'address' => $address->mailbox . '@' . $address->host
);
}
}
}
} else {
//Use this simpler parser
$list = explode(',', $addrstr);
foreach ($list as $address) {
$address = trim($address);
//Is there a separate name part?
if (strpos($address, '<') === false) {
//No separate name, just use the whole thing
if ($this->validateAddress($address)) {
$addresses[] = array(
'name' => '',
'address' => $address
);
}
} else {
list($name, $email) = explode('<', $address);
$email = trim(str_replace('>', '', $email));
if ($this->validateAddress($email)) {
$addresses[] = array(
'name' => trim(str_replace(array('"', "'"), '', $name)),
'address' => $email
);
}
}
}
}
return $addresses;
}

/**
* Set the From and FromName properties.
* @param string $address
Expand Down
84 changes: 84 additions & 0 deletions test/phpmailerTest.php
Expand Up @@ -1364,6 +1364,90 @@ public function testAddressing()
$this->Mail->clearReplyTos();
}

/**
* Test RFC822 address splitting.
*/
public function testAddressSplitting()
{
//Test built-in address parser
$this->assertCount(
2,
$this->Mail->parseAddresses(
'Joe User <joe@example.com>, Jill User <jill@example.net>'
),
'Failed to recognise address list (IMAP parser)'
);
//Test simple address parser
$this->assertCount(
2,
$this->Mail->parseAddresses(
'Joe User <joe@example.com>, Jill User <jill@example.net>',
false
),
'Failed to recognise address list'
);
//Test single address
$this->assertNotEmpty(
$this->Mail->parseAddresses(
'Joe User <joe@example.com>',
false
),
'Failed to recognise single address'
);
//Test quoted name IMAP
$this->assertNotEmpty(
$this->Mail->parseAddresses(
'Tim "The Book" O\'Reilly <foo@example.com>'
),
'Failed to recognise quoted name (IMAP)'
);
//Test quoted name
$this->assertNotEmpty(
$this->Mail->parseAddresses(
'Tim "The Book" O\'Reilly <foo@example.com>',
false
),
'Failed to recognise quoted name'
);
//Test single address IMAP
$this->assertNotEmpty(
$this->Mail->parseAddresses(
'Joe User <joe@example.com>'
),
'Failed to recognise single address (IMAP)'
);
//Test unnamed address
$this->assertNotEmpty(
$this->Mail->parseAddresses(
'joe@example.com',
false
),
'Failed to recognise unnamed address'
);
//Test unnamed address IMAP
$this->assertNotEmpty(
$this->Mail->parseAddresses(
'joe@example.com'
),
'Failed to recognise unnamed address (IMAP)'
);
//Test invalid addresses
$this->assertEmpty(
$this->Mail->parseAddresses(
'Joe User <joe@example.com.>, Jill User <jill.@example.net>'
),
'Failed to recognise invalid addresses (IMAP)'
);
//Test invalid addresses
$this->assertEmpty(
$this->Mail->parseAddresses(
'Joe User <joe@example.com.>, Jill User <jill.@example.net>',
false
),
'Failed to recognise invalid addresses'
);
}

/**
* Test address escaping.
*/
Expand Down

0 comments on commit 0d1f35f

Please sign in to comment.