You are here

function mandrill_mailsend in Mandrill 7.2

Same name and namespace in other branches
  1. 8 mandrill.module \mandrill_mailsend()
  2. 7 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.

string $function: The name of the function to use to send the message.

array $args: Array of arguments to pass to the function provided by $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 158
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'][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) {

        // Allow other modules to react based on a send 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;

              // Since we cannot do much about normal mail bounces do not treat
              // them as errors, just notices.
              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;
  }
}