You are here

public function PHPMailer::SmtpConnect in SMTP Authentication Support 7.2

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

Initiates a connection to an SMTP server. Returns FALSE if the operation failed. @uses SMTP @access public

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 746
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() {
  if (is_null($this->smtp)) {
    $this->smtp = new SMTP();
  }
  $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)) {
        $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;
}