You are here

public function MandrillMailSystem::mail in Mandrill 7.2

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

Send the email message.

Parameters

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

Return value

bool TRUE if the mail was successfully accepted, otherwise FALSE.

Overrides MailSystemInterface::mail

See also

drupal_mail()

File

lib/mandrill.mail.inc, line 46
Implements Mandrill as a Drupal MailSystemInterface

Class

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

Code

public function mail(array $message) {

  // Optionally log mail keys not using Mandrill already. Helpful in
  // configuring Mandrill.
  if (variable_get('mandrill_log_defaulted_sends', FALSE)) {
    $systems = mailsystem_get();
    $registered = FALSE;
    foreach ($systems as $key => $system) {
      if ($message['id'] == $key) {
        $registered = TRUE;
      }
      if (!$registered) {
        watchdog('mandrill', "Module: %module Key: %key invoked Mandrill to send email because Mandrill is configured as the default mail system. Specify alternate configuration for this module & key in !mailsystem if this is not desirable.", array(
          '%module' => $message['module'],
          '%key' => $message['key'],
          '!mailsystem' => l(t('Mail System'), 'admin/config/system/mailsystem'),
        ), WATCHDOG_INFO);
      }
    }
  }

  // Extract an array of recipients.
  $to = mandrill_get_to($message);

  // Work through bcc emails.
  if (!empty($message['headers']['Bcc'])) {

    // Parse emails & turn comma-separated string into array.
    $bcc_recipients = mandrill_map_mail_recipients($message['headers']['Bcc'], 'bcc');

    // If 'bcc_email' wasn't explicitly set within the code & we have only
    // one bcc recipient then we can use Mandrill's 'bcc_email' argument
    // to set bcc recipient properly.
    if (empty($message['bcc_email']) && count($bcc_recipients) == 1) {
      $message['bcc_email'] = $bcc_recipients[0]['email'];
    }
    else {
      $to = array_merge($to, $bcc_recipients);
    }
  }

  // Prepare headers, defaulting the reply-to to the from address since
  // Mandrill needs the from address to be configured separately.
  // Note that only Reply-To and X-* headers are allowed.
  $headers = isset($message['headers']) ? $message['headers'] : array();
  if (isset($message['params']['mandrill']['header'])) {
    $headers = $message['params']['mandrill']['header'] + $headers;
  }
  if (!empty($message['from']) && empty($headers['Reply-To'])) {
    $headers['Reply-To'] = $message['from'];
  }

  // Prepare attachments.
  $attachments = array();
  if (isset($message['attachments']) && !empty($message['attachments'])) {
    foreach ($message['attachments'] as $attachment) {
      if (is_file($attachment)) {
        try {
          $attachments[] = $this
            ->getAttachmentStruct($attachment);
        } catch (\Exception $e) {
          $attachments[] = NULL;
          watchdog('php', 'Error message: %error\\n%object', array(
            '%error' => $e
              ->getMessage() . ' Email was sent without this attachment: ' . $attachment . '\\n File: ' . $e
              ->getFile() . ' Line: ' . $e
              ->getLine(),
            '%object' => 'Object: ' . var_export($attachment, TRUE),
          ), WATCHDOG_ERROR);
        }
      }
    }
  }

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

  // The Mime Mail module (mimemail) expects attachments as an array of file
  // arrays in $message['params']['attachments']. As many modules assume you
  // will be using Mime Mail to handle attachments, we need to parse this
  // array as well.
  if (isset($message['params']['attachments']) && !empty($message['params']['attachments'])) {
    foreach ($message['params']['attachments'] as $attachment) {
      if (isset($attachment['uri']) || isset($attachment['filepath'])) {
        if (isset($attachment['uri'])) {
          $attachment_path = $attachment['uri'];
        }
        else {
          $attachment_path = $attachment['filepath'];
        }
        $attachment_path = drupal_realpath($attachment_path);
        if (is_file($attachment_path)) {
          $struct = $this
            ->getAttachmentStruct($attachment_path);

          // Allow for customised filenames.
          if (!empty($attachment['filename'])) {
            $struct['name'] = $attachment['filename'];
          }
          $attachments[] = $struct;
        }
      }
      elseif (isset($attachment['filecontent'])) {
        $attachments[] = array(
          'type' => $attachment['filemime'],
          'name' => $attachment['filename'],
          'content' => chunk_split(base64_encode($attachment['filecontent']), 76, "\n"),
        );
      }
    }

    // Remove the file objects from $message['params']['attachments'].
    // (This prevents double-attaching in the drupal_alter hook below.)
    unset($message['params']['attachments']);
  }

  // Account for the plaintext parameter provided by the mimemail module.
  $plain_text = empty($message['params']['plaintext']) ? drupal_html_to_text($message['body']) : $message['params']['plaintext'];

  // Get metadata.
  $metadata = isset($message['metadata']) ? $message['metadata'] : array();

  // Use sender information from the Mandrill configuration.
  // This ensures the sending domain is correct.
  // If provided, the custom sender email address is in the Reply-To header.
  $from = mandrill_from();
  $overrides = isset($message['params']['mandrill']['overrides']) ? $message['params']['mandrill']['overrides'] : array();
  $mandrill_message = $overrides + array(
    'html' => $message['body'],
    'text' => $plain_text,
    'subject' => $message['subject'],
    'from_email' => $from['email'],
    'from_name' => isset($message['params']['mandrill']['from_name']) ? $message['params']['mandrill']['from_name'] : $from['name'],
    'to' => $to,
    'headers' => $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')) : array(),
    'google_analytics_campaign' => variable_get('mandrill_analytics_campaign', ''),
    'attachments' => $attachments,
    'view_content_link' => $view_content,
    'metadata' => $metadata,
  );
  $subaccount = variable_get('mandrill_subaccount', FALSE);
  if ($subaccount) {
    $mandrill_message['subaccount'] = $subaccount;
  }

  // Allow other modules to alter the Mandrill message, and sender/args.
  $mandrill_params = array(
    'message' => $mandrill_message,
    'function' => 'mandrill_sender_plain',
    'args' => array(),
  );
  drupal_alter('mandrill_mail', $mandrill_params, $message);

  // Queue for processing during cron or send immediately.
  $status = NULL;
  if (mandrill_process_async()) {
    $queue = DrupalQueue::get(MANDRILL_QUEUE, TRUE);
    $queue
      ->createItem($mandrill_params);
    if (variable_get('mandrill_batch_log_queued', TRUE)) {
      watchdog('mandrill', 'Message from %from to %to queued for delivery.', array(
        '%from' => $from['email'],
        '%to' => $to[0]['email'],
      ), WATCHDOG_NOTICE);
    }
    return TRUE;
  }
  else {
    return mandrill_mailsend($mandrill_params['message'], $mandrill_params['function'], $mandrill_params['args']);
  }
}