Skip to content
Snippets Groups Projects
Commit 2780a4ba authored by Jason Flatt's avatar Jason Flatt Committed by Jason Flatt
Browse files

Issue #2947486 by oadaeh, loopduplicate: Clean up code

parent d39cf0bd
No related branches found
No related tags found
No related merge requests found
...@@ -6,12 +6,16 @@ ...@@ -6,12 +6,16 @@
*/ */
/** /**
* Base PHPMailer for Drupal implementation with support for SMTP keep-alive * Class for implementing the PHPMailer library in Drupal.
* and setting a custom Return-Path. *
* Includes support for SMTP keep-alive and setting a custom Return-Path.
*/ */
class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
/** /**
* Stores the Return-Path, which may be different from Sender. * Stores the Return-Path, which may be different from Sender.
*
* @var string
*/ */
public $ReturnPath = ''; public $ReturnPath = '';
...@@ -25,17 +29,19 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -25,17 +29,19 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
* DrupalPHPMailer::SmtpSend() overrides PHPMailer::SmtpSend() to capture the * DrupalPHPMailer::SmtpSend() overrides PHPMailer::SmtpSend() to capture the
* debug output string and make it available for watchdog() calls. * debug output string and make it available for watchdog() calls.
* *
* @var int
*
* @see PHPMailer::SMTPDebug * @see PHPMailer::SMTPDebug
* @see SMTP::do_debug * @see SMTP::do_debug
* @see DrupalPHPMailer::SmtpSend() * @see DrupalPHPMailer::SmtpSend()
* @see DrupalPHPMailer::drupalDebugOutput * @see DrupalPHPMailer::drupalDebugOutput
*
* @var int
*/ */
public $drupalDebug = 0; public $drupalDebug = 0;
/** /**
* Overrides PHPMailer::SMTPDebug to capture SMTP communication errors by default. * Overrides PHPMailer::SMTPDebug to capture SMTP communication errors.
*
* @var int
*/ */
public $SMTPDebug = 2; public $SMTPDebug = 2;
...@@ -109,7 +115,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -109,7 +115,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
$this->SmtpClose(); $this->SmtpClose();
} }
} }
catch (phpmailerException $exception) {} catch (phpmailerException $exception) {
}
if ($this->SMTPDebug) { if ($this->SMTPDebug) {
if ($this->drupalDebug && ($this->drupalDebugOutput = ob_get_contents())) { if ($this->drupalDebug && ($this->drupalDebugOutput = ob_get_contents())) {
...@@ -132,7 +139,7 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -132,7 +139,7 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
} }
/** /**
* (Re-)initialize properties after sending mail. * Initialize or re-initialize properties after sending mail.
*/ */
public function Reset() { public function Reset() {
$this->ClearAllRecipients(); $this->ClearAllRecipients();
...@@ -188,21 +195,21 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -188,21 +195,21 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
'connect_host' => t('SMTP error: Could not connect to host.'), 'connect_host' => t('SMTP error: Could not connect to host.'),
'data_not_accepted' => t('SMTP error: Data not accepted.'), 'data_not_accepted' => t('SMTP error: Data not accepted.'),
'smtp_connect_failed' => t('SMTP error: Could not connect to SMTP host.'), 'smtp_connect_failed' => t('SMTP error: Could not connect to SMTP host.'),
'smtp_error' => t('SMTP server error: '), 'smtp_error' => t('SMTP server error:') . ' ',
// Messages used during email generation. // Messages used during email generation.
'empty_message' => t('Message body empty'), 'empty_message' => t('Message body empty'),
'encoding' => t('Unknown encoding: '), 'encoding' => t('Unknown encoding:') . ' ',
'variable_set' => t('Cannot set or reset variable: '), 'variable_set' => t('Cannot set or reset variable:') . ' ',
'file_access' => t('File error: Could not access file: '), 'file_access' => t('File error: Could not access file:') . ' ',
'file_open' => t('File error: Could not open file: '), 'file_open' => t('File error: Could not open file:') . ' ',
// Non-administrative messages. // Non-administrative messages.
'from_failed' => t('The following From address failed: '), 'from_failed' => t('The following From address failed:') . ' ',
'invalid_address' => t('Invalid address'), 'invalid_address' => t('Invalid address'),
'provide_address' => t('You must provide at least one recipient e-mail address.'), 'provide_address' => t('You must provide at least one recipient e-mail address.'),
'recipients_failed' => t('The following recipients failed: '), 'recipients_failed' => t('The following recipients failed:') . ' ',
) + $this->language; ) + $this->language;
return TRUE; return TRUE;
} }
...@@ -214,10 +221,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -214,10 +221,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
$tz = date('Z'); $tz = date('Z');
$tzs = ($tz < 0) ? '-' : '+'; $tzs = ($tz < 0) ? '-' : '+';
$tz = abs($tz); $tz = abs($tz);
$tz = (int)($tz / 3600) * 100 + ($tz % 3600) / 60; $tz = (int) ($tz / 3600) * 100 + ($tz % 3600) / 60;
$result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz); return sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
return $result;
} }
/** /**
...@@ -227,13 +232,13 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -227,13 +232,13 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
* module, plain-text e-mails are sent, which require the same processing as * module, plain-text e-mails are sent, which require the same processing as
* in the DefaultMailSystem implementation. * in the DefaultMailSystem implementation.
* *
* @see DefaultMailSystem::format() * @param array $message
*
* @param $message
* A message array, as described in hook_mail_alter(). * A message array, as described in hook_mail_alter().
* *
* @return * @return array
* The formatted $message. * The formatted $message.
*
* @see DefaultMailSystem::format()
*/ */
public function format(array $message) { public function format(array $message) {
// Join the body array into one string. // Join the body array into one string.
...@@ -248,9 +253,10 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -248,9 +253,10 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
/** /**
* Sends an e-mail message composed by drupal_mail(). * Sends an e-mail message composed by drupal_mail().
* *
* @param $message * @param array $message
* A message array, as described in hook_mail_alter(). * A message array, as described in hook_mail_alter().
* @return *
* @return bool
* TRUE if the mail was successfully accepted, otherwise FALSE. * TRUE if the mail was successfully accepted, otherwise FALSE.
* *
* @see PHPMailer::Send() * @see PHPMailer::Send()
...@@ -297,8 +303,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -297,8 +303,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
unset($message['headers']['Reply-To']); unset($message['headers']['Reply-To']);
} }
elseif (variable_get('smtp_always_replyto', FALSE)) { elseif (variable_get('smtp_always_replyto', FALSE)) {
// If no Reply-To header has been explicitly set, use the From address to // If no Reply-To header has been explicitly set, use the From address
// be able to respond to e-mails sent via Google Mail. // to be able to respond to e-mails sent via Google Mail.
$this->AddReplyTo($from['mail'], $from['name']); $this->AddReplyTo($from['mail'], $from['name']);
} }
...@@ -338,8 +344,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -338,8 +344,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
unset($message['headers']['MIME-Version']); unset($message['headers']['MIME-Version']);
// Add remaining header lines. // Add remaining header lines.
// Note: Any header lines MUST already be checked by the caller for unwanted // Note: Any header lines MUST already be checked by the caller for
// newline characters to avoid header injection. // unwanted newline characters to avoid header injection.
// @see PHPMailer::SecureHeader() // @see PHPMailer::SecureHeader()
foreach ($message['headers'] as $key => $value) { foreach ($message['headers'] as $key => $value) {
$this->AddCustomHeader("$key:$value"); $this->AddCustomHeader("$key:$value");
...@@ -354,9 +360,9 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -354,9 +360,9 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
// Log the error including verbose debug information. // Log the error including verbose debug information.
// Since DBLog module is the most common case, we use HTML to format the // Since DBLog module is the most common case, we use HTML to format the
// message for visual inspection. For sites running with Syslog or other // message for visual inspection. For sites running with Syslog or other
// logging modules, we put the actual values on separate lines (\n), so the // logging modules, we put the actual values on separate lines (\n), so
// surrounding HTML markup doesn't get too disturbing. // the surrounding HTML markup doesn't get too disturbing.
//
// Message is a safe t() string from DrupalPHPMailer::SetLanguage(). // Message is a safe t() string from DrupalPHPMailer::SetLanguage().
$output = $e->getMessage(); $output = $e->getMessage();
// Attempt to delimit summary from full message. // Attempt to delimit summary from full message.
...@@ -364,7 +370,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -364,7 +370,8 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
$arguments = array(); $arguments = array();
// Append SMTP communication output. // Append SMTP communication output.
if ($this->drupalDebugOutput) { if ($this->drupalDebugOutput) {
// PHPMailer debug output contains HTML linebreaks. PRE is more readable. // PHPMailer debug output contains HTML linebreaks. PRE is more
// readable.
$this->drupalDebugOutput = str_replace('<br />', '', $this->drupalDebugOutput); $this->drupalDebugOutput = str_replace('<br />', '', $this->drupalDebugOutput);
$output .= '<p><strong>Server response:</strong></p>'; $output .= '<p><strong>Server response:</strong></p>';
$output .= "<pre>\n@smtp_output\n</pre>"; $output .= "<pre>\n@smtp_output\n</pre>";
...@@ -404,5 +411,5 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface { ...@@ -404,5 +411,5 @@ class DrupalPHPMailer extends PHPMailer implements MailSystemInterface {
return FALSE; return FALSE;
} }
} }
}
}
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
/** /**
* Sends an already formatted e-mail message composed by drupal_mail(). * Sends an already formatted e-mail message composed by drupal_mail().
* *
* @param $message * @param array $message
* A message array, as described in hook_mail_alter(). * A message array, as described in hook_mail_alter().
* @return *
* @return bool
* TRUE if the mail was successfully accepted, otherwise FALSE. * TRUE if the mail was successfully accepted, otherwise FALSE.
* *
* @see DrupalPHPMailer::mail() * @see DrupalPHPMailer::mail()
...@@ -18,7 +19,7 @@ ...@@ -18,7 +19,7 @@
* @todo Consider to either drop this entirely, or move into phpmailer.module * @todo Consider to either drop this entirely, or move into phpmailer.module
* as singleton to instantiate the DrupalPHPMailer class. * as singleton to instantiate the DrupalPHPMailer class.
*/ */
function phpmailer_send($message) { function phpmailer_send(array $message) {
$mail = &drupal_static(__FUNCTION__); $mail = &drupal_static(__FUNCTION__);
if (!isset($mail)) { if (!isset($mail)) {
...@@ -26,4 +27,3 @@ function phpmailer_send($message) { ...@@ -26,4 +27,3 @@ function phpmailer_send($message) {
} }
return $mail->mail($message); return $mail->mail($message);
} }
...@@ -8,10 +8,13 @@ ...@@ -8,10 +8,13 @@
/** /**
* Send out an e-mail. * Send out an e-mail.
* *
* @param $message * @param array $message
* Mime Mail message array. * Mime Mail message array.
*
* @return bool
* TRUE if the mail was successfully accepted, otherwise FALSE.
*/ */
function mimemail_phpmailer_send($message) { function mimemail_phpmailer_send(array $message) {
static $mail; static $mail;
if (!isset($mail)) { if (!isset($mail)) {
...@@ -23,7 +26,7 @@ function mimemail_phpmailer_send($message) { ...@@ -23,7 +26,7 @@ function mimemail_phpmailer_send($message) {
try { try {
// Extract and assign e-mail addresses required for SMTP. // Extract and assign e-mail addresses required for SMTP.
// Display names are usually not required. Leave header intact. // Display names are usually not required. Leave header intact.
//
// Parse 'From' e-mail address. // Parse 'From' e-mail address.
$from = phpmailer_parse_address($message['from']); $from = phpmailer_parse_address($message['from']);
$from = reset($from); $from = reset($from);
...@@ -76,11 +79,12 @@ function mimemail_phpmailer_send($message) { ...@@ -76,11 +79,12 @@ function mimemail_phpmailer_send($message) {
$message['headers']['Subject'] = mime_header_encode($message['subject']); $message['headers']['Subject'] = mime_header_encode($message['subject']);
} }
// FIXME SpamAssassin says INVALID_MSGID to PHPMailer's generated Message-ID. 06/04/2009 smk // FIXME SpamAssassin says INVALID_MSGID to PHPMailer's generated
// if (!isset($message['headers']['Message-ID'])) { // Message-ID. 06/04/2009 smk.
// $message['headers']['Message-ID'] = sprintf("<%s@%s>", md5(uniqid(time())), $mail->ServerHostname()); // if (!isset($message['headers']['Message-ID'])) {
// } // $message['headers']['Message-ID'] = sprintf("<%s@%s>",
// md5(uniqid(time())), $mail->ServerHostname());
// }
$header = mimemail_rfc_headers($message['headers']) . $mail->LE . $mail->LE; $header = mimemail_rfc_headers($message['headers']) . $mail->LE . $mail->LE;
return $mail->SmtpSend($header, $message['body']); return $mail->SmtpSend($header, $message['body']);
......
...@@ -37,7 +37,7 @@ function phpmailer_settings_form($form, $form_state) { ...@@ -37,7 +37,7 @@ function phpmailer_settings_form($form, $form_state) {
$form['smtp_on']['#default_value'] = 0; $form['smtp_on']['#default_value'] = 0;
$form['smtp_on']['#description'] = t('MimeMail has been detected. To enable PHPMailer for mail transport, go to the <a href="@url">MimeMail settings page</a> and select PHPMailer from the available e-mail engines.', array('@url' => url('admin/config/system/mimemail'))); $form['smtp_on']['#description'] = t('MimeMail has been detected. To enable PHPMailer for mail transport, go to the <a href="@url">MimeMail settings page</a> and select PHPMailer from the available e-mail engines.', array('@url' => url('admin/config/system/mimemail')));
} }
elseif (!variable_get('smtp_on', 0) && empty($form_state['input'])) { elseif (!variable_get('smtp_on', 0) && empty($form_state['values'])) {
drupal_set_message(t('PHPMailer is currently disabled.'), 'warning'); drupal_set_message(t('PHPMailer is currently disabled.'), 'warning');
} }
...@@ -92,18 +92,16 @@ function phpmailer_settings_form($form, $form_state) { ...@@ -92,18 +92,16 @@ function phpmailer_settings_form($form, $form_state) {
'NTLM' => 'NTLM', 'NTLM' => 'NTLM',
'XOAUTH2' => 'XOAUTH2', 'XOAUTH2' => 'XOAUTH2',
); );
$description = t( $description = t('If you do not specify an authentication type for use when authenticating with the server, the PHPMailer library will attempt to guess one based on the infomration it receives from the server.');
' If you do not specify an authentication type for use when authenticating with the server, the PHPMailer library will attempt to guess one, based on the infomration it receives from the server.' $description .= ' ' . t('If it does not work and you receive notices about failed authentication, try specifying the authentication type.');
. ' If that does not work, and you receive authentication failed notices, try specifying the authentication type.' $description .= '<br />' . t('If you are unsure which type to use, consider the following guidelines:');
. ' <br />If you are unsure which type to use, consider the following guidelines:' $description .= '<ul>';
. ' <ul>' $description .= '<li>' . t('If the connection is NOT using a secure protocol, try CRAM-MD5 first, then try LOGIN or PLAIN.') . '</li>';
. ' <li>If the connection is NOT using a sucure protocol, try CRAM-MD5 first, then try LOGIN or PLAIN.</li>' $description .= '<li>' . t('If the connection is using a secure protocol, you can try LOGIN or PLAIN first, then CRAM-MD5.') . '</li>';
. ' <li>If the connection is using a secure protocol, you can try LOGIN or PLAIN first, then CRAM-MD5.</li>' $description .= '</ul>';
. ' </ul>' $description .= t('(CRAM-MD5 hashes the password before sending it, so it is not transmitted "in the clear.")');
. ' (CRAM-MD5 hashes the password before sending, so it is not transmitted in the clear.)' $description .= '<br />' . t('If none of those options work, contact your email hoster to find out what their SMTP server authentication type is.');
. ' <br />If none of those options work, contact your email hoster to find out what their SMTP server authentication type is.' $description .= '<br />' . t('The PHPMailer library only supports those types listed.');
. ' <br />The PHPMailer library only supports those types listed.'
);
$form['auth']['smtp_auth_type'] = array( $form['auth']['smtp_auth_type'] = array(
'#type' => 'select', '#type' => 'select',
'#title' => t('Authentication type'), '#title' => t('Authentication type'),
...@@ -169,11 +167,17 @@ function phpmailer_settings_form($form, $form_state) { ...@@ -169,11 +167,17 @@ function phpmailer_settings_form($form, $form_state) {
'#default_value' => variable_get('smtp_encode_headers', 0), '#default_value' => variable_get('smtp_encode_headers', 0),
'#description' => t('If you have subjects and names with accented, etc. characters, check this box to UTF-8 encode them before sending the message.'), '#description' => t('If you have subjects and names with accented, etc. characters, check this box to UTF-8 encode them before sending the message.'),
); );
$debug_options = array(
0 => t('Disabled'),
1 => t('Errors only'),
2 => t('Server responses'),
4 => t('Full communication'),
);
$form['advanced']['smtp_debug'] = array( $form['advanced']['smtp_debug'] = array(
'#type' => 'select', '#type' => 'select',
'#title' => t('Debug level'), '#title' => t('Debug level'),
'#default_value' => variable_get('smtp_debug', 0), '#default_value' => variable_get('smtp_debug', 0),
'#options' => array(0 => t('Disabled'), 1 => t('Errors only'), 2 => t('Server responses'), 4 => t('Full communication')), '#options' => $debug_options,
'#description' => t("Debug the communication with the SMTP server. You normally shouldn't enable this unless you're trying to debug e-mail sending problems."), '#description' => t("Debug the communication with the SMTP server. You normally shouldn't enable this unless you're trying to debug e-mail sending problems."),
); );
$form['advanced']['smtp_debug_log'] = array( $form['advanced']['smtp_debug_log'] = array(
...@@ -236,17 +240,17 @@ function phpmailer_settings_form($form, $form_state) { ...@@ -236,17 +240,17 @@ function phpmailer_settings_form($form, $form_state) {
$result = phpmailer_send($message); $result = phpmailer_send($message);
} }
$notice = 'A test e-mail has been sent to %email. You might still want to !watchdog-url for possible error messages.';
$replacements = array( $replacements = array(
'%email' => $test_address, '%email' => $test_address,
'!watchdog-url' => l(t('check the logs'), 'admin/reports/dblog'), '!watchdog-url' => l(t('check the logs'), 'admin/reports/dblog'),
); );
$notice = t('A test e-mail has been sent to %email. You might still want to !watchdog-url for possible error messages.', $replacements);
$message_type = 'status'; $message_type = 'status';
if (empty($result) || (is_array($result) && empty($result['result']))) { if (empty($result) || (is_array($result) && empty($result['result']))) {
$notice = 'A test e-mail has NOT been sent to %email. You should !watchdog-url for error messages.'; $notice = t('A test e-mail has NOT been sent to %email. You should !watchdog-url for error messages.', $replacements);
$message_type = 'error'; $message_type = 'error';
} }
drupal_set_message(t($notice, $replacements), $message_type); drupal_set_message($notice, $message_type);
} }
$form['test'] = array( $form['test'] = array(
...@@ -297,7 +301,7 @@ function phpmailer_settings_form_submit($form, &$form_state) { ...@@ -297,7 +301,7 @@ function phpmailer_settings_form_submit($form, &$form_state) {
watchdog('phpmailer', 'PHPMailer has been enabled.'); watchdog('phpmailer', 'PHPMailer has been enabled.');
} }
} }
else if (phpmailer_enabled()) { elseif (phpmailer_enabled()) {
// Remove PHPMailer from all mail keys it is configured for. // Remove PHPMailer from all mail keys it is configured for.
$mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem')); $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
foreach ($mail_system as $key => $class) { foreach ($mail_system as $key => $class) {
...@@ -326,7 +330,7 @@ function phpmailer_settings_form_submit($form, &$form_state) { ...@@ -326,7 +330,7 @@ function phpmailer_settings_form_submit($form, &$form_state) {
$settings[] = 'password'; $settings[] = 'password';
} }
foreach ($settings as $setting) { foreach ($settings as $setting) {
if ($form_state['values']['smtp_'. $setting] != variable_get('smtp_'. $setting, '')) { if ($form_state['values']['smtp_' . $setting] != variable_get('smtp_' . $setting, '')) {
watchdog('phpmailer', 'SMTP configuration changed.'); watchdog('phpmailer', 'SMTP configuration changed.');
break; break;
} }
...@@ -334,7 +338,7 @@ function phpmailer_settings_form_submit($form, &$form_state) { ...@@ -334,7 +338,7 @@ function phpmailer_settings_form_submit($form, &$form_state) {
} }
/** /**
* Implementation of hook_mail(). * Implements hook_mail().
*/ */
function phpmailer_mail($key, &$message, $params) { function phpmailer_mail($key, &$message, $params) {
$message['subject'] = t('PHPMailer test e-mail'); $message['subject'] = t('PHPMailer test e-mail');
...@@ -342,7 +346,8 @@ function phpmailer_mail($key, &$message, $params) { ...@@ -342,7 +346,8 @@ function phpmailer_mail($key, &$message, $params) {
} }
/** /**
* Menu callback; Render a HTML mail preview in the browser. * Renders an HTML mail preview in the browser.
*
* @todo Move to Mime Mail project. * @todo Move to Mime Mail project.
*/ */
function phpmailer_preview() { function phpmailer_preview() {
...@@ -352,16 +357,11 @@ function phpmailer_preview() { ...@@ -352,16 +357,11 @@ function phpmailer_preview() {
$GLOBALS['devel_shutdown'] = TRUE; $GLOBALS['devel_shutdown'] = TRUE;
$mailkey = 'phpmailer_preview'; $mailkey = 'phpmailer_preview';
// Use example address to prevent usage of configurable mail format setting.
$recipient = 'test@example.com';
// @see user_register_submit() // @see user_register_submit()
$language = user_preferred_language($user); $language = user_preferred_language($user);
$variables = user_mail_tokens($user, $language); $variables = user_mail_tokens($user, $language);
$variables['!password'] = 'test'; $variables['!password'] = 'test';
$subject = _user_mail_text('register_no_approval_required_subject', $language, $variables);
$body = _user_mail_text('register_no_approval_required_body', $language, $variables); $body = _user_mail_text('register_no_approval_required_body', $language, $variables);
$sender = NULL;
$headers = array();
// Convert non-html messages. // Convert non-html messages.
// @see drupal_mail_wrapper() // @see drupal_mail_wrapper()
...@@ -370,7 +370,7 @@ function phpmailer_preview() { ...@@ -370,7 +370,7 @@ function phpmailer_preview() {
// @see mimemail_prepare() // @see mimemail_prepare()
$body = theme('mimemail_message', $body, $mailkey); $body = theme('mimemail_message', $body, $mailkey);
foreach (module_implements('mail_post_process') as $module) { foreach (module_implements('mail_post_process') as $module) {
$function = $module .'_mail_post_process'; $function = $module . '_mail_post_process';
$function($body, $mailkey); $function($body, $mailkey);
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
/** /**
* Implementation of hook_requirements(). * Implements hook_requirements().
*/ */
function phpmailer_requirements($phase) { function phpmailer_requirements($phase) {
$requirements = array(); $requirements = array();
...@@ -58,24 +58,77 @@ function phpmailer_requirements($phase) { ...@@ -58,24 +58,77 @@ function phpmailer_requirements($phase) {
} }
/** /**
* Implementation of hook_uninstall(). * Implements hook_enable().
*/
function phpmailer_enable() {
if (!phpmailer_enabled() && !(module_exists('mimemail') && variable_get('mimemail_engine', 'mimemail') == 'phpmailer')) {
$t = get_t();
drupal_set_message($t('PHPMailer has been installed, but is currently disabled. <a href="@settings-url">Configure it now</a>.', array('@settings-url' => url('admin/config/system/phpmailer'))));
}
if (module_exists('mailsystem')) {
mailsystem_set(array('phpmailer' => 'DrupalPHPMailer'));
}
}
/**
* Implements hook_disable().
*/
function phpmailer_disable() {
if (phpmailer_enabled()) {
// Remove PHPMailer from all mail keys it is configured for.
$mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
foreach ($mail_system as $key => $class) {
if ($class == 'DrupalPHPMailer') {
if ($key != 'default-system') {
unset($mail_system[$key]);
}
else {
$mail_system[$key] = 'DefaultMailSystem';
}
}
}
variable_set('mail_system', $mail_system);
variable_del('smtp_on');
drupal_set_message(t('PHPMailer has been disabled.'));
}
if (module_exists('mimemail') && variable_get('mimemail_engine', 'mimemail') == 'phpmailer') {
variable_del('mimemail_engine');
drupal_set_message(t('MimeMail e-mail engine has been reset to default.'), 'warning');
}
if (module_exists('mailsystem')) {
mailsystem_clear(array('phpmailer' => 'DrupalPHPMailer'));
}
}
/**
* Implements hook_uninstall().
*/ */
function phpmailer_uninstall() { function phpmailer_uninstall() {
$mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem')); $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
$mail_system['default-system'] = 'DefaultMailSystem'; $mail_system['default-system'] = 'DefaultMailSystem';
variable_set('mail_system', $mail_system); variable_set('mail_system', $mail_system);
variable_del('smtp_on'); variable_del('smtp_always_replyto');
variable_del('smtp_auth_type');
variable_del('smtp_debug');
variable_del('smtp_debug_log');
variable_del('smtp_encode_headers');
variable_del('smtp_fromname');
variable_del('smtp_hide_password');
variable_del('smtp_host'); variable_del('smtp_host');
variable_del('smtp_hostbackup'); variable_del('smtp_hostbackup');
variable_del('smtp_keepalive');
variable_del('smtp_on');
variable_del('smtp_password');
variable_del('smtp_port'); variable_del('smtp_port');
variable_del('smtp_protocol'); variable_del('smtp_protocol');
variable_del('smtp_fromname');
variable_del('smtp_username'); variable_del('smtp_username');
variable_del('smtp_password'); variable_del('ssl_verify_peer');
variable_del('smtp_keepalive'); variable_del('ssl_verify_peer_name');
variable_del('smtp_debug'); variable_del('ssl_allow_self_signed');
variable_del('smtp_debug_log');
} }
/** /**
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
/** /**
* Implementation of hook_perm(). * Implements hook_perm().
*/ */
function phpmailer_permission() { function phpmailer_permission() {
return array( return array(
...@@ -18,7 +18,7 @@ function phpmailer_permission() { ...@@ -18,7 +18,7 @@ function phpmailer_permission() {
} }
/** /**
* Implementation of hook_menu(). * Implements hook_menu().
*/ */
function phpmailer_menu() { function phpmailer_menu() {
$items['admin/config/system/phpmailer'] = array( $items['admin/config/system/phpmailer'] = array(
...@@ -41,7 +41,7 @@ function phpmailer_menu() { ...@@ -41,7 +41,7 @@ function phpmailer_menu() {
} }
/** /**
* Implementation of hook_form_FORM_ID_alter(). * Implements hook_form_FORM_ID_alter().
*/ */
function phpmailer_form_mimemail_admin_settings_alter(&$form, &$form_state) { function phpmailer_form_mimemail_admin_settings_alter(&$form, &$form_state) {
// Hide the Mime Mail global enabler setting if phpmailer is used to deliver // Hide the Mime Mail global enabler setting if phpmailer is used to deliver
...@@ -71,7 +71,7 @@ function phpmailer_enabled() { ...@@ -71,7 +71,7 @@ function phpmailer_enabled() {
} }
/** /**
* Implementation of hook_mailengine(). * Implements hook_mailengine().
*/ */
function phpmailer_mailengine($op, $message = array()) { function phpmailer_mailengine($op, $message = array()) {
if (!phpmailer_library_exists()) { if (!phpmailer_library_exists()) {
...@@ -103,11 +103,11 @@ function phpmailer_mailengine($op, $message = array()) { ...@@ -103,11 +103,11 @@ function phpmailer_mailengine($op, $message = array()) {
/** /**
* Extract address and optional display name of an e-mail address. * Extract address and optional display name of an e-mail address.
* *
* @param $string * @param string $string
* A string containing one or more valid e-mail address(es) separated with * A string containing one or more valid e-mail address(es) separated with
* commas. * commas.
* *
* @return * @return array
* An array containing all found e-mail addresses split into mail and name. * An array containing all found e-mail addresses split into mail and name.
* *
* @see http://tools.ietf.org/html/rfc5322#section-3.4 * @see http://tools.ietf.org/html/rfc5322#section-3.4
...@@ -173,52 +173,6 @@ function phpmailer_preview_access() { ...@@ -173,52 +173,6 @@ function phpmailer_preview_access() {
return FALSE; return FALSE;
} }
/**
* Implementation of hook_enable().
*/
function phpmailer_enable() {
if (!phpmailer_enabled() && !(module_exists('mimemail') && variable_get('mimemail_engine', 'mimemail') == 'phpmailer')) {
$t = get_t();
drupal_set_message($t('PHPMailer has been installed, but is currently disabled. <a href="@settings-url">Configure it now</a>.', array('@settings-url' => url('admin/config/system/phpmailer'))));
}
if (module_exists('mailsystem')) {
mailsystem_set(array('phpmailer' => 'DrupalPHPMailer'));
}
}
/**
* Implementation of hook_disable().
*/
function phpmailer_disable() {
if (phpmailer_enabled()) {
// Remove PHPMailer from all mail keys it is configured for.
$mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
foreach ($mail_system as $key => $class) {
if ($class == 'DrupalPHPMailer') {
if ($key != 'default-system') {
unset($mail_system[$key]);
}
else {
$mail_system[$key] = 'DefaultMailSystem';
}
}
}
variable_set('mail_system', $mail_system);
variable_del('smtp_on');
drupal_set_message(t('PHPMailer has been disabled.'));
}
if (module_exists('mimemail') && variable_get('mimemail_engine', 'mimemail') == 'phpmailer') {
variable_del('mimemail_engine');
drupal_set_message(t('MimeMail e-mail engine has been reset to default.'), 'warning');
}
if (module_exists('mailsystem')) {
mailsystem_clear(array('phpmailer' => 'DrupalPHPMailer'));
}
}
/** /**
* Implements hook_registry_files_alter(). * Implements hook_registry_files_alter().
* *
......
...@@ -5,7 +5,14 @@ ...@@ -5,7 +5,14 @@
* PHPMailer tests. * PHPMailer tests.
*/ */
/**
* Class for testing the PHPMailer module.
*/
class PHPMailerUnitTestCase extends DrupalUnitTestCase { class PHPMailerUnitTestCase extends DrupalUnitTestCase {
/**
* Define this test's meta data.
*/
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'E-mail address parser', 'name' => 'E-mail address parser',
...@@ -14,13 +21,16 @@ class PHPMailerUnitTestCase extends DrupalUnitTestCase { ...@@ -14,13 +21,16 @@ class PHPMailerUnitTestCase extends DrupalUnitTestCase {
); );
} }
/**
* {@inheritDoc}
*/
function setUp() { function setUp() {
parent::setUp(); parent::setUp();
drupal_load('module', 'phpmailer'); drupal_load('module', 'phpmailer');
} }
/** /**
* Test e-mail address extraction using phpmailer_parse_address(). * Tests e-mail address extraction using phpmailer_parse_address().
*/ */
function testAddressParser() { function testAddressParser() {
// Set up various test addresses according to RFC 5322. // Set up various test addresses according to RFC 5322.
...@@ -28,42 +38,42 @@ class PHPMailerUnitTestCase extends DrupalUnitTestCase { ...@@ -28,42 +38,42 @@ class PHPMailerUnitTestCase extends DrupalUnitTestCase {
// addr-spec. // addr-spec.
array( array(
'mail' => 'user-1@domain.tld', 'mail' => 'user-1@domain.tld',
'name' => '' 'name' => '',
), ),
// Invalid but supported angle-addr without preceding display-name. // Invalid but supported angle-addr without preceding display-name.
'<user-2@domain.tld>' => array( '<user-2@domain.tld>' => array(
'mail' => 'user-2@domain.tld', 'mail' => 'user-2@domain.tld',
'name' => '' 'name' => '',
), ),
// Unquoted atom name-addr. // Unquoted atom name-addr.
'John Doe <user-3@domain.tld>' => array( 'John Doe <user-3@domain.tld>' => array(
'mail' => 'user-3@domain.tld', 'mail' => 'user-3@domain.tld',
'name' => 'John Doe' 'name' => 'John Doe',
), ),
// Quoted atom name-addr. // Quoted atom name-addr.
'"John Doe" <user-4@domain.tld>' => array( '"John Doe" <user-4@domain.tld>' => array(
'mail' => 'user-4@domain.tld', 'mail' => 'user-4@domain.tld',
'name' => 'John Doe' 'name' => 'John Doe',
), ),
// name-addr with a quoted-string in display-name. // name-addr with a quoted-string in display-name.
array( array(
'mail' => 'user-5@domain.tld', 'mail' => 'user-5@domain.tld',
'name' => 'John "The Dude" Doe' 'name' => 'John "The Dude" Doe',
), ),
// name-addr with a quoted-string and comma in display-name. // name-addr with a quoted-string and comma in display-name.
array( array(
'mail' => 'user-6@domain.tld', 'mail' => 'user-6@domain.tld',
'name' => 'John "The Dude" Doe (Foo, Bar)' 'name' => 'John "The Dude" Doe (Foo, Bar)',
), ),
// name-addr containing non-ASCII chars in display-name. // name-addr containing non-ASCII chars in display-name.
array( array(
'mail' => 'user-7@domain.tld', 'mail' => 'user-7@domain.tld',
'name' => 'Jöhn "The Düde" Döe' 'name' => 'Jöhn "The Düde" Döe',
), ),
); );
$all = array(); $all = array();
// Validate each address format is correctly parsed. // Validates each address format is correctly parsed.
foreach ($addresses as $test => $address) { foreach ($addresses as $test => $address) {
if (is_numeric($test)) { if (is_numeric($test)) {
if ($address['name'] != '') { if ($address['name'] != '') {
...@@ -75,7 +85,12 @@ class PHPMailerUnitTestCase extends DrupalUnitTestCase { ...@@ -75,7 +85,12 @@ class PHPMailerUnitTestCase extends DrupalUnitTestCase {
} }
} }
$result = phpmailer_parse_address($test); $result = phpmailer_parse_address($test);
$this->assertEqual($result[0], $address, t('Successfully extracted %email, %name from %address.', array('%email' => $result[0]['mail'], '%name' => $result[0]['name'] ? $result[0]['name'] : '(blank)', '%address' => $test)), 'PHPMailer'); $replacements = array(
'%email' => $result[0]['mail'],
'%name' => $result[0]['name'] ? $result[0]['name'] : '(blank)',
'%address' => $test,
);
$this->assertEqual($result[0], $address, t('Successfully extracted %email, %name from %address.', $replacements), 'PHPMailer');
$all[] = $test; $all[] = $test;
} }
...@@ -84,5 +99,5 @@ class PHPMailerUnitTestCase extends DrupalUnitTestCase { ...@@ -84,5 +99,5 @@ class PHPMailerUnitTestCase extends DrupalUnitTestCase {
$expected_result = array_values($addresses); $expected_result = array_values($addresses);
$this->assertEqual($result, $expected_result, t('All concatenated e-mail addresses could be extracted.'), 'PHPMailer'); $this->assertEqual($result, $expected_result, t('All concatenated e-mail addresses could be extracted.'), 'PHPMailer');
} }
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment