function _drupal_mail_wrapper in SMTP Authentication Support 5
Sends out the email.
Parameters
$mailkey: A key to identify the mail sent, for altering.
$to: string email address to send to.
$subject: string subject of email.
$body: string body of message.
$headers: array of headers.
1 call to _drupal_mail_wrapper()
- smtp.module in ./
smtp.module - Enables drupal to send email directly to an SMTP server using authentication. Uses the PHPMailer class by Brent R. Matzelle.
File
- ./
smtp.module, line 172 - Enables drupal to send email directly to an SMTP server using authentication. Uses the PHPMailer class by Brent R. Matzelle.
Code
function _drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $headers) {
$mail = new phpmailer();
//Create a new phpmailer object.
if (variable_get('smtp_debugging', 0) == 1) {
$mail->SMTPDebug = TRUE;
}
$username = variable_get('smtp_username', '');
$password = variable_get('smtp_password', '');
//Set the default name emails should be from. If value is not defined in settings use site_name.
if (variable_get('smtp_fromname', '') != '') {
$from_name = variable_get('smtp_fromname', '');
}
else {
// Blank value will let the email address appear.
$from_name = variable_get('site_name', '');
}
// If from email address is blank use smtp_from config option
if ($from == NULL || $from == '') {
if (variable_get('smtp_from', '') != '') {
$from = variable_get('smtp_from', '');
}
else {
// If smtp_from config option is blank use site_email.
$from = variable_get('site_email', '');
}
}
$from_name = preg_replace("/\"(.*)\"(.*)/i", "\$1", $from);
// It gives: Name
$from = preg_replace("/(.*)\\<(.*)\\>/i", "\$2", $from);
// It gives: name@domain.tld
// Defines the From value to what we expect
$mail->From = "\"" . $from_name . "\" <" . $from . ">";
$mail->FromName = $from_name;
$mail->Sender = $from;
//Decide whether to use SMTP Authorization. Do so if username and password are given.
if ($username != '' and $password != '') {
$auth = TRUE;
}
else {
$auth = FALSE;
}
//Take care of the email headers.
foreach ($headers as $key => $value) {
$lkey = strtolower($key);
$lvalue = strtolower($value);
switch ($lkey) {
case 'from':
if ($from == NULL or $from == '') {
// If a from value was already given then set based on header.
// Should be the most common situation since drupal_mail moves the from to headers
$from = $value;
$mail->From = $value;
$mail->FromName = '';
// then from can be out of sync with from_name !
$mail->Sender = $value;
}
break;
case 'content-type':
if (strpos($lvalue, 'text/html') !== FALSE) {
$mail
->IsHTML(TRUE);
}
else {
if (strpos($lvalue, 'multipart/mixed') !== FALSE || strpos($lvalue, 'multipart/alternative') !== FALSE) {
//$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone
$mail
->AddCustomHeader($key . ": " . $value);
$mail->message_type = "pre";
}
}
break;
case 'reply-to':
// Only add a "reply-to" if it's not the same as "return-path".
if ($value != $header['Return-Path']) {
$mail
->AddReplyTo($value);
}
break;
case 'return-path':
if (trim($value) != '') {
// This is be set by SmtpSend()
// $mail->Sender = $value;
}
break;
case 'content-transfer-encoding':
$mail->Encoding = $value;
break;
case 'mime-version':
case 'x-mailer':
case 'errors-to':
// ommit since it will be set by PHP-Mailer
break;
case 'bcc':
$bcc_addresses = _smtp_recipients_to_address_list($value);
foreach ($bcc_addresses as $bcc_address) {
$mail
->AddBCC($bcc_address['addr'], $bcc_address['name']);
}
break;
default:
$mail
->AddCustomHeader($key . ": " . $value);
}
}
// TODO: Delete the following commented 4 lines, as this is now done at the beginning of the function.
//If no from address has been set than use a default.
// if ($from == '' or $from == NULL){
// $from = variable_get('site_mail', 'test@example.com'); //If no from can be found use site_mail variable.
// }
//Set the correct protocol prefix to append to the smtp host.
switch (variable_get('smtp_protocol', 'standard')) {
case "ssl":
$mail->Protocol = 'ssl://';
break;
case "tls":
$mail->Protocol = 'tls://';
break;
case "standard":
$mail->Protocol = '';
}
$mail->Host = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
$mail->Port = variable_get('smtp_port', '25');
$mail->Mailer = "smtp";
$mail->SMTPAuth = $auth;
$mail->Username = $username;
$mail->Password = $password;
$mail->CharSet = 'utf-8';
$mail
->AddCustomHeader("Errors-To: " . $from);
// TODO: Delete the following commented 2 lines, as this is now done at the beginning of the function.
// $mail->From = $from;
// $mail->FromName = $from_name;
$to_addresses = _smtp_recipients_to_address_list($to);
foreach ($to_addresses as $to_address) {
$mail
->AddAddress($to_address['addr'], $to_address['name']);
}
$mail->Subject = $subject;
$mail->Body = $body;
watchdog('smtp', t('Sending mail to: !to', array(
'!to' => $to,
)));
//Try to send email, if it fails set watchdog entry.
if (!$mail
->Send()) {
watchdog("smtp", t('Error sending e-mail from !from to !to: ', array(
'!from' => $from,
'!to' => $to,
)) . $mail->ErrorInfo, WATCHDOG_ERROR);
return false;
}
$mail
->SmtpClose();
return true;
}