Php uses the smtp service to send mail first define (& #39; SMTP_STATUS_NOT_CONNECTED & #39;, 1, true); define (& #39; SMTP_STATUS_CONNECTED & #39;, 2, true); classsmtp {var $ connection; var $ recipients; php uses the smtp service to send mail first
Define ('smtp _ STATUS_NOT_CONNECTED ', 1, true); define ('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 host default: localhost * port SMTP server port default: 25 * name of the helo command sent by HELO default: localhost * user SMTP server user name default: Null Value * pass SMTP server login password default: NULL value * timeout connection timeout time 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 no user name is set, do not verify $ this-> auth = (''= $ this-> user )? False: true;} function connect ($ params = array () {if (! Isset ($ this-> status) {$ obj = new smtp ($ params); 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; re Turn $ this-> auth? $ This-> ehlo (): $ this-> helo ();} else {log_write ($ errstr, _ FILE __, _ LINE __); $ this-> errors [] = 'failed' to connect to server :'. $ errstr; return false ;}}/ *** the parameter is the address of the * recipients receiver array * from sender, it will also be used as an array of * body email body */function send ($ params = array () reply addresses * headers header information ()) {foreach ($ params AS $ key = >$ value) {$ this-> $ key = $ value;} if ($ this-> is_connected ()) {// whether the 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) === '20140901 ');} else {$ this-> errors [] = 'not ccted! '; Return false ;}} function helo () {if (is_resource ($ this-> connection) AND $ this-> send_data ('helo '. $ this-> helo) AND substr ($ error = $ this-> get_data (), 0, 3) === '000000') {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) === '000000') {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 login') AND substr ($ error = $ this-> get_data (), 0, 3) === '2013' AND $ this-> send_data (base64_encode ($ this-> user) // Send username AND substr ($ error = $ this -> Get_data (), 334) === '000000' AND $ this-> send_data (base64_encode ($ this-> pass )) // Send password AND substr ($ error = $ this-> get_data (), 235) === '000000') {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) =' 250 ') {return true;} else {return false;} function rcpt ($ to) {if ($ this-> is_connected () AND $ this-> send_data ('rcpt TO: <'. $. '>') AND substr ($ error = $ this-> get_data (), 0, 2) ==='25') {return true ;} else {$ this-> errors [] = trim (substr ($ error, 3); return false ;}} function data () {if ($ this-> is_connected () AND $ this-> send_data ('data') AND substr ($ error = $ this-> get_data (), 0, 3) === '000000') {return true;} else {$ this-> errors [] = trim (substr ($ error, 3); return false ;}} function is_connected () {return (is_resource ($ this-> connection) AND ($ this-> status = SMTP_STATUS_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 () {$ retur N = ''; $ 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 '';}} /*** get 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 '';}}}?>