You are here

mimemail.inc in PHPMailer 6

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

File

includes/mimemail.inc
View source
<?php

/**
 * @file
 * Implements PHPMailer support on behalf of Mime Mail module.
 */
module_load_include('php', 'phpmailer', 'phpmailer/class.phpmailer');
class MimeMailPHPMailer extends PHPMailer {

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

  /**
   * Constructor.
   */
  function MimeMailPHPMailer() {
    $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
 *   Mime Mail message array.
 */
function mimemail_phpmailer_send($message) {
  static $mail;
  if (!isset($mail)) {
    $mail = new DrupalPHPMailer();

    // Keep linefeed style in sync.
    $mail->LE = variable_get('mimemail_crlf', "\n");
  }

  // Extract and assign e-mail addresses required for SMTP.
  // Display names are not required. Leave header intact.
  // Parse 'From' e-mail address.
  $address = phpmailer_parse_address($message['from']);
  $mail->From = $address[0]['mail'];
  if (variable_get('phpmailer_debug_email', '') === '') {

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

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

    // Reroute to debug e-mail address.
    $message['address'] = variable_get('phpmailer_debug_email', '');
    $mail
      ->AddAddress($message['address']);
  }
  unset($message['headers']['CC'], $message['headers']['BCC']);
  if ($mail->SMTPDebug) {
    ob_start();
  }
  $message['headers']['Date'] = $mail
    ->RFCDate();
  if ($message['address']) {
    $message['headers']['To'] = $message['address'];
  }
  $message['headers']['Subject'] = $message['subject'];

  // FIXME SpamAssassin says INVALID_MSGID to PHPMailer's generated Message-ID. 06/04/2009 smk
  //  if (!isset($message['headers']['Message-ID'])) {
  //    $message['headers']['Message-ID'] = sprintf("<%s@%s>", md5(uniqid(time())), $mail->ServerHostname());
  //  }
  $header = mimemail_rfc_headers($message['headers']) . $mail->LE . $mail->LE;
  if (!($result = $mail
    ->SmtpSend($header, $message['body']))) {
    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
mimemail_phpmailer_send Send out an e-mail.

Classes

Namesort descending Description
MimeMailPHPMailer