You are here

function maillog_mail_send in Maillog / Mail Developer 6

Receive a command to send a mail.

$message to: recipient from: source $mailengine_call TRUE if function called from maillog_mailengine, otherwise falls. This variable helps to prevent a kind of recursion.

Return value

TRUE on success

2 calls to maillog_mail_send()
maillog.inc in includes/maillog.inc
Provides drupal_mail_wrapper function
maillog_mailengine in ./maillog.module
Implementation of hook_mailengine from mimemail modul

File

./maillog.module, line 314
Provides a 'maillog' node type

Code

function maillog_mail_send($message, $mailengine_call = FALSE) {

  // Static variable used to check for recursive mail sending and prevent it
  static $recursive_send = FALSE;
  if ($recursive_send) {
    drupal_set_message(t('Maillog has detected recursive mail sending.'), 'warning');
    return FALSE;
  }
  $recursive_send = TRUE;

  // Log the e-mail
  if (variable_get('maillog_log', TRUE)) {
    $record = new stdClass();
    $record->header_message_id = isset($message['headers']['Message-ID']) ? $message['headers']['Message-ID'] : NULL;
    $record->subject = $message['subject'];
    $record->body = $message['body'];
    $record->header_from = isset($message['from']) ? $message['from'] : NULL;
    $record->header_to = NULL;
    if (isset($message['to'])) {
      $header_to = $message['to'];
      if (is_array($header_to)) {
        $header_to = implode(', ', $header_to);
      }
      $record->header_to = $header_to;
    }
    $record->header_reply_to = isset($message['headers']['Reply-To']) ? $message['headers']['Reply-To'] : '';
    $record->header_all = serialize($message['headers']);
    $record->sent_date = time();
    drupal_write_record('maillog', $record);
  }

  // Display the e-mail using Devel module
  if (variable_get('maillog_devel', TRUE) && function_exists('dpm')) {
    $devel_msg = array();
    $devel_msg[t('Subject')] = $message['subject'];
    $devel_msg[t('From')] = $message['from'];
    $devel_msg[t('To')] = $message['to'];
    $devel_msg[t('Reply-To')] = $message['reply_to'];
    $devel_msg[t('Header')] = $message['headers'];
    $devel_msg[t('Body')] = $message['body'];
    dpm($devel_msg, 'maillog');
  }

  // Send the e-mail
  $ret = FALSE;
  if (variable_get('maillog_send', TRUE)) {

    // note: CORE: we're unable to support further mail wrappers here (function name limit)
    // We implement mimemail hook_engine for extensibility.
    $mail_sent = FALSE;
    if ($mailengine_call == TRUE) {
      $engine = variable_get('maillog_engine', '') . '_mailengine';
      if (function_exists($engine) && $engine != 'maillog_mailengine') {
        $message['sender'] = $message['from'];
        $message['address'] = $message['to'];
        unset($message['from']);
        unset($message['to']);
        $ret = $engine('send', $message);
        $mail_sent = TRUE;
      }
    }
    if ($mail_sent == FALSE) {

      // build headers
      $mimeheaders = array();
      foreach ($message['headers'] as $name => $value) {
        $mimeheaders[] = $name . ': ' . mime_header_encode($value);
      }
      $mail_headers = join("\n", $mimeheaders);

      // encode subject
      $mail_subject = mime_header_encode($message['subject']);

      // cleanup body
      $mail_body = str_replace("\r", '', $message['body']);
      $ret = mail($message['to'], $mail_subject, $mail_body, $mail_headers);
    }
  }
  else {
    static $mailwarning = TRUE;
    if ($mailwarning == TRUE) {
      if (user_access('administer maillog')) {
        drupal_set_message(t('Sending of e-mail messages is disabled by Maillog module. Go ' . l('here', 'admin/settings/maillog') . ' to enable.'), 'warning', TRUE);
      }
      else {
        drupal_set_message(t('Drupal tried to send a mail. Note that mails are disabled currently.'), 'warning', TRUE);
      }
    }
    $mailwarning = FALSE;

    // important: report fake success for sending mail for debugging purposes
    $ret = TRUE;
  }
  $recursive_send = FALSE;
  return $ret;
}