You are here

public function SMTP::StartTLS in SMTP Authentication Support 7.2

Same name and namespace in other branches
  1. 7 smtp.transport.inc \SMTP::StartTLS()

Initiate a TLS communication with the server.

SMTP CODE 220 Ready to start TLS SMTP CODE 501 Syntax error (no parameters allowed) SMTP CODE 454 TLS not available due to temporary reason @access public

Return value

bool success

File

./smtp.transport.inc, line 173
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 StartTLS() {
  $this->error = NULL;

  # to avoid confusion
  if (!$this
    ->connected()) {
    $this->error = array(
      "error" => "Called StartTLS() without being connected",
    );
    return FALSE;
  }
  fputs($this->smtp_conn, "STARTTLS" . $this->CRLF);
  $rply = $this
    ->get_lines();
  $code = substr($rply, 0, 3);
  if ($this->do_debug >= 2) {
    drupal_set_message(t("SMTP -> FROM SERVER: @rply", array(
      "@rply" => $rply,
    )));
  }
  if ($code != 220) {
    $this->error = array(
      "error" => "STARTTLS not accepted from server",
      "smtp_code" => $code,
      "smtp_msg" => substr($rply, 4),
    );
    if ($this->do_debug >= 1) {
      drupal_set_message(t("SMTP -> ERROR: @error: @rply", array(
        "@error" => $this->error["error"],
        "@rply" => $rply,
      )));
    }
    return FALSE;
  }

  // Begin encrypted connection
  if (!stream_socket_enable_crypto($this->smtp_conn, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
    return FALSE;
  }
  return TRUE;
}