You are here

public function PHPMailer::SmtpConnect in SMTP Authentication Support 7

Same name and namespace in other branches
  1. 5 smtp.module \PHPMailer::SmtpConnect()
  2. 7.2 smtp.phpmailer.inc \PHPMailer::SmtpConnect()

Initiates a connection to an SMTP server. Returns FALSE if the operation failed.

@uses SMTP @access public

Parameters

array $options An array of options compatible with stream_context_create():

Return value

bool

1 call to PHPMailer::SmtpConnect()
PHPMailer::SmtpSend in ./smtp.phpmailer.inc
Sends mail via SMTP using PhpSMTP Returns FALSE if there is a bad MAIL FROM, RCPT, or DATA input.

File

./smtp.phpmailer.inc, line 753
The mail handler class in smtp module, based on code of the phpmailer library, customized and relicensed to GPLv2.

Class

PHPMailer
PHPMailer - PHP email transport class NOTE: Requires PHP version 5 or later @package PHPMailer @author Andy Prevost @author Marcus Bointon @copyright 2004 - 2009 Andy Prevost

Code

public function SmtpConnect($options = null) {
  if (is_null($this->smtp)) {
    $this->smtp = new SMTP();
  }

  // if no options are provided, use whatever is set in the instance
  if (is_null($options)) {
    $options = $this->SMTPOptions;
  }
  $this->smtp->do_debug = $this->SMTPDebug;
  $hosts = explode(';', $this->Host);
  $index = 0;
  $connection = $this->smtp
    ->Connected();
  $lastexception = NULL;

  // Retry while there is no connection
  while ($index < count($hosts) && !$connection) {
    try {
      $hostinfo = array();
      if (preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) {
        $host = $hostinfo[1];
        $port = $hostinfo[2];
      }
      else {
        $host = $hosts[$index];
        $port = $this->Port;
      }
      $tls = $this->SMTPSecure == 'tls';
      $ssl = $this->SMTPSecure == 'ssl';
      if ($this->smtp
        ->Connect(($ssl ? 'ssl://' : '') . $host, $port, $this->Timeout, $options)) {
        $hello = $this->Helo != '' ? $this->Helo : $this
          ->ServerHostname();
        $this->smtp
          ->Hello($hello);
        if ($tls) {
          if (!$this->smtp
            ->StartTLS()) {
            throw new phpmailerException(t('StartTLS not supported by server or could not initiate session.'));
          }

          //We must resend HELO after tls negotiation
          $this->smtp
            ->Hello($hello);
        }
        $connection = TRUE;
        if ($this->SMTPAuth) {
          if (!$this->smtp
            ->Authenticate($this->Username, $this->Password)) {
            throw new phpmailerException(t('SMTP Error: Could not authenticate.'));
          }
        }
      }
    } catch (phpmailerException $e) {
      if ($connection) {
        $this
          ->SmtpClose();
        $connection = FALSE;
      }
      $lastexception = $e;
    }
    $index++;
  }
  if (!$connection) {
    if ($lastexception != NULL) {
      throw $lastexception;
    }
    else {
      throw new phpmailerException(t('SMTP Error: Could not connect to SMTP host.'));
    }
  }
  return TRUE;
}