From 8456e280e6577af7a40783110ae6e7bcbf287f9b Mon Sep 17 00:00:00 2001 From: Jakx Date: Wed, 10 Jul 2013 11:28:32 +1200 Subject: [PATCH 1/2] SmtpMailer.php line 110 overriding pass through names format handling for sender (lines 97-101). Commented it out. --- code/SmtpMailer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/SmtpMailer.php b/code/SmtpMailer.php index 03c8aea..940dcc8 100644 --- a/code/SmtpMailer.php +++ b/code/SmtpMailer.php @@ -107,7 +107,7 @@ protected function buildBasicMail($to, $from, $subject){ $this->mailer->ClearAddresses(); $this->mailer->AddAddress($to, ucfirst(substr($to, 0, strpos($to, '@')))); //For the recipient's name, the string before the @ from the e-mail address is used - $this->mailer->SetFrom($from); + //$this->mailer->SetFrom($from); /* This is overriding the values set in lines 97-101 } $this->mailer->Subject = $subject; } From 5548caa4d98c70284a1467627159274f01d5bf1b Mon Sep 17 00:00:00 2001 From: "Jakx.nz" Date: Thu, 17 Jul 2014 14:45:27 +1200 Subject: [PATCH 2/2] Recipient & From address formatting and validation --- code/SmtpMailer.php | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/code/SmtpMailer.php b/code/SmtpMailer.php index 940dcc8..fb39ae9 100644 --- a/code/SmtpMailer.php +++ b/code/SmtpMailer.php @@ -93,25 +93,41 @@ function handleError($msg, $msgForLog){ } - protected function buildBasicMail($to, $from, $subject){ - if(preg_match('/(\'|")(.*?)\1[ ]+<[ ]*(.*?)[ ]*>/', $from, $from_splitted)){ //If $from countains a name, e.g. "My Name" - $this->mailer->SetFrom($from_splitted[3], $from_splitted[2]); - } else { - $this->mailer->SetFrom($from); + protected function buildBasicMail($to, $from = null, $subject) { + if($from) { //Only handle $from if set + if($from = $this->formatAddress($from)); + $this->mailer->SetFrom($from["address"], $from["name"]); } - if(preg_match('/(\'|")(.*?)\1[ ]+<[ ]*(.*?)[ ]*>/', $to, $to_splitted)){ //If $from countains a name, e.g. "My Name" + //Recipient $to + if($to = $this->formatAddress($to)) { $this->mailer->ClearAddresses(); - $this->mailer->AddAddress($to_splitted[3], $to_splitted[2]); + $this->mailer->AddAddress($to["address"], $to["name"]); } else { - $to = validEmailAddr($to); - $this->mailer->ClearAddresses(); - $this->mailer->AddAddress($to, ucfirst(substr($to, 0, strpos($to, '@')))); - //For the recipient's name, the string before the @ from the e-mail address is used - //$this->mailer->SetFrom($from); /* This is overriding the values set in lines 97-101 + //There should probably be an error handler here, rather than letting PHPMailer crash the process with a plain error. + return false; } $this->mailer->Subject = $subject; } + /* Format address values from plain email address or name & email address e.g "My Name" */ + protected function formatAddress($address) { + //Format from values + $name = null; + if(preg_match('/(\'|")(.*?)\1[ ]+<[ ]*(.*?)[ ]*>/', $address, $address_splitted)) { //If $address countains a name, e.g. "My Name" + $address = $address_splitted[3]; + $name = $address_splitted[2]; + } + $address = trim($address); //Clear whitespace + if(!Email::validEmailAddress($address)) { //Check for valid email address + $this->handleError("Email address is invalid", "Email address is invalid"); + return false; + } + if(!$name) //If name is not defined, use string before @ in e-mail address + $name = ucfirst(substr($address, 0, strpos($address, '@'))); + + return compact("address", "name"); + } + protected function addCustomHeaders($headers){ if(($headers == null) || !is_array($headers)){