phpmailer_smtp.module in PHPMailer SMTP 2.x
Same filename and directory in other branches
Uses the PHPMailer library to send emails via SMTP.
File
phpmailer_smtp.moduleView source
<?php
/**
* @file
* Uses the PHPMailer library to send emails via SMTP.
*/
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function phpmailer_smtp_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.phpmailer_smtp':
$text = file_get_contents(__DIR__ . '/README.txt');
return '<pre>' . Html::escape($text) . '</pre>';
default:
}
}
/**
* Implements hook_mail().
*/
function phpmailer_smtp_mail($key, &$message, $params) {
switch ($key) {
case 'test':
$message['subject'] = (string) t('PHPMailer SMTP test email');
$message['body'][] = (string) t('Your site is properly configured to send emails using the PHPMailer library.');
break;
}
}
/**
* Extract address and optional display name of an e-mail address.
*
* @param string $string
* A string containing one or more valid e-mail address(es) separated with
* commas.
*
* @return array
* 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_smtp_parse_address($string) {
$parsed = [];
// 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_callback('(".*?(?<!\\\\)")', '_phpmailer_smtp_stack', $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 = "/^(?P<name>.*)\\s<(?P<address>{$address})>\$/";
// Split string into multiple parts and process each.
foreach (explode(',', $string) as $email) {
// Re-inject stripped placeholders.
$email = preg_replace_callback('(\\x01)', '_phpmailer_smtp_stack', trim($email));
// Check if it's a name-addr or a plain address (3.4).
if (preg_match($adr_rx, $email, $matches)) {
// PHPMailer expects an unencoded display name.
$parsed[] = [
'mail' => $matches['address'],
'name' => Unicode::mimeHeaderDecode(stripslashes($matches['name'])),
];
}
else {
$parsed[] = [
'mail' => trim($email, '<>'),
'name' => '',
];
}
}
return $parsed;
}
/**
* Implements a FIFO stack to store extracted quoted strings.
*/
function _phpmailer_smtp_stack($matches = NULL) {
$string = $matches[0];
static $stack = [];
if ($string == "\1") {
// Unescape quoted characters (3.2.4) to prevent double escaping.
return str_replace([
'\\"',
'\\\\',
], [
'"',
'\\',
], 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";
}
Functions
Name | Description |
---|---|
phpmailer_smtp_help | Implements hook_help(). |
phpmailer_smtp_mail | Implements hook_mail(). |
phpmailer_smtp_parse_address | Extract address and optional display name of an e-mail address. |
_phpmailer_smtp_stack | Implements a FIFO stack to store extracted quoted strings. |