You are here

public function MandrillMailSystem::mail in Mandrill 6

Same name and namespace in other branches
  1. 7.2 lib/mandrill.mail.inc \MandrillMailSystem::mail()
  2. 7 lib/mandrill.mail.inc \MandrillMailSystem::mail()

Send the email message.

Parameters

$message: A message array, as described in hook_mail_alter().

Return value

TRUE if the mail was successfully accepted, otherwise FALSE.

See also

drupal_mail()

1 method overrides MandrillMailSystem::mail()
TestingMandrillMailSystem::mail in ./mandrill.mail.inc
Accept an e-mail message and store it in a variable.

File

./mandrill.mail.inc, line 53
Class for sending mails via Mandrill.

Class

MandrillMailSystem
Modify the drupal mail system to use Mandrill when sending emails.

Code

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;
  }

  // send the email passing the message id as the tag for use in reporting
  $mailer = mandrill_get_api_object();

  // apply input format to body
  $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);
      }
    }
  }

  // determine if content should be available for this message
  $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,
    // optional extra headers to add to the message (currently only Reply-To and X-* headers are allowed)
    'headers' => $message['headers'],
    'track_opens' => variable_get('mandrill_track_opens', TRUE),
    'track_clicks' => variable_get('mandrill_track_clicks', TRUE),
    // we're handling this with drupal_html_to_text().
    '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);

    // @todo: look for rejected messages and log
    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;
  }
}