View source
<?php
module_load_include('php', 'phpmailer', 'phpmailer/class.phpmailer');
class DrupalPHPMailer extends PHPMailer {
var $ReturnPath = '';
function DrupalPHPMailer() {
$this
->IsSMTP();
$this
->Reset();
$protocol = variable_get('smtp_protocol', '');
$this->Host = $protocol . variable_get('smtp_host', '');
if ($backup = variable_get('smtp_hostbackup', '')) {
$this->Host .= ';' . $protocol . $backup;
}
$this->Port = variable_get('smtp_port', '25');
$this->Username = variable_get('smtp_username', '');
$this->Password = variable_get('smtp_password', '');
$this->SMTPAuth = (bool) ($this->Username != '' && $this->Password != '');
$this->SMTPKeepAlive = variable_get('smtp_keepalive', 0);
$this->SMTPDebug = variable_get('smtp_debug', 0);
$this->PluginDir = drupal_get_path('module', 'phpmailer') . '/phpmailer/';
}
function Reset() {
$this
->ClearAllRecipients();
$this
->ClearAttachments();
$this
->ClearCustomHeaders();
$this->Priority = 3;
$this->CharSet = variable_get('smtp_charset', 'utf-8');
$this->ContentType = 'text/plain';
$this->Encoding = '8bit';
$this->ErrorInfo = '';
$from_name = variable_get('smtp_fromname', '');
if ($from_name == '') {
$from_name = variable_get('site_name', 'Drupal');
}
$this->FromName = $from_name;
$this->Sender = '';
$this->MessageID = '';
$this->ReturnPath = '';
}
function SetLanguage($lang_type = 'en', $lang_path = 'language/') {
$PHPMAILER_LANG = array();
$PHPMAILER_LANG['provide_address'] = 'You must provide at least one recipient email address.';
$PHPMAILER_LANG['mailer_not_supported'] = ' mailer is not supported.';
$PHPMAILER_LANG['execute'] = 'Could not execute: ';
$PHPMAILER_LANG['instantiate'] = 'Could not instantiate mail function.';
$PHPMAILER_LANG['authenticate'] = 'SMTP Error: Could not authenticate.';
$PHPMAILER_LANG['from_failed'] = 'The following From address failed: ';
$PHPMAILER_LANG['recipients_failed'] = 'SMTP Error: The following recipients failed: ';
$PHPMAILER_LANG['data_not_accepted'] = 'SMTP Error: Data not accepted.';
$PHPMAILER_LANG['connect_host'] = 'SMTP Error: Could not connect to SMTP host.';
$PHPMAILER_LANG['file_access'] = 'Could not access file: ';
$PHPMAILER_LANG['file_open'] = 'File Error: Could not open file: ';
$PHPMAILER_LANG['encoding'] = 'Unknown encoding: ';
$PHPMAILER_LANG['signing'] = 'Signing Error: ';
$this->language = $PHPMAILER_LANG;
return TRUE;
}
function CreateHeader() {
$old_sender = $this->Sender;
if ($this->ReturnPath != '') {
$this->Sender = $this->ReturnPath;
}
$result = parent::CreateHeader();
$this->Sender = $old_sender;
return $result;
}
function RFCDate() {
$tz = date('Z');
$tzs = $tz < 0 ? '-' : '+';
$tz = abs($tz);
$tz = (int) ($tz / 3600) * 100 + $tz % 3600 / 60;
$result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
return $result;
}
}
function phpmailer_send($message) {
static $mail;
if (!isset($mail)) {
$mail = new DrupalPHPMailer();
}
$address = phpmailer_parse_address($message['from']);
$mail->From = $address[0]['mail'];
$mail->FromName = $address[0]['name'];
unset($message['headers']['From']);
if (variable_get('phpmailer_debug_email', '') === '') {
foreach (phpmailer_parse_address($message['to']) as $address) {
$mail
->AddAddress($address['mail'], $address['name']);
}
if (isset($message['headers']['CC'])) {
foreach (phpmailer_parse_address($message['headers']['CC']) as $address) {
$mail
->AddCC($address['mail'], $address['name']);
}
}
if (isset($message['headers']['BCC'])) {
foreach (phpmailer_parse_address($message['headers']['BCC']) as $address) {
$mail
->AddBCC($address['mail'], $address['name']);
}
}
}
else {
$mail
->AddAddress(variable_get('phpmailer_debug_email', ''));
}
unset($message['headers']['CC'], $message['headers']['BCC']);
if (isset($message['headers']['Content-Type'])) {
$content_type = explode(';', $message['headers']['Content-Type']);
$mail->ContentType = trim(array_shift($content_type));
foreach ($content_type as $param) {
$param = explode('=', $param, 2);
$key = trim($param[0]);
if ($key == 'charset') {
$mail->CharSet = trim($param[1]);
}
else {
$mail->ContentType .= '; ' . $key . '=' . trim($param[1]);
}
}
unset($message['headers']['Content-Type']);
}
$properties = array(
'X-Priority' => 'Priority',
'Content-Transfer-Encoding' => 'Encoding',
'Sender' => 'Sender',
'Message-ID' => 'MessageID',
'Return-Path' => 'ReturnPath',
);
foreach ($properties as $source => $property) {
if (isset($message['headers'][$source])) {
$mail->{$property} = $message['headers'][$source];
unset($message['headers'][$source]);
}
}
unset($message['headers']['MIME-Version']);
foreach ($message['headers'] as $key => $value) {
$mail
->AddCustomHeader("{$key}:{$value}");
}
$mail->Subject = $message['subject'];
$mail->Body = $message['body'];
if ($mail->SMTPDebug) {
ob_start();
}
if (!($result = $mail
->Send())) {
watchdog('phpmailer', $mail->ErrorInfo, NULL, WATCHDOG_ERROR);
}
$mail
->Reset();
if ($mail->SMTPDebug) {
if ($debug = ob_get_contents()) {
drupal_set_message($debug);
}
ob_end_clean();
}
return $result;
}