You are here

function phpmailer_send in PHPMailer 5.2

Same name and namespace in other branches
  1. 8.3 includes/phpmailer.drupal.inc \phpmailer_send()
  2. 5 includes/phpmailer.inc \phpmailer_send()
  3. 6.3 includes/phpmailer.drupal.inc \phpmailer_send()
  4. 6 includes/phpmailer.inc \phpmailer_send()
  5. 6.2 includes/phpmailer.drupal.inc \phpmailer_send()
  6. 7.4 includes/phpmailer.drupal.inc \phpmailer_send()
  7. 7.3 includes/phpmailer.drupal.inc \phpmailer_send()

Send out an e-mail.

Parameters

$to: The mail address or addresses where the message will be send to.

$subject: Subject of the e-mail to be sent.

$body: Message to be sent.

$from: Sender e-mail address.

$headers: Associative array containing the headers to add.

1 call to phpmailer_send()
phpmailer.module in ./phpmailer.module
Integrates the PHPMailer library for SMTP e-mail delivery.

File

includes/phpmailer.drupal.inc, line 24

Code

function phpmailer_send($to, $subject, $body, $from, $headers) {
  static $mail;
  if (!isset($mail)) {
    $mail = new DrupalPHPMailer();
  }
  try {

    // Fix empty From address. This resembles the behavior of Drupal 6 and later.
    if (!$from) {
      $from = variable_get('site_mail', ini_get('sendmail_from'));
    }

    // Parse 'From' e-mail address.
    $address = phpmailer_parse_address($from);
    $mail->From = $address[0]['mail'];
    if ($address[0]['name'] != '') {
      $mail->FromName = $address[0]['name'];
    }
    unset($headers['From']);
    if (variable_get('phpmailer_debug_email', '') === '') {

      // Set recipients.
      foreach (phpmailer_parse_address($to) as $address) {
        $mail
          ->AddAddress($address['mail'], $address['name']);
      }

      // Extract CCs and BCCs from headers.
      if (isset($headers['CC'])) {
        foreach (phpmailer_parse_address($headers['CC']) as $address) {
          $mail
            ->AddCC($address['mail'], $address['name']);
        }
      }
      if (isset($headers['BCC'])) {
        foreach (phpmailer_parse_address($headers['BCC']) as $address) {
          $mail
            ->AddBCC($address['mail'], $address['name']);
        }
      }
    }
    else {

      // Reroute to debug e-mail address.
      $mail
        ->AddAddress(variable_get('phpmailer_debug_email', ''));
    }
    unset($headers['CC'], $headers['BCC']);

    // Extract Reply-To from headers.
    if (isset($headers['Reply-To'])) {
      foreach (phpmailer_parse_address($headers['Reply-To']) as $address) {
        $mail
          ->AddReplyTo($address['mail'], $address['name']);
      }
      unset($headers['Reply-To']);
    }

    // Extract Content-Type and charset.
    if (isset($headers['Content-Type'])) {
      $content_type = explode(';', $headers['Content-Type']);
      $mail->ContentType = trim(array_shift($content_type));
      foreach ($content_type as $param) {
        $param = explode('=', $param, 2);
        $key = trim($param[0]);
        if ($key == 'charset') {
          $mail->CharSet = trim($param[1]);
        }
        else {
          $mail->ContentType .= '; ' . $key . '=' . trim($param[1]);
        }
      }
      unset($headers['Content-Type']);
    }

    // Set additional properties.
    $properties = array(
      'X-Priority' => 'Priority',
      'Content-Transfer-Encoding' => 'Encoding',
      'Sender' => 'Sender',
      'Message-ID' => 'MessageID',
      // Custom property.
      // @see DrupalPHPMailer::CreateHeader()
      'Return-Path' => 'ReturnPath',
    );
    foreach ($properties as $source => $property) {
      if (isset($headers[$source])) {
        $mail->{$property} = $headers[$source];
        unset($headers[$source]);
      }
    }

    // This one is always set by PHPMailer.
    unset($headers['MIME-Version']);

    // Add remaining header lines.
    // Note: Any header lines MUST already be checked by the caller for unwanted
    // newline characters to avoid header injection.
    // @see PHPMailer::SecureHeader()
    foreach ($headers as $key => $value) {
      $mail
        ->AddCustomHeader("{$key}:{$value}");
    }
    $mail->Subject = $subject;
    $mail->Body = $body;
    return $mail
      ->Send();
  } catch (phpmailerException $e) {
    drupal_set_message(t('Sending of at least one e-mail failed. The error returned was:<br />@error.', array(
      '@error' => $e
        ->getMessage(),
    )), 'error');
    watchdog('phpmailer', $e
      ->getMessage(), WATCHDOG_ERROR);
    return FALSE;
  }
}