You are here

function drupal_mail in Drupal 5

Same name and namespace in other branches
  1. 6 includes/mail.inc \drupal_mail()
  2. 7 includes/mail.inc \drupal_mail()

Send an e-mail message, using Drupal variables and default settings. More information in the PHP function reference for mail()

Parameters

$mailkey: A key to identify the mail sent, for altering.

$to: The mail address or addresses where the message will be send to. The formatting of this string must comply with RFC 2822. Some examples are: user@example.com user@example.com, anotheruser@example.com User <user@example.com> User <user@example.com>, Another User <anotheruser@example.com>

$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. Drupal will format the correct line endings for you.

$from: Sets From to this value, if given.

$headers: Associative array containing the headers to add. This is typically used to add extra headers (From, Cc, and Bcc). <em>When sending mail, the mail must contain a From header.</em>

Return value

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

Related topics

4 calls to drupal_mail()
contact_mail_page_submit in modules/contact/contact.module
Process the site-wide contact page form submission.
contact_mail_user_submit in modules/contact/contact.module
Process the personal contact page form submission.
user_pass_submit in modules/user/user.module
user_register_submit in modules/user/user.module

File

includes/common.inc, line 2081
Common functions that many Drupal modules will need to reference.

Code

function drupal_mail($mailkey, $to, $subject, $body, $from = NULL, $headers = array()) {
  $defaults = array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/plain; charset=UTF-8; format=flowed',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer' => 'Drupal',
  );

  // To prevent e-mail from looking like spam, the addresses in the Sender and
  // Return-Path headers should have a domain authorized to use the originating
  // SMTP server.  Errors-To is redundant, but shouldn't hurt.
  $default_from = variable_get('site_mail', ini_get('sendmail_from'));
  if ($default_from) {
    $defaults['From'] = $defaults['Sender'] = $defaults['Return-Path'] = $defaults['Errors-To'] = $default_from;
  }
  if ($from) {
    $defaults['From'] = $from;
  }
  $headers = array_merge($defaults, $headers);

  // Custom hook traversal to allow pass by reference
  foreach (module_implements('mail_alter') as $module) {
    $function = $module . '_mail_alter';
    $function($mailkey, $to, $subject, $body, $from, $headers);
  }

  // Allow for custom mail backend
  if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) {
    include_once './' . variable_get('smtp_library', '');
    return drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $headers);
  }
  else {

    // Note: if you are having problems with sending mail, or mails look wrong
    // when they are received you may have to modify the str_replace to suit
    // your systems.
    //  - \r\n will work under dos and windows.
    //  - \n will work for linux, unix and BSDs.
    //  - \r will work for macs.
    //
    // According to RFC 2646, it's quite rude to not wrap your e-mails:
    //
    // "The Text/Plain media type is the lowest common denominator of
    // Internet e-mail, with lines of no more than 997 characters (by
    // convention usually no more than 80), and where the CRLF sequence
    // represents a line break [MIME-IMT]."
    //
    // CRLF === \r\n
    //
    // http://www.rfc-editor.org/rfc/rfc2646.txt
    $mimeheaders = array();
    foreach ($headers as $name => $value) {
      $mimeheaders[] = $name . ': ' . mime_header_encode($value);
    }
    return mail($to, mime_header_encode($subject), str_replace("\r", '', $body), join("\n", $mimeheaders));
  }
}