You are here

public function NewsletterMailSystem::mail in Newsletter 7.2

Same name and namespace in other branches
  1. 7 includes/newsletter.mail.inc \NewsletterMailSystem::mail()

Send a message composed by drupal_mail().

Parameters

$message: Message array with at least the following elements:

  • id: A unique identifier of the e-mail type. Examples: 'contact_user_copy', 'user_password_reset'.
  • to: The mail address or addresses where the message will be sent to. The formatting of this string will be validated with the PHP e-mail validation filter. Some examples are:

  • subject: Subject of the e-mail to be sent. This must not contain any newline characters, or the mail may not be sent properly.
  • body: Message to be sent. Accepts both CRLF and LF line-endings. E-mail bodies must be wrapped. You can use drupal_wrap_mail() for smart plain text wrapping.
  • headers: Associative array containing all additional mail headers not defined by one of the other parameters. PHP's mail() looks for Cc and Bcc headers and sends the mail to addresses in these headers too.

Return value

TRUE if the mail was successfully accepted for delivery, otherwise FALSE.

Overrides MailSystemInterface::mail

File

includes/newsletter.mail.inc, line 66
Class file for MailSystemInterface implementation.

Class

NewsletterMailSystem
Modify the drupal mail system to send HTML emails.

Code

public function mail(array $message) {
  if ($message['format'] == 'html') {
    $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
  }

  // SMTP check
  if (variable_get('newsletter_use_smtp', FALSE) && module_exists('smtp')) {
    $smtp = new SmtpMailSystem();
    $message['body'] = array(
      $message['body'],
    );
    $message = $smtp
      ->format($message);
    $mail_result = $smtp
      ->mail($message);
  }
  else {
    if (isset($message['headers']['Return-Path']) && !ini_get('safe_mode')) {
      $return_path_set = strpos(ini_get('sendmail_path'), ' -f');
      if (!$return_path_set) {
        $message['Return-Path'] = $message['headers']['Return-Path'];
        unset($message['headers']['Return-Path']);
      }
    }
    $mimeheaders = array();
    foreach ($message['headers'] as $name => $value) {
      $mimeheaders[] = $name . ': ' . mime_header_encode($value);
    }
    $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
    $mail_subject = mime_header_encode($message['subject']);
    $mail_body = $message['body'];
    $mail_headers = implode("\n", $mimeheaders);
    if (isset($message['Return-Path']) && !ini_get('safe_mode')) {
      $mail_result = mail($message['to'], $mail_subject, $mail_body, $mail_headers, '-f ' . $message['Return-Path']);
    }
    else {
      $mail_result = mail($message['to'], $mail_subject, $mail_body, $mail_headers);
    }
  }

  // Integration with maillog
  if (module_exists('maillog') && 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 = isset($message['to']) ? $message['to'] : NULL;
    $record->header_reply_to = isset($message['headers']['Reply-To']) ? $message['headers']['Reply-To'] : '';
    $record->header_all = serialize($message['headers']);
    $record->sent_date = REQUEST_TIME;
    drupal_write_record('maillog', $record);
  }
  return $mail_result;
}