mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
commit
2eb406dcd3
@ -292,6 +292,18 @@ class Email extends ViewableData
|
||||
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|null $name
|
||||
@ -299,6 +311,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function setFrom($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->setFrom($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -311,6 +324,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function addFrom($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->addFrom($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -331,6 +345,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function setSender($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->setSender($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -352,6 +367,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function setReturnPath($address)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->setReturnPath($address);
|
||||
return $this;
|
||||
}
|
||||
@ -376,6 +392,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function setTo($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->setTo($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -388,6 +405,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function addTo($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->addTo($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -408,6 +426,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function setCC($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->setCc($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -420,6 +439,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function addCC($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->addCc($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -440,6 +460,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function setBCC($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->setBcc($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -452,6 +473,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function addBCC($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->addBcc($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -472,6 +494,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function setReplyTo($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->setReplyTo($address, $name);
|
||||
|
||||
return $this;
|
||||
@ -484,6 +507,7 @@ class Email extends ViewableData
|
||||
*/
|
||||
public function addReplyTo($address, $name = null)
|
||||
{
|
||||
$address = $this->sanitiseAddress($address);
|
||||
$this->getSwiftMessage()->addReplyTo($address, $name);
|
||||
|
||||
return $this;
|
||||
|
@ -104,11 +104,16 @@ class TestMailer implements Mailer
|
||||
$matched = true;
|
||||
|
||||
foreach (['To', 'From', 'Subject', 'Content'] as $field) {
|
||||
$emailValue = $email[$field];
|
||||
if ($value = $compare[$field]) {
|
||||
if ($field == 'To') {
|
||||
$emailValue = $this->normaliseSpaces($emailValue);
|
||||
$value = $this->normaliseSpaces($value);
|
||||
}
|
||||
if ($value[0] == '/') {
|
||||
$matched = preg_match($value, $email[$field]);
|
||||
$matched = preg_match($value, $emailValue);
|
||||
} else {
|
||||
$matched = ($value == $email[$field]);
|
||||
$matched = ($value == $emailValue);
|
||||
}
|
||||
if (!$matched) {
|
||||
break;
|
||||
@ -122,4 +127,12 @@ class TestMailer implements Mailer
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
private function normaliseSpaces(string $value)
|
||||
{
|
||||
return str_replace([', ', '; '], [',', ';'], $value);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user