You are here

function mandrill_mailsend in Mandrill 7

Same name and namespace in other branches
  1. 8 mandrill.module \mandrill_mailsend()
  2. 7.2 mandrill.module \mandrill_mailsend()

Abstracts sending of messages, allowing queueing option.

Parameters

array $message: A message array formatted for Mandrill's sending API, plus 2 additional indexes for the send_function and an array of $args, if needed by the send function.

Return value

bool TRUE if no exception thrown

2 calls to mandrill_mailsend()
MandrillMailSystem::mail in lib/mandrill.mail.inc
Send the email message.
mandrill_queue_worker_mailsend in ./mandrill.module
Sends a queued email.

File

./mandrill.module, line 135
Enables Drupal to send email directly through Mandrill.

Code

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'],
        '%function' => $function,
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    $params = array(
      $message,
    ) + $args;
    $results = call_user_func_array($function, $params);
    foreach ($results as $result) {

      // Allow other modules to react based on a send result.
      module_invoke_all('mandrill_mailsend_result', $result);
      switch ($result['status']) {
        case "error":
        case "invalid":
        case "rejected":
          $to = isset($result['email']) ? $result['email'] : 'recipient';
          $status = isset($result['status']) ? $result['status'] : 'message';
          $error_message = isset($result['message']) ? $result['message'] : 'no message';
          watchdog('mandrill', 'Failed sending email from %from to %to. @status: @message', array(
            '%from' => $message['from_email'],
            '%to' => $to,
            '@status' => $status,
            '@message' => $error_message,
          ), WATCHDOG_ERROR);
          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;
      }
    }
    return TRUE;
  } catch (MandrillException $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;
  }
}