PHP uses the SMTP service to send mail first

Source: Internet
Author: User
PHP uses the SMTP service to send mail first
Define (' smtp_status_not_connected ', 1, true);d efine (' smtp_status_connected ', 2, true); class smtp{var $connection;    var $recipients;    var $headers;    var $timeout;    var $errors;    var $status;    var $body;    var $from;    var $host;    var $port;    var $helo;    var $auth;    var $user;    var $pass; /** * parameter is an array * Host SMTP server hosts default: localhost * port default for SMTP server: * HEL     o The name of the Send helo command default: localhost * User name for SMTP server default: null * Pass SMTP server login password default: null value * Timeout Connection time-out default: 5 * @return BOOL */function SMTP ($params = Array ()) {if (!        Defined (' CRLF ')) {define (' CRLF ', "\ r \ n", true);        } $this->timeout = 10;        $this->status = smtp_status_not_connected;        $this->host = ' localhost ';        $this->port = 25;        $this->auth = false;        $this->user = "; $This->pass = ";        $this->errors = Array ();        foreach ($params as $key = + $value) {$this $key = $value;        } $this->helo = $this->host; If the user name is not set, the $this->auth = (' = = $this->user) is not verified?    False:true; } function Connect ($params = Array ()) {if (!isset ($this->status)) {$obj = new SMTP ($PA            Rams);            if ($obj->connect ()) {$obj->status = smtp_status_connected;        } return $obj; } else {if (!empty ($GLOBALS [' _cfg '] [' Smtp_ssl '])) {$this->host = " ssl://".            $this->host;            } $this->connection = @fsockopen ($this->host, $this->port, $errno, $errstr, $this->timeout);                if ($this->connection = = = False) {$this->errors[] = ' Access is denied. ';       return false;     } @socket_set_timeout ($this->connection, 0, 250000);            $greeting = $this->get_data ();                if (Is_resource ($this->connection)) {$this->status = 2; Return $this->auth?            $this->ehlo (): $this->helo ();                } else {log_write ($errstr, __file__, __line__); $this->errors[] = ' Failed to connect to server: '.                $ERRSTR;            return false;         }}}/** * parameter is an array of * recipients receiver's arrays * from Sender's address, also as reply address * headers An array of header information * The body of the message */function send ($params = Array ()) {foreach ($params as $key =&gt ;        $value) {$this, $key = $value;                if ($this->is_connected ()) {//server needs to verify if ($this->auth) {              if (! $this->auth ()) {      return false;            }} $this->mail ($this->from);                    if (Is_array ($this->recipients)) {foreach ($this->recipients as $value) {                $this->rcpt ($value);            }} else {$this->rcpt ($this->recipients);            } if (! $this->data ()) {return false; } $headers = Str_replace (CRLF. '. ', CRLF.            '.. ', Trim (implode (CRLF, $this->headers))); $body = Str_replace (CRLF. '. ', CRLF.            '.. ', $this->body); $body = substr ($body, 0, 1) = = '. '? '.' .            $body: $body;            $this->send_data ($headers);            $this->send_data (");            $this->send_data ($body);            $this->send_data ('. ');        Return (substr ($this->get_data (), 0, 3) = = = ' 250 '); } else {$this-&GT;errors[] = ' not connected! ';        return false;  }} function Helo () {if (Is_resource ($this->connection) and $this->send_data (' helo ' . $this->helo) and substr ($error = $this->get_data (), 0, 3) = = = ' + ') {return true        ;            } else {$this->errors[] = ' HELO command failed, output: '. Trim (substr ($error, 3));        return false;  }} function Ehlo () {if (Is_resource ($this->connection) and $this->send_data (' EHLO ' . $this->helo) and substr ($error = $this->get_data (), 0, 3) = = = ' + ') {return true        ;            } else {$this->errors[] = ' EHLO command failed, output: '. Trim (substr ($error, 3));        return false; }} function auth () {if (Is_resource ($this->connection) and $this->send_data (' auth L     Ogin ')           and substr ($error = $this->get_data (), 0, 3) = = = ' 334 ' and $this->send_data (Base64_encode (                $this->user)//Send username and substr ($error = $this->get_data (), 0, 3) = = = ' 334 ' and $this->send_data (Base64_encode ($this->pass))//Send password and substr ($err        or = $this->get_data (), 0,3) = = = ' 235 ') {return true;            } else {$this->errors[] = ' AUTH command failed: '. Trim (substr ($error, 3));        return false; }} function mail ($from) {if ($this->is_connected () and $this->send_data (' Mail from:< ' . $from.        ' > ') and substr ($this->get_data (), 0, 2) = = = ' + ') {return true;        } else {return false; }} function Rcpt ($to) {if ($this->is_connected () and $this->send_data (' RCPT TO:< '. $to.        ' > ') and substr ($error = $this->get_data (), 0, 2) = = = ' + ') {return true;            } else {$this->errors[] = Trim (substr ($error, 3));        return false; }} function data () {if ($this->is_connected () and $this->send_data (' data ') A        ND substr ($error = $this->get_data (), 0, 3) = = = ' 354 ') {return true;            } else {$this->errors[] = Trim (substr ($error, 3));        return false; }} function is_connected () {return (Is_resource ($this->connection) and ($this->status = = = Smtp_sta    tus_connected)); } function Send_data ($data) {if (Is_resource ($this->connection)) {return fwrite ($this- >connection, $data.        CRLF, strlen ($data) + 2);        } else {return false; }} function Get_data () {$return = ';        $line = ";            if (Is_resource ($this->connection)) {while (Strpos ($return, CRLF) = = = False OR $line {3}!== ")                {$line = fgets ($this->connection, 512);            $return. = $line;        } return Trim ($return);        } else {return ';        }}/** * Gets the last error message * * @access public * @return String */function error_msg () {            if (!empty ($this->errors)) {$len = count ($this->errors)-1;        return $this->errors[$len];        } else {return '; }}}?>

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.