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

hadnle multiple 'to' + empty 'from' #16

Open
wants to merge 1 commit 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
69 changes: 43 additions & 26 deletions code/SmtpMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $c


protected function sendMailViaSmtp($to, $from, $subject, $attachedFiles = false, $customheaders = false, $inlineImages = false){
// setting a blank $from address should try to use the site's default administrator email
if(empty($from)) $from = Email::config()->admin_email;

if($this->mailer->SMTPDebug > 0){
echo "<em><strong>*** Debug mode is on</strong>, printing debug messages and not redirecting to the website:</em><br/>";
echo "To: $to, From: $from, Subject: $subject<br>";
Expand Down Expand Up @@ -103,7 +106,11 @@ protected function sendMailViaSmtp($to, $from, $subject, $attachedFiles = false,


function handleError($msg, $msgForLog){
user_error($msg, $this->mailer->ErrorLevel);
// $msgForLog contains whole body of email also
SS_Log::log('SMTP Mailer: error occured with exception: '.$msg."\nEmail content: ".$msgForLog, SS_Log::WARN);
throw new Exception('SMTP Mailer: error occured with exception: '.$msg."\nEmail content: ".$msgForLog);
// do not end up in error
// user_error($msg, $this->mailer->ErrorLevel);
}


Expand All @@ -115,11 +122,17 @@ protected function buildBasicMail($to, $from, $subject){
}

$this->mailer->ClearAddresses();
if(preg_match('/(\'|")(.*?)\1[ ]+<[ ]*(.*?)[ ]*>/', $to, $to_splitted)){ //If $to countains a name, e.g. "My Name" <[email protected]>
$this->mailer->AddAddress($to_splitted[3], $to_splitted[2]);
} else {
//For the recipient's name, the string before the @ from the e-mail address is used
$this->mailer->AddAddress($to, ucfirst(substr($to, 0, strpos($to, '@'))));
// loop through addressees if more are set
foreach (explode(',', $to) as $recipient) {
$recipient = trim($recipient);
if (strlen($recipient) > 0) {
if(preg_match('/(\'|")(.*?)\1[ ]+<[ ]*(.*?)[ ]*>/', $recipient, $recipient_splitted)){ //If $to countains a name, e.g. "My Name" <[email protected]>
$this->mailer->AddAddress($recipient_splitted[3], $recipient_splitted[2]);
} else {
//For the recipient's name, the string before the @ from the e-mail address is used
$this->mailer->AddAddress($recipient, ucfirst(substr($recipient, 0, strpos($recipient, '@'))));
}
}
}

$this->mailer->Subject = $subject;
Expand All @@ -139,31 +152,35 @@ protected function addCustomHeaders($headers){

$this->mailer->ClearCustomHeaders();

//Convert cc/bcc/ReplyTo from headers to properties
//Convert cc/bcc/ReplyTo from headers to properties
foreach($headers as $header_name => $header_value){
if(in_array(strtolower($header_name), array('cc', 'bcc', 'reply-to', 'replyto'))){
$addresses = preg_split('/(,|;)/', $header_value);
}
switch(strtolower($header_name)) {
case 'cc':
foreach($addresses as $address){ $this->mailer->addCC($address); }
break;
case 'bcc':
foreach($addresses as $address) { $this->mailer->addBCC($address); }
break;
case 'reply-to':
foreach($addresses as $address) { $this->mailer->addReplyTo($address); }
break;
default:
$this->mailer->AddCustomHeader($header_name . ':' . $header_value);
break;
}
if(in_array(strtolower($header_name), array('cc', 'bcc', 'reply-to', 'replyto'))){
$addresses = preg_split('/(,|;)/', $header_value);
}
switch(strtolower($header_name)) {
case 'cc':
foreach($addresses as $address){ $this->mailer->addCC($address); }
break;
case 'bcc':
foreach($addresses as $address) { $this->mailer->addBCC($address); }
break;
case 'reply-to':
foreach($addresses as $address) { $this->mailer->addReplyTo($address); }
break;
default:
$this->mailer->AddCustomHeader($header_name . ':' . $header_value);
break;
}
}
}


protected function attachFiles($attachedFiles){
if(!empty($attachedFiles) && is_array($attachedFiles)){
// add attachments
if ($attachedFiles && is_array($attachedFiles)) foreach ($attachedFiles as $attachedFile) {
$this->mailer->addStringAttachment($attachedFile['contents'], $attachedFile['filename']);
}
/*if(!empty($attachedFiles) && is_array($attachedFiles)){
foreach($attachedFiles as $attachedFile){
if(substr($attachedFile['filename'], 0, strlen(Director::baseFolder())) === Director::baseFolder()){ // If the file path is already included, don't include it again
$filePath = $attachedFile['filename'];
Expand All @@ -172,7 +189,7 @@ protected function attachFiles($attachedFiles){
}
$this->mailer->AddAttachment($filePath);
}
}
}*/
}


Expand Down