View source
<?php
function _mandrill_remove_newlines($matches) {
return str_replace(array(
"\n",
"\r",
), ' ', $matches[0]);
}
class MandrillMailSystem {
public function format(array $message) {
if (is_array($message['body'])) {
$message['body'] = implode("\n\n", $message['body']);
}
$message['body'] = preg_replace_callback('/<[^<>]*>/s', '_mandrill_remove_newlines', $message['body']);
return $message;
}
public function mail(array $message) {
if (!($from = variable_get('mandrill_from', ''))) {
drupal_set_message(t('Mandrill can\'t send email. Please !link.', array(
'!link' => l('add a verified from address', 'admin/settings/mandrill'),
)), 'error');
return FALSE;
}
$mailer = mandrill_get_api_object();
$html = $message['body'];
$format = variable_get('mandrill_filter_format', '');
if (!empty($format)) {
$html = check_markup($message['body'], $format, FALSE);
}
$to = mandrill_get_to($message['to']);
$attachments = array();
if (isset($message['attachments']) && !empty($message['attachments'])) {
foreach ($message['attachments'] as $attachment) {
if (is_file($attachment)) {
$attachments[] = $mailer
->getAttachmentStruct($attachment);
}
}
}
$blacklisted_keys = explode(',', mandrill_mail_key_blacklist());
$view_content = TRUE;
foreach ($blacklisted_keys as $key) {
if ($message['id'] == drupal_strtolower(trim($key))) {
$view_content = FALSE;
break;
}
}
$mandrill_message = array(
'html' => $html,
'text' => drupal_html_to_text($message['body']),
'subject' => $message['subject'],
'from_name' => variable_get('mandrill_from_name', ''),
'from_email' => $from,
'to' => $to,
'headers' => $message['headers'],
'track_opens' => variable_get('mandrill_track_opens', TRUE),
'track_clicks' => variable_get('mandrill_track_clicks', TRUE),
'auto_text' => FALSE,
'url_strip_qs' => variable_get('mandrill_url_strip_qs', FALSE),
'bcc_address' => isset($message['bcc_email']) ? $message['bcc_email'] : NULL,
'tags' => array(
$message['id'],
),
'google_analytics_domains' => variable_get('mandrill_analytics_domains', NULL) ? explode(',', variable_get('mandrill_analytics_domains', NULL)) : array(),
'google_analytics_campaign' => variable_get('mandrill_analytics_campaign', ''),
'attachments' => $attachments,
'view_content_link' => $view_content,
);
drupal_alter('mandrill_mail', $mandrill_message, $message);
try {
$result = $mailer
->messages_send($mandrill_message);
return TRUE;
} catch (Mandrill_Exception $e) {
watchdog('mandrill', 'Error sending email from %from to %to. @code: @message', array(
'%from' => $from,
'%to' => $message['to'],
'@code' => $e
->getCode(),
'@message' => $e
->getMessage(),
), WATCHDOG_ERROR);
return FALSE;
}
}
}
class TestingMandrillMailSystem extends MandrillMailSystem {
public function mail(array $message) {
if (!($from = variable_get('mandrill_from', ''))) {
drupal_set_message(t('Mandrill can\'t send email. Please !link.', array(
'!link' => l('add a verified from address', 'admin/settings/mandrill'),
)), 'error');
return FALSE;
}
$to = mandrill_get_to($message['to']);
watchdog('mandrill', 'Mandrill test email sent from %from to %to. Message: <pre>@message</pre>', array(
'%from' => $from,
'%to' => $to[0]['email'],
'@message' => print_r($message, TRUE),
), WATCHDOG_NOTICE);
drupal_set_message(t('Mandrill test email sent from %from to %to.', array(
'%from' => $from,
'%to' => $to[0]['email'],
)), 'notice');
return TRUE;
}
}
function mandrill_get_to($to) {
$recipients = array();
if ($to_array = explode(',', $to)) {
foreach ($to_array as $email) {
$recipients[] = array(
'email' => $email,
);
}
}
else {
$recipients[] = array(
'email' => $to,
);
}
return $recipients;
}
function drupal_mail_wrapper($message) {
$mandrill_status = variable_get('mandrill_status', MANDRILL_STATUS_ON);
$mandrill = $mandrill_status == MANDRILL_STATUS_TEST ? new TestingMandrillMailSystem() : new MandrillMailSystem();
$message = $mandrill
->format($message);
return $mandrill
->mail($message);
}