phpmailer.module in PHPMailer 5.2
Same filename and directory in other branches
Integrates the PHPMailer library for SMTP e-mail delivery.
File
phpmailer.moduleView source
<?php
/**
* @file
* Integrates the PHPMailer library for SMTP e-mail delivery.
*/
/**
* Implementation of hook_perm().
*/
function phpmailer_perm() {
return array(
'administer phpmailer settings',
);
}
/**
* Implementation of hook_menu().
*/
function phpmailer_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/phpmailer',
'title' => t('PHPMailer'),
'description' => t('Configure PHPMailer settings.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'phpmailer_settings',
),
'access' => phpmailer_settings_access(),
);
$items[] = array(
'path' => 'phpmailer/preview',
'title' => t('Mail preview'),
'callback' => 'phpmailer_preview',
'access' => phpmailer_preview_access(),
'type' => MENU_CALLBACK,
);
}
return $items;
}
/**
* Implementation of hook_form_alter().
*/
function phpmailer_form_alter($form_id, &$form) {
if ($form_id == 'mimemail_settings') {
// Hide the mimemail global enabler setting if phpmailer is used to deliver
// all e-mails (they can't be both active).
if (phpmailer_enabled()) {
$form['mimemail']['mimemail_alter'] = array(
'#type' => 'value',
'#value' => 0,
);
}
// Add custom validation and submit handlers.
$form['#validate']['phpmailer_settings_form_validate'] = array();
$form['#submit']['phpmailer_settings_form_submit'] = array();
}
}
/**
* Determine if PHPMailer is used to deliver e-mails.
*/
function phpmailer_enabled() {
return strpos(variable_get('smtp_library', ''), 'phpmailer');
}
if (phpmailer_enabled() && !function_exists('drupal_mail_wrapper')) {
/**
* Implementation of drupal_mail_wrapper().
*/
function drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $headers) {
require_once drupal_get_path('module', 'phpmailer') . '/includes/phpmailer.drupal.inc';
return phpmailer_send($to, $subject, $body, $from, $headers);
}
}
/**
* Implementation of hook_mailengine().
*/
function phpmailer_mailengine($op, $message = array()) {
switch ($op) {
case 'name':
return t('PHPMailer');
case 'description':
return t('Mailing engine using the PHPMailer library.');
case 'settings':
require_once drupal_get_path('module', 'phpmailer') . '/phpmailer.admin.inc';
return phpmailer_settings_form();
case 'multiple':
case 'single':
case 'send':
require_once drupal_get_path('module', 'phpmailer') . '/includes/phpmailer.mimemail.inc';
return mimemail_phpmailer_send($message);
}
}
/**
* Extract address and optional display name of an e-mail address.
*
* @param $string
* A string containing one or more valid e-mail address(es) separated with
* commas.
*
* @return
* An array containing all found e-mail addresses split into mail and name.
*
* @see http://tools.ietf.org/html/rfc5322#section-3.4
*/
function phpmailer_parse_address($string) {
$parsed = array();
// The display name may contain commas (3.4). Extract all quoted strings
// (3.2.4) to a stack and replace them with a placeholder to prevent
// splitting at wrong places.
$string = preg_replace('/(".*?(?<!\\\\)")/e', '_phpmailer_stack("$1")', $string);
// Build a regex that matches a name-addr (3.4).
// @see valid_email_address()
$user = '[a-zA-Z0-9_\\-\\.\\+\\^!#\\$%&*+\\/\\=\\?\\`\\|\\{\\}~\']+';
$domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.?)+';
$ipv4 = '[0-9]{1,3}(?:\\.[0-9]{1,3}){3}';
$ipv6 = '[0-9a-fA-F]{1,4}(?:\\:[0-9a-fA-F]{1,4}){7}';
$address = "{$user}@(?:{$domain}|(?:\\[(?:{$ipv4}|{$ipv6})\\]))";
$adr_rx = "/^(?<name>.*)\\s<(?<address>{$address})>\$/";
// Split string into multiple parts and process each.
foreach (explode(',', $string) as $email) {
// Re-inject stripped placeholders.
$email = preg_replace('/\\x01/e', '_phpmailer_stack()', trim($email));
// Check if it's a name-addr or a plain address (3.4).
if (preg_match($adr_rx, $email, $matches)) {
$parsed[] = array(
'mail' => $matches['address'],
'name' => $matches['name'],
);
}
else {
$parsed[] = array(
'mail' => trim($email, '<>'),
'name' => '',
);
}
}
return $parsed;
}
/**
* Implements a FIFO stack to store extracted quoted strings.
*/
function _phpmailer_stack($string = NULL) {
static $stack = array();
if (!isset($string)) {
// Unescape quoted characters (3.2.4) to prevent double escaping.
return str_replace(array(
'\\"',
'\\\\',
), array(
'"',
'\\',
), array_shift($stack));
}
// Remove surrounding quotes and push on stack.
array_push($stack, substr($string, 1, -1));
// Return placeholder substitution. 0x01 may never appear outside a quoted
// string (3.2.3).
return "\1";
}
/**
* Menu callback to render the settings page.
*/
function phpmailer_settings() {
require_once drupal_get_path('module', 'phpmailer') . '/phpmailer.admin.inc';
return phpmailer_settings_form();
}
/**
* Block access to settings page if Mime Mail module is enabled.
*/
function phpmailer_settings_access() {
if (module_exists('mimemail')) {
return FALSE;
}
return user_access('administer phpmailer settings');
}
/**
* Menu access callback; Determine access for HTML mail preview page.
*/
function phpmailer_preview_access() {
if (module_exists('mimemail')) {
return user_access('administer phpmailer settings');
}
return FALSE;
}
/**
* Implementation of hook_disable().
*/
function phpmailer_disable() {
if (phpmailer_enabled()) {
variable_del('smtp_library');
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.'), 'warning');
}
}
/**
* Menu callback; Render a HTML mail as preview in the browser.
*/
function phpmailer_preview() {
global $user, $base_url;
// Suppress devel output in preview.
$GLOBALS['devel_shutdown'] = TRUE;
$mailkey = 'phpmailer-test';
// Use example address to prevent usage of configurable mail format setting.
$recipient = 'test@example.com';
// @see user_register_submit()
$variables = array(
'!username' => $user->name,
'!site' => variable_get('site_name', 'Drupal'),
'!password' => 'test',
'!uri' => $base_url,
'!uri_brief' => substr($base_url, strlen('http://')),
'!mailto' => $user->mail,
'!date' => format_date(time()),
'!login_uri' => url('user', NULL, NULL, TRUE),
'!edit_uri' => url('user/' . $user->uid . '/edit', NULL, NULL, TRUE),
'!login_url' => user_pass_reset_url($user),
);
$subject = _user_mail_text('welcome_subject', $variables);
$body = _user_mail_text('welcome_body', $variables);
$sender = NULL;
$headers = array();
mimemail_mail_alter($mailkey, $recipient, $subject, $body, $sender, $headers);
print theme('mimemail_message', $body, $mailkey);
exit;
}
Functions
Name![]() |
Description |
---|---|
phpmailer_disable | Implementation of hook_disable(). |
phpmailer_enabled | Determine if PHPMailer is used to deliver e-mails. |
phpmailer_form_alter | Implementation of hook_form_alter(). |
phpmailer_mailengine | Implementation of hook_mailengine(). |
phpmailer_menu | Implementation of hook_menu(). |
phpmailer_parse_address | Extract address and optional display name of an e-mail address. |
phpmailer_perm | Implementation of hook_perm(). |
phpmailer_preview | Menu callback; Render a HTML mail as preview in the browser. |
phpmailer_preview_access | Menu access callback; Determine access for HTML mail preview page. |
phpmailer_settings | Menu callback to render the settings page. |
phpmailer_settings_access | Block access to settings page if Mime Mail module is enabled. |
_phpmailer_stack | Implements a FIFO stack to store extracted quoted strings. |