View source
<?php
define('MANDRILL_QUEUE', 'mandrill_queue');
define('MANDRILL_EMAIL_REGEX', '/^\\s*(.+?)\\s*<\\s*([^>]+)\\s*>$/');
function mandrill_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/help#mandrill':
$output = t('Allow for site emails to be sent through Mandrill.');
}
return $output;
}
function mandrill_menu() {
$items = array();
$items['admin/config/services/mandrill'] = array(
'title' => 'Mandrill',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mandrill_admin_settings',
),
'access arguments' => array(
'administer mandrill',
),
'description' => 'Send emails through the Mandrill transactional email service.',
'file' => 'mandrill.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/services/mandrill/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['admin/config/services/mandrill/test'] = array(
'title' => 'Send test email',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mandrill_test_form',
),
'access callback' => 'mandrill_test_access',
'description' => 'Send a test email using the Mandrill API.',
'file' => 'mandrill.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
return $items;
}
function mandrill_libraries_info() {
$libraries['mandrill'] = array(
'name' => 'Mandrill API',
'vendor url' => 'https://mandrillapp.com/api/docs',
'download url' => 'https://bitbucket.org/mailchimp/mandrill-api-php/get/1.0.52.tar.gz',
'path' => 'src',
'version arguments' => array(
'file' => 'composer.json',
'pattern' => '/\\"version": \\"((\\d+)\\.(\\d+)\\.(\\d+))\\",/',
),
'files' => array(
'php' => array(
'Mandrill.php',
),
),
);
return $libraries;
}
function mandrill_test_access() {
$a = user_access('administer mandrill');
$b = variable_get('mandrill_api_key');
return $a & !empty($b);
}
function mandrill_permission() {
return array(
'administer mandrill' => array(
'title' => t('Administer Mandrill'),
'description' => t('Perform administration tasks for the Mandrill email service.'),
"restrict access" => TRUE,
),
);
}
function mandrill_cron_queue_info() {
$queues = array();
$queues[MANDRILL_QUEUE] = array(
'worker callback' => 'mandrill_queue_worker_mailsend',
'time' => variable_get('mandrill_queue_worker_timeout', 15),
);
return $queues;
}
function mandrill_queue_worker_mailsend($data) {
mandrill_mailsend($data['message'], $data['function'], $data['args']);
}
function mandrill_mail($key, &$message, $params) {
if ($key == 'test') {
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
if ($params['include_attachment']) {
$message['attachments'][] = drupal_realpath('misc/druplicon.png');
$message['body'] .= ' ' . t('The Drupal icon is included as an attachment to test the attachment functionality.');
}
}
}
function mandrill_mailsend($message, $function, $args = array()) {
try {
if (!function_exists($function)) {
watchdog('mandrill', 'Error sending email from %from to %to. Function %function not found.', array(
'%from' => $message['from_email'],
'%to' => $message['to'][0]['email'],
'%function' => $function,
), WATCHDOG_ERROR);
return FALSE;
}
$params = array(
$message,
) + $args;
$response = call_user_func_array($function, $params);
if (!isset($response['status'])) {
foreach ($response as $result) {
module_invoke_all('mandrill_mailsend_result', $result, $params);
switch ($result['status']) {
case "error":
case "invalid":
case "rejected":
if (!variable_get('mandrill_test_mode')) {
$to = isset($result['email']) ? $result['email'] : 'recipient';
$status = isset($result['status']) ? $result['status'] : 'message';
$error_message = isset($result['message']) ? $result['message'] : 'no message';
$debug = print_r($result, TRUE);
$severity = WATCHDOG_ERROR;
$return = FALSE;
if (isset($result['reject_reason']) && in_array($result['reject_reason'], array(
'hard-bounce',
'soft-bounce',
'spam',
))) {
$severity = WATCHDOG_NOTICE;
$return = TRUE;
}
watchdog('mandrill', 'Failed sending email from %from to %to. @status: @message <pre>@debug</pre>', array(
'%from' => $message['from_email'],
'%to' => $to,
'@status' => $status,
'@message' => $error_message,
'@debug' => $debug,
), $severity);
return $return;
}
return FALSE;
case "queued":
watchdog('mandrill', 'Email from %from to %to queued by Mandrill App.', array(
'%from' => $message['from_email'],
'%to' => $result['email'],
), WATCHDOG_INFO);
break;
}
}
}
else {
watchdog('mandrill', 'Mail send failed with status %status: code %code, %name, %message', array(
'%status' => $response['status'],
'%code' => $response['code'],
'%name' => $response['name'],
'%message' => $response['message'],
), WATCHDOG_WARNING);
return FALSE;
}
return $response;
} catch (Mandrill_Error $e) {
watchdog('mandrill', 'Error sending email from %from to %to. @code: @message', array(
'%from' => $message['from_email'],
'%to' => $message['to'],
'@code' => $e
->getCode(),
'@message' => $e
->getMessage(),
), WATCHDOG_ERROR);
return FALSE;
}
}
function mandrill_sender_plain($message) {
if ($mailer = mandrill_get_api_object()) {
return $mailer->messages
->send($message);
}
else {
throw new Mandrill_Error('Missing API key.');
}
}
function mandrill_get_api_object($reset = FALSE) {
$api =& drupal_static(__FUNCTION__, NULL);
if ($api === NULL || $reset === TRUE) {
$library = libraries_load('mandrill');
if (!$library['installed']) {
$msg = t('Failed to load Mandrill PHP library. Please refer to the installation requirements.');
watchdog('mandrill', $msg, NULL, WATCHDOG_ERROR);
drupal_set_message($msg, 'error');
return NULL;
}
$api_key = variable_get('mandrill_api_key', '');
$api_timeout = variable_get('mandrill_api_timeout', 60);
if (empty($api_key)) {
$msg = t('Failed to load Mandrill API Key. Please check your Mandrill settings.');
drupal_set_message($msg, 'error');
return FALSE;
}
$classname = variable_get('mandrill_api_classname', 'DrupalMandrill');
$api = new $classname($api_key, $api_timeout);
}
return $api;
}
function mandrill_get_module_key_names() {
$name_array = array(
'' => '--none--',
'default-system' => "Site-wide default",
);
$descriptions = array();
foreach (system_rebuild_module_data() as $item) {
if ($item->status) {
$descriptions[$item->name] = (empty($item->info['package']) ? '' : $item->info['package']) . ' » ' . t('!module module', array(
'!module' => $item->info['name'],
));
}
}
asort($descriptions);
$mailsystem_settings = mailsystem_get();
unset($mailsystem_settings['default-system']);
foreach ($mailsystem_settings as $id => $class) {
$module = $id;
while ($module && empty($descriptions[$module])) {
$module = implode('_', explode('_', $module, -1));
}
if (empty($module)) {
}
$title = preg_replace('/^.* » /', '', $descriptions[$module]);
if ($key = substr($id, strlen($module) + 1)) {
$title .= " ({$key} key)";
}
$name_array[$id] = $title;
}
return $name_array;
}
function mandrill_prepare_message($message) {
$module = $message['module'];
$key = $message['key'];
$to = $message['to'];
$subject = $message['subject'];
$body = $message['body'];
$hook = [
'mandrill_message__' . $key,
'mandrill_message__' . $module . '__' . $key,
];
$variables = [
'module' => $module,
'key' => $key,
'recipient' => $to,
'subject' => $subject,
'body' => $body,
];
$message['params']['plaintext'] = drupal_html_to_text($body);
$message['body'] = theme($hook, $variables);
return $message;
}
function mandrill_theme() {
$path = drupal_get_path('module', 'mandrill') . '/theme';
return [
'mandrill_message' => [
'variables' => [
'module' => NULL,
'key' => NULL,
'recipient' => NULL,
'subject' => NULL,
'body' => NULL,
],
'template' => 'mandrill-message',
'pattern' => 'mandrill_message__',
'file' => 'mandrill.theme.inc',
'mail theme' => TRUE,
'path' => $path,
],
];
}
function mandrill_get_templates() {
$templates = NULL;
try {
if ($mailer = mandrill_get_api_object()) {
$templates = $mailer->templates
->getList();
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $templates;
}
function mandrill_get_subaccounts() {
$subaccounts = array();
try {
if ($mandrill = mandrill_get_api_object()) {
$subaccounts = $mandrill->subaccounts
->getList();
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $subaccounts;
}
function mandrill_get_webhooks() {
$webhooks = array();
try {
if ($mandrill = mandrill_get_api_object()) {
$webhooks = $mandrill->webhooks
->getList();
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $webhooks;
}
function mandrill_get_inbound_domains() {
$domains = array();
try {
if ($mandrill = mandrill_get_api_object()) {
$domains = $mandrill->inbound
->domains();
}
else {
$domains = FALSE;
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $domains;
}
function mandrill_get_inbound_routes($domain) {
$routes = array();
try {
if ($mandrill = mandrill_get_api_object()) {
$routes = $mandrill->inbound
->routes($domain);
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $routes;
}
function mandrill_add_inbound_domain($domain) {
$result = NULL;
try {
if ($mandrill = mandrill_get_api_object()) {
$result = $mandrill->inbound
->addDomain($domain);
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $result;
}
function mandrill_add_webhook($path, $events, $description = 'Drupal Webhook') {
$result = NULL;
try {
if ($mandrill = mandrill_get_api_object()) {
$result = $mandrill->webhooks
->add($GLOBALS['base_url'] . '/' . $path, $description, $events);
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $result;
}
function mandrill_update_webhook($id, $path, $events, $description = 'Drupal Webhook') {
$result = NULL;
try {
if ($mandrill = mandrill_get_api_object()) {
$result = $mandrill->webhooks
->update($id, $GLOBALS['base_url'] . '/' . $path, $description, $events);
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $result;
}
function mandrill_delete_webhook($webhook_id) {
$result = NULL;
try {
if ($mandrill = mandrill_get_api_object()) {
$result = $mandrill->webhooks
->delete($webhook_id);
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $result;
}
function mandrill_delete_inbound_domain($domain) {
$result = NULL;
try {
if ($mandrill = mandrill_get_api_object()) {
$result = $mandrill->inbound
->deleteDomain($domain);
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $result;
}
function mandrill_add_inbound_route($domain, $pattern, $url) {
$route = NULL;
try {
if ($mandrill = mandrill_get_api_object()) {
$route = $mandrill->inbound
->addRoute($domain, $pattern, $url);
}
} catch (Mandrill_Error $e) {
drupal_set_message(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)), 'error');
watchdog_exception('mandrill', $e);
}
return $route;
}
function mandrill_mail_key_blacklist() {
return variable_get('mandrill_mail_key_blacklist', 'user_password_reset');
}
function mandrill_get_to($message) {
$recipients = mandrill_map_mail_recipients($message['to']);
if (!empty($message['headers']['Cc'])) {
$recipients = array_merge($recipients, mandrill_map_mail_recipients($message['headers']['Cc'], 'cc'));
unset($message['headers']['Cc']);
}
return $recipients;
}
function mandrill_map_mail_recipients($emails, $type = 'to') {
$recipients = array();
$email_array = explode(',', $emails);
foreach ($email_array as $email) {
if (preg_match(MANDRILL_EMAIL_REGEX, $email, $matches)) {
$recipients[] = array(
'email' => $matches[2],
'name' => $matches[1],
'type' => $type,
);
}
else {
$recipients[] = array(
'email' => $email,
'type' => $type,
);
}
}
return $recipients;
}
function mandrill_process_async() {
return variable_get('mandrill_process_async', FALSE);
}
function mandrill_from($from = '') {
$default = array();
$provided = array();
$default_from = variable_get('site_mail', ini_get('sendmail_from'));
$default['email'] = variable_get('mandrill_from', $default_from);
$default['name'] = variable_get('mandrill_from_name', variable_get('site_name'));
if (preg_match_all('/(?:"?([^"]*)"?\\s)?(?:<?(.+@[^>]+)>?)/', $from, $matches)) {
if (isset($matches[1][0]) && !empty($matches[1][0])) {
$provided['name'] = trim($matches[1][0]);
}
if (isset($matches[2][0]) && !empty($matches[2][0])) {
$provided['email'] = $matches[2][0];
}
}
return array_merge($default, $provided);
}
function mandrill_form_mailsystem_admin_settings_alter(&$form, &$form_state, $form_id) {
$form['mailsystem']['mailsystem_theme']['#description'] .= t(" If using MandrillHtmlMailSystem above, you can control your email theming by copying file sites/all/modules/mandrill/theme/mandrill-message.tpl.php into your theme's templates directory. For example to sites/all/themes/theme-name/templates.");
}