You are here

public function SMTP::Connect in SMTP Authentication Support 7

Same name and namespace in other branches
  1. 5 smtp.module \SMTP::Connect()
  2. 7.2 smtp.transport.inc \SMTP::Connect()

Connect to the server specified on the port specified. If the port is not specified use the default SMTP_PORT. If tval is specified then a connection will try and be established with the server for that number of seconds. If tval is not specified the default is 30 seconds to try on the connection.

SMTP CODE SUCCESS: 220 SMTP CODE FAILURE: 421 @access public

Return value

bool

File

./smtp.transport.inc, line 117
SMTP mail transport class for the smtp module,based on code of the phpmailer library, customized and relicensed to GPLv2

Class

SMTP
SMTP is rfc 821 compliant and implements all the rfc 821 SMTP commands except TURN which will always return a not implemented error. SMTP also provides some utility methods for sending mail to an SMTP server. original author: Chris Ryan

Code

public function Connect($host, $port = 0, $tval = 30, $options = array()) {
  static $streamok;

  // This is enabled by default since 5.0.0 but some providers disable it
  // Check this once and cache the result
  if (is_null($streamok)) {
    $streamok = function_exists('stream_socket_client');
  }

  // clear errors to avoid confusion
  $this->error = NULL;

  // make sure we are __not__ connected
  if ($this
    ->connected()) {

    // already connected, generate error
    $this->error = array(
      "error" => "Already connected to a server",
    );
    return FALSE;
  }
  if (empty($port)) {
    $port = $this->SMTP_PORT;
  }
  $errno = 0;
  $errstr = '';
  if ($streamok) {
    $socket_context = stream_context_create($options);

    // Suppress errors; connection failures are handled at a higher level
    $this->smtp_conn = @stream_socket_client($host . ":" . $port, $errno, $errstr, $tval, STREAM_CLIENT_CONNECT, $socket_context);
  }
  else {

    // Fall back to fsockopen which should work in more places, but is missing some features
    $this->smtp_conn = @fsockopen($host, $port, $errno, $errstr, $tval);
  }

  // verify we connected properly
  if (!is_resource($this->smtp_conn)) {
    $this->error = array(
      "error" => "Failed to connect to server",
      "errno" => $errno,
      "errstr" => $errstr,
    );
    if ($this->do_debug >= 1) {
      drupal_set_message(t("SMTP -> ERROR: @error: @errstr (@errno)", array(
        "@error" => $this->error["error"],
        "@errstr" => $errstr,
        "@errno" => $errno,
      )));
    }
    return FALSE;
  }

  // SMTP server can take longer to respond, give longer timeout for first read
  // Windows does not have support for this timeout function
  if (substr(PHP_OS, 0, 3) != 'WIN') {
    $max = ini_get('max_execution_time');

    // Don't bother if unlimited
    if ($max != 0 && $tval > $max) {
      @set_time_limit($tval);
    }
    stream_set_timeout($this->smtp_conn, $tval, 0);
  }

  // get any announcement
  $announce = $this
    ->get_lines();
  if ($this->do_debug >= 2) {
    drupal_set_message(t("SMTP -> FROM SERVER: @announce", array(
      "@announce" => $announce,
    )));
  }
  return TRUE;
}