Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipient & From address formatting and validation #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions code/SmtpMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" <[email protected]>
$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" <[email protected]>
//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);
//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" <[email protected]> */
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" <[email protected]>
$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)){
Expand Down