You are here

public function SMTP::Authenticate in SMTP Authentication Support 7.2

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

Performs SMTP authentication. Must be run after running the Hello() method. Returns TRUE if successfully authenticated. @access public

Return value

bool

File

./smtp.transport.inc, line 215
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 Authenticate($username, $password) {

  // Start authentication
  fputs($this->smtp_conn, "AUTH LOGIN" . $this->CRLF);
  $rply = $this
    ->get_lines();
  $code = substr($rply, 0, 3);
  if ($code != 334) {
    $this->error = array(
      "error" => "AUTH 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;
  }

  // Send encoded username
  fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
  $rply = $this
    ->get_lines();
  $code = substr($rply, 0, 3);
  if ($code != 334) {
    $this->error = array(
      "error" => "Username 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;
  }

  // Send encoded password
  fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
  $rply = $this
    ->get_lines();
  $code = substr($rply, 0, 3);
  if ($code != 235) {
    $this->error = array(
      "error" => "Password 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;
  }
  return TRUE;
}