You are here

public function SMTP::StartTLS in SMTP Authentication Support 7

Same name and namespace in other branches
  1. 7.2 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 203
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;
  }

  // Allow the best TLS version(s) we can
  $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;

  // PHP 5.6.7 dropped inclusion of TLS 1.1 and 1.2 in STREAM_CRYPTO_METHOD_TLS_CLIENT
  // so add them back in manually if we can
  if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
    $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
    $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
  }

  // Begin encrypted connection
  $crypto_ok = stream_socket_enable_crypto($this->smtp_conn, TRUE, $crypto_method);
  return (bool) $crypto_ok;
}