php / ************************************* FileName .... ...: class.smtp.inc ** project ........: smtp class ** version ........: 1.00b ** last modified ..: 30 September 2001 *** ******************************************* /
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:
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?> Php / ************************************************* *** FileName .......: test.php ** project ........: smtp class ** last modified ..: 30 September 2001 ********** ************************************************** ****************** ** Include the class. The header () Makes ** The output look lovely. ************ ************************** / include ('Class.smtp.inc'); Header ('Content-Type: Text / Plain ") ;
/ ********************************************************* ** 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"
// any Recipients That failed (relaying Denied for Example) Will Be Logged in The Errors Variable. Print_r ($ SMTP-> Errors);
} else {echo 'error sending mail'. "/ r / n / r / n"; // The Reason for Failure Should Be in The Errors Variable Print_R ($ SMTP-> Errors);
?> Body> html>