You are here

phpmailer.inc in PHPMailer 6

Same filename and directory in other branches
  1. 5 includes/phpmailer.inc

File

includes/phpmailer.inc
View source
<?php

/**
 * @file
 * Implements PHPMailer support on behalf of Drupal core.
 */
module_load_include('php', 'phpmailer', 'phpmailer/class.phpmailer');
class DrupalPHPMailer extends PHPMailer {

  /**
   * Stores the Return-Path, which can be different from Sender.
   */
  var $ReturnPath = '';

  /**
   * Constructor.
   */
  function DrupalPHPMailer() {
    $this
      ->IsSMTP();
    $this
      ->Reset();
    $protocol = variable_get('smtp_protocol', '');
    $this->Host = $protocol . variable_get('smtp_host', '');
    if ($backup = variable_get('smtp_hostbackup', '')) {
      $this->Host .= ';' . $protocol . $backup;
    }
    $this->Port = variable_get('smtp_port', '25');

    // Use SMTP authentication if both username and password are given.
    $this->Username = variable_get('smtp_username', '');
    $this->Password = variable_get('smtp_password', '');
    $this->SMTPAuth = (bool) ($this->Username != '' && $this->Password != '');
    $this->SMTPKeepAlive = variable_get('smtp_keepalive', 0);
    $this->SMTPDebug = variable_get('smtp_debug', 0);

    // Adjust path to SMTP class.
    $this->PluginDir = drupal_get_path('module', 'phpmailer') . '/phpmailer/';
  }

  /**
   * (Re-)initialize properties after sending mail.
   */
  function Reset() {
    $this
      ->ClearAllRecipients();
    $this
      ->ClearAttachments();
    $this
      ->ClearCustomHeaders();
    $this->Priority = 3;
    $this->CharSet = variable_get('smtp_charset', 'utf-8');
    $this->ContentType = 'text/plain';
    $this->Encoding = '8bit';
    $this->ErrorInfo = '';

    // Set default From name.
    $from_name = variable_get('smtp_fromname', '');
    if ($from_name == '') {

      // Fall back on the site name.
      $from_name = variable_get('site_name', 'Drupal');
    }
    $this->FromName = $from_name;
    $this->Sender = '';
    $this->MessageID = '';
    $this->ReturnPath = '';
  }

  /**
   * Sets the language for all class error messages.
   *
   * To avoid having people to copy the languages/ directory, we provide
   * the english translations in this overridden method.
   * Drupal's language code are different from those used by PHPMailer anyway,
   * and the messages only ever appear in the watchdog reports.
   */
  function SetLanguage($lang_type = 'en', $lang_path = 'language/') {
    $PHPMAILER_LANG = array();
    $PHPMAILER_LANG['provide_address'] = 'You must provide at least one recipient email address.';
    $PHPMAILER_LANG['mailer_not_supported'] = ' mailer is not supported.';
    $PHPMAILER_LANG['execute'] = 'Could not execute: ';
    $PHPMAILER_LANG['instantiate'] = 'Could not instantiate mail function.';
    $PHPMAILER_LANG['authenticate'] = 'SMTP Error: Could not authenticate.';
    $PHPMAILER_LANG['from_failed'] = 'The following From address failed: ';
    $PHPMAILER_LANG['recipients_failed'] = 'SMTP Error: The following recipients failed: ';
    $PHPMAILER_LANG['data_not_accepted'] = 'SMTP Error: Data not accepted.';
    $PHPMAILER_LANG['connect_host'] = 'SMTP Error: Could not connect to SMTP host.';
    $PHPMAILER_LANG['file_access'] = 'Could not access file: ';
    $PHPMAILER_LANG['file_open'] = 'File Error: Could not open file: ';
    $PHPMAILER_LANG['encoding'] = 'Unknown encoding: ';
    $PHPMAILER_LANG['signing'] = 'Signing Error: ';
    $this->language = $PHPMAILER_LANG;
    return TRUE;
  }

  /**
   * Assembles message header.
   *
   * PHPMailer always sets Return-Path to Sender, we want more flexibility.
   */
  function CreateHeader() {
    $old_sender = $this->Sender;
    if ($this->ReturnPath != '') {
      $this->Sender = $this->ReturnPath;
    }
    $result = parent::CreateHeader();

    // Restore sender for use in MAIL FROM command.
    $this->Sender = $old_sender;
    return $result;
  }

  /**
   * Returns the proper RFC 822 formatted date.
   */
  function RFCDate() {
    $tz = date('Z');
    $tzs = $tz < 0 ? '-' : '+';
    $tz = abs($tz);
    $tz = (int) ($tz / 3600) * 100 + $tz % 3600 / 60;
    $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
    return $result;
  }

}

/**
 * Send out an e-mail.
 *
 * @param $message
 *   Message array structure.
 */
function phpmailer_send($message) {
  static $mail;
  if (!isset($mail)) {
    $mail = new DrupalPHPMailer();
  }

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

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

    // Extract CCs and BCCs from headers.
    if (isset($message['headers']['CC'])) {
      foreach (phpmailer_parse_address($message['headers']['CC']) as $address) {
        $mail
          ->AddCC($address['mail'], $address['name']);
      }
    }
    if (isset($message['headers']['BCC'])) {
      foreach (phpmailer_parse_address($message['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($message['headers']['CC'], $message['headers']['BCC']);

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

  // This one is always set by PHPMailer.
  unset($message['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 ($message['headers'] as $key => $value) {
    $mail
      ->AddCustomHeader("{$key}:{$value}");
  }
  $mail->Subject = $message['subject'];
  $mail->Body = $message['body'];
  if ($mail->SMTPDebug) {
    ob_start();
  }
  if (!($result = $mail
    ->Send())) {
    watchdog('phpmailer', $mail->ErrorInfo, NULL, WATCHDOG_ERROR);
  }

  // Reset object properties.
  $mail
    ->Reset();
  if ($mail->SMTPDebug) {
    if ($debug = ob_get_contents()) {
      drupal_set_message($debug);
    }
    ob_end_clean();
  }
  return $result;
}

Functions

Namesort descending Description
phpmailer_send Send out an e-mail.

Classes

Namesort descending Description
DrupalPHPMailer