You are here

function phpmailer_send in PHPMailer 5

Same name and namespace in other branches
  1. 8.3 includes/phpmailer.drupal.inc \phpmailer_send()
  2. 5.2 includes/phpmailer.drupal.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()

Sends out the email.

Parameters

$addresses: Array of recipient addresses.

$subject: Message subject.

$body: Message body.

$from: Sender e-mail address.

$header: Message header.

1 call to phpmailer_send()
phpmailer.module in ./phpmailer.module
This module integrates PHPMailer with Drupal, both as native drupal_mail() wrapper, and as part of the Mime Mail module.

File

includes/phpmailer.inc, line 81

Code

function phpmailer_send($recipients, $subject, $body, $from, $header) {
  $mail = new DrupalPHPMailer();
  settype($header, 'array');

  // Select which From address to use:
  // $from parameter takes precedence over $header['From'].
  if (isset($header['From'])) {

    // Check whether a full e-mail address has been specified
    // ("some name" <user@example.com>).
    if ($parsed = phpmailer_parse_address($header['From'])) {
      $mail->FromName = $parsed['name'];
      $from_address = $parsed['address'];
    }
    else {
      $from_address = $header['From'];
    }
    unset($header['From']);
  }
  if (empty($from)) {
    if (!empty($from_address)) {
      $from = $from_address;
    }
    else {
      $from = variable_get('site_mail', ini_get('sendmail_from'));
    }
  }
  $mail->From = $from;

  // Extract Content-Type and charset.
  if (isset($header['Content-Type'])) {
    $content_type = explode(';', $header['Content-Type']);
    $mail->ContentType = 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($header['Content-Type']);
  }

  // Set additional properties.
  $properties = array(
    'X-Priority' => 'Priority',
    'Content-Transfer-Encoding' => 'Encoding',
  );
  foreach ($properties as $source => $property) {
    if (isset($header[$source])) {
      $mail->{$property} = $header[$source];
      unset($header[$source]);
    }
  }

  // Always set by PHPMailer.
  unset($header['MIME-Version']);

  // Add remaining header lines.
  // Note: Any header lines MUST be checked by the caller for unwanted
  // newline characters to avoid header injection. Use PHPMailer::SecureHeader()
  // for that purpose.
  foreach ($header as $key => $value) {
    $mail
      ->AddCustomHeader("{$key}:{$value}");
  }

  // Check if debugging is turned on and replace recipient.
  $debug_email = variable_get('phpmailer_debug_email', '');
  if ($debug_email) {
    $recipients = array(
      $debug_email,
    );
  }

  // Add recipients.
  foreach ($recipients as $recipient) {
    $mail
      ->AddAddress($recipient);
  }
  $mail->Subject = $subject;
  $mail->Body = $body;
  if (!($result = $mail
    ->Send())) {
    watchdog('smtp', t('Error sending email: From: @from To: @to Error: @error', array(
      '@from' => '<' . $from . '>',
      '@to' => '<' . implode('>, <', $recipients) . '>',
      '@error' => $mail->ErrorInfo,
    )), WATCHDOG_ERROR);
  }

  // Commented out to benefit from keep-alive feature.

  //$mail->SmtpClose();
  return $result;
}