Merge pull request #10065 from creative-commoners/pulls/4/trim-email-addresses

FIX Trim email addresses to comply with RFC 2822, 3.6.2
This commit is contained in:
Steve Boyd 2021-08-25 09:07:35 +12:00 committed by GitHub
commit 2eb406dcd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View File

@ -292,6 +292,18 @@ class Email extends ViewableData
return $this->getSwiftMessage()->getFrom(); return $this->getSwiftMessage()->getFrom();
} }
/**
* @param string|array $address
* @return string|array
*/
private function sanitiseAddress($address)
{
if (is_array($address)) {
return array_map('trim', $address);
}
return trim($address);
}
/** /**
* @param string|array $address * @param string|array $address
* @param string|null $name * @param string|null $name
@ -299,6 +311,7 @@ class Email extends ViewableData
*/ */
public function setFrom($address, $name = null) public function setFrom($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->setFrom($address, $name); $this->getSwiftMessage()->setFrom($address, $name);
return $this; return $this;
@ -311,6 +324,7 @@ class Email extends ViewableData
*/ */
public function addFrom($address, $name = null) public function addFrom($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->addFrom($address, $name); $this->getSwiftMessage()->addFrom($address, $name);
return $this; return $this;
@ -331,6 +345,7 @@ class Email extends ViewableData
*/ */
public function setSender($address, $name = null) public function setSender($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->setSender($address, $name); $this->getSwiftMessage()->setSender($address, $name);
return $this; return $this;
@ -352,6 +367,7 @@ class Email extends ViewableData
*/ */
public function setReturnPath($address) public function setReturnPath($address)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->setReturnPath($address); $this->getSwiftMessage()->setReturnPath($address);
return $this; return $this;
} }
@ -376,6 +392,7 @@ class Email extends ViewableData
*/ */
public function setTo($address, $name = null) public function setTo($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->setTo($address, $name); $this->getSwiftMessage()->setTo($address, $name);
return $this; return $this;
@ -388,6 +405,7 @@ class Email extends ViewableData
*/ */
public function addTo($address, $name = null) public function addTo($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->addTo($address, $name); $this->getSwiftMessage()->addTo($address, $name);
return $this; return $this;
@ -408,6 +426,7 @@ class Email extends ViewableData
*/ */
public function setCC($address, $name = null) public function setCC($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->setCc($address, $name); $this->getSwiftMessage()->setCc($address, $name);
return $this; return $this;
@ -420,6 +439,7 @@ class Email extends ViewableData
*/ */
public function addCC($address, $name = null) public function addCC($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->addCc($address, $name); $this->getSwiftMessage()->addCc($address, $name);
return $this; return $this;
@ -440,6 +460,7 @@ class Email extends ViewableData
*/ */
public function setBCC($address, $name = null) public function setBCC($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->setBcc($address, $name); $this->getSwiftMessage()->setBcc($address, $name);
return $this; return $this;
@ -452,6 +473,7 @@ class Email extends ViewableData
*/ */
public function addBCC($address, $name = null) public function addBCC($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->addBcc($address, $name); $this->getSwiftMessage()->addBcc($address, $name);
return $this; return $this;
@ -472,6 +494,7 @@ class Email extends ViewableData
*/ */
public function setReplyTo($address, $name = null) public function setReplyTo($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->setReplyTo($address, $name); $this->getSwiftMessage()->setReplyTo($address, $name);
return $this; return $this;
@ -484,6 +507,7 @@ class Email extends ViewableData
*/ */
public function addReplyTo($address, $name = null) public function addReplyTo($address, $name = null)
{ {
$address = $this->sanitiseAddress($address);
$this->getSwiftMessage()->addReplyTo($address, $name); $this->getSwiftMessage()->addReplyTo($address, $name);
return $this; return $this;

View File

@ -104,11 +104,16 @@ class TestMailer implements Mailer
$matched = true; $matched = true;
foreach (['To', 'From', 'Subject', 'Content'] as $field) { foreach (['To', 'From', 'Subject', 'Content'] as $field) {
$emailValue = $email[$field];
if ($value = $compare[$field]) { if ($value = $compare[$field]) {
if ($field == 'To') {
$emailValue = $this->normaliseSpaces($emailValue);
$value = $this->normaliseSpaces($value);
}
if ($value[0] == '/') { if ($value[0] == '/') {
$matched = preg_match($value, $email[$field]); $matched = preg_match($value, $emailValue);
} else { } else {
$matched = ($value == $email[$field]); $matched = ($value == $emailValue);
} }
if (!$matched) { if (!$matched) {
break; break;
@ -122,4 +127,12 @@ class TestMailer implements Mailer
} }
return null; return null;
} }
/**
* @param string $value
*/
private function normaliseSpaces(string $value)
{
return str_replace([', ', '; '], [',', ';'], $value);
}
} }