PHP sends an email via SMTP

xiaoxiao2021-03-06  39

Define ('SMTP_STATUS_NOT_CONNECTED', 1, TRUE); Define ('SMTP_STATUS_CONNECTED', 2, TRUE);

Class SMTP {

Var $ rececon; var $ timeout; var $ ireout; var $ ireout; var $ status; var $ from; var $ host; var $ port; var $ helo; var $ port; var $ helo; var $ av; var $ h USER; VAR $ Pass;

/ *********************************************** ** CONSTRUCTOR function. Arguments: ** $ params - An assoc array of parameters: ** ** host - The hostname of the smtp server Default: localhost ** port - The port the smtp server runs on Default: 25 ** helo - What to send as the HELO command Default: localhost ** (typically the hostname of the ** machine this script runs on) ** auth - Whether to use basic authentication Default: FALSE ** user - Username for authentication Default: ** pass - Password for authentication Default: ** Timeout - The Timeout In Seconds for the Call Default: 5 ** to fsockopen () ***************************************** ************ /

Function SMTP ($ params = array ()) {

IF (! Defined ('CRLF')) Define ('CRLF', "/ R / N", TRUE); $ this-> Timeout = 5; $ this-> status = SMTP_STATUS_NOT_CONNECTED; $ this-> host = 'localhost '; $ This-> port = 25; $ this-> helo =' localhost '; $ this-> auth = false; $ this-> user ='; $ this-> pass = '; $ this-> Errors = array (); foreach ($ Params AS $ Key => $ value) {$ this -> $ key = $ value;}}

/ ************************************************ ********** ** CONNECT FUNCTION. This Will, When Called ** static or create a new smtp object, ** Call The Connect Function (IE this function) ** and return it. When not caled staticly, ** it will connect to the server and send ** the helo command. ** ********************************************* /

Function Connect ($ params = array ()) {

IF ($ this-> status) {$ obj = new smtp ($ params); if ($ obj-> connect ()) {$ obj-> status = SMTP_STATUS_CONNECTED;

Return $ OBJ;

} else {$ this-> Connection = fsockopen ($ this-> Host, $ this-> Port, $ errno, $ errstr, $ this-> timeout; socket_set_timeout ($ this-> connection, 0, 250000);

$ greeting = $ this-> get_data (); if (is_resource ($ this-> connection)) {return $ this-> author? $ this-> ehlo (): $ this-> helo ();} else {$ This-> Errors [] = 'Failed to Connect To Server:'. $ Errstr; Return False;}}}

/ ********************************************************** ** Function Which Handles Sending the mail. * * Arguments: ** $ params - Optional assoc array of parameters ** Can contain:. ** recipients - Indexed array of recipients ** from - The from address (used in MAIL fROM :), ** this will be the return. Path ** Headers, One Header Per Array Entry ** Body - The Body of the Email ** It Can Also Contain Any of The Parameters from the connection *************** **************************************** / Function Send ($ Params = array ()) {

Foreach ($ Params AS $ Key => $ Value) {$ this-> set ($ key, $ value);

IF ($ THIS-> IS_CONNECTED ()) {

// do we auth or not? Note The Distinction Between The Auth variable and auth () Function IF ($ this-> auth) {if (! $ This-> auth ()) Return False;}

$ this-> mail ($ this-> from); IF (is_Array ($ this-> Recipients) "$ this-> rcpt ($ value); else $ this-> RCPT ($ this-> Recipients);

IF ($ this-> data ()) Return False;

// Transparency $ headers = str_replace (CRLF. '.', CRLF. '..', Trim (Implode (CRLF, $ THIS-> Headers)))))))); $ body = str_replace (crlf. '.', CRLF. ' .. ', $ this-> body); $ body = $ body [0] =='. '?'. '. $ 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-> errors [] = 'not connection!'; returnaf,}} / ************************************************ ** FUNCTION TO IMPLEMENT HELO CMD **** ****************************************************** / FUNCTION HELO () {if (is_Resource ($ this-> Connection ) And $ this-> send_data ('Helo'. $ This-> Helo) And Substr (TRIM ($ error = $ this-> get_data ()), 0, 3) === 250 ') {

Return True;

} else {$ this-> Errors [] = 'Helo Command Faled, Output:'. Trim (Substr (Trim ($ Error), 3); Return False;} / *********** **************************** ** FUNCTION TO IMPLEMENT EHLO CMD ************** ************************ /

Function ehlo () {IF (is_resource ($ this-> connection) and $ this-> send_data ('ehlo'. $ this-> helo) and subs (Trim ($ error = $ this-> get_data ()), 0 , 3) === '250') {

Return True;

} else {$ this-> errors [] = 'EHLO Command Failed, Output:'. Trim (Substr (Trim ($ Error), 3); Return False;} / ********** **************************** ** FUNCTION TO IMPLEMENT Auth cmd ********************** ************************ /

Function auth () {if (is_resource ($ this-> connection) and $ this-> send_data ('auth login ") And Substr (TRIM ($ error = $ this-> get_data ()), 0, 3) == = '334' and $ this-> send_data ($ this-> user)) // send username and substr (TRIM ($ error = $ this-> get_data ()), 0, 3) === '334 'And $ this-> send_data ($ this-> pass) // send password and substr (TRIM ($ error = $ this-> get_data ()), 0, 3) ===' 235 ') { Return True;

} else {$ this-> errors [] = 'auth command failed:'. Trim (Substr (Trim ($ Error), 3); Return False;}}

/ ********************************************************** ** Function That Handles The Mail from: CMD ********************************************************************* / FUNCTION MAIL ($ from) {

IF ($ this-> is_connected () and $ this-> send_data ('mail from: <'. $ from. '>') And Substr (TRIM ($ this-> get_data ()), 0, 2) == = '250') {

Return True;

} else returnaf

/ ********************************************************** ** Function That Handles the RCPT to: cmd *********************************************************** / FUNCTION RCPT ($ to) {

IF ($ THIS-> IS_CONNECTED () and $ this-> send_data ('RCPT TO: <'. $ to. '>') And Substr (TRIM ($ error = $ this-> get_data ()), 0, 2 ) === '25') {

Return True;

} else {$ this-> Errors [] = Trim (Substr (Trim ($ Error), 3)); Return False;}}

/ ************************************************ ** Function That Sends the data cmd ** ********************************************* /

Function data () {

IF ($ this-> is_connected () and $ this-> send_data ('data') And Substr (TRIM ($ error = $ this-> get_data ()), 0, 3) === '354') {Return Else {$ this-> errors [] = trim (Substr (Trim ($ Error), 3); Return False;}}

/ *********************************************************** ** FUNCTION TO DETERMINE IF this Object ** IS connected to the server or not. **************************************

Function is_connected () {

Return (is_resource ($ this-> connection) and ($ this-> status === SMTP_STATUS_CONNECTED);}

/ *********************************************************** ** FUNCTION TO Send A bit of data * *********************************************** /

Function Send_Data ($ data) {

IF (is_Resource ($ this-> connection) {Return Fwrite ($ THIS-> Connection, $ DATA.CRLF, STRLEN ($ DATA) 2);} else returnaf false;

/ ************************************************ ** Function to get data. *** ******************************************* /

Function & Get_Data () {

$ RETURN = ''; $ line = '';

IF (is_resource ($ RETURN, CRLF) {while ($ RETURN, CRLF) === false or substr ($ Line, 3, 1)! == ') {$ line = fgets ($ this- > Connection, 512); $ RETURN. = $ line;} Return $ RETURN;

} else returnaf

/ ************************************************ ** STS a variable ***** **************************************** {VAR, $ VALUE) {

$ this -> $ var = $ value; return true;}

} // end of class?>

/ ********************************************************* ** SETUP SOME Parameters Which Will be ** Passed to the SMTP :: Connect () Call. ************************************************** / $ Params ['host'] = 'smtp.sohu.com'; // The SMTP Server Host / IP $ Params ['Port'] = 25; // The SMTP Server Port $ Params ['Helo'] = EXEC ('hostname'); // What to use when desnding the helo command. Typically, Your domain / hostname $ params ['auth'] = true; // WHether To Use Basic Authentication or Not $ Params ['user'] = 'pHW82'; // username for authentication $ params ['pass'] = 'xxxxxxx'; // Password for Authentication

/ ********************************************************* ** THESE Parameters get passed to the ** SMTP-> send () call. *********************************************** /

$ send_params ['Recipients'] = array ('phw82@sohu.com '); // The Recipients (can be multiple) $ send_params [' headers '] = array (' from: "Pan" ', //Headers' to: phw82@sohu.com', 'Subject: Test email'); $ send_params ['from'] = 'phw82@sohu.com'; // this is buy as in the mail from : cmd // it shop end up as the return-path: header $ send_params ['body'] = '.test email.'; // the body of the email / **************** ******************************************* ** THE CRE CREATES The Object and ** sends the email. ******** ****************************** /?>

new document </ title> <meta name = "generator" content = "editplus"> < Meta Name = "Author" content = "> <meta name =" keywords "content ="> <meta name = "description" content = "> </ head></p> <p><Body> <? If (is_Object ($ SMTP = SMTP :: Connect ($ params)) and $ SMTP-> Send ($ send_params)) {echo 'email Sent successfully!'. "/ R / n / r / n "</p> <p>// any Recipients That failed (relaying Denied for Example) Will Be Logged in The Errors Variable. Print_r ($ SMTP-> Errors);</p> <p>} else {echo 'error sending mail'. "/ r / n / r / n"; // The Reason for Failure Should Be in The Errors Variable Print_R ($ SMTP-> Errors);</p> <p>?> </ Body> </ html></p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-64829.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="64829" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.053</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'sKy9g_2FOD8If3i_2BKi7SfxvSH7pqzzR1O45Drl4QZzhSKYg1TCfqLq_2Ffqvd6Q_2BRdRFKiveeHZJCvn_2F1pq_2Fj7JR0Q_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>