A complete SMTP class in PHP (resolving questions when mail () encounters the mail server to verify) [转]

xiaoxiao2021-03-06  57

Class.smtp.php

Class SMTP

{

/ * Public variables * /

VAR $ SMTP_PORT;

VAR $ TIME_OUT;

Var $ host_name;

Var $ log_file;

Var $ relay_host;

Var $ debug;

Var $ auth;

Var $ user;

Var $ Pass;

/ * Private variables * / var $ sock;

/ * Constractor * /

Function SMTP ($ relay_host = ", $ satp_port = 25, $ auth = false, $ user, $ pass)

{

$ this-> debug = false;

$ this-> SMTP_PORT = $ SMTP_PORT;

$ this-> relay_host = $ relay_host;

$ this-> time_out = 30; // is buy in fsockopen () #

$ this-> auth = $ auth; // auth

$ this-> user = $ us;

$ this-> pass = $ pass;

#

$ this-> Host_name = "localhost"; // is buy in hello command $ this-> log_file = ";

$ this-> sock = false;

}

/ * Main function * /

Function Sendmail ($ To, $ from, $ Subject = ", $ BODY =", $ MAILTYPE, $ CC = ", $ BCC =" ", $ additional_headers =" "

{

$ mail_from = $ this-> get_address ($ this-> Strip_comment);

$ body = EREG_REPLACE ("(^ | (/ r / n)) (/.)", "/1./3", $ body);

$ header. = "MIME-VERSION: 1.0 / R / N";

IF ($ shipype == "html") {

$ Header. = "Content-Type: Text / HTML / R / N";

}

$ header. = "TO:". $ TO. "/ r / n";

IF ($ cc! = "") {

$ Header. = "CC:". $ cc. "/ r / n";

}

$ Header. = "from: $ from <". $ from. "> / r / n";

$ header. = "Subject:" $ subject. "/ r / n";

$ header. = $ additional_headers;

$ Header. = "Date:" .date ("r"). "/ r / n";

$ header. = "x-mailer: by redhat (pHP /". phpversion (). ") / r / n";

List ($ msec, $ sec) = expedition ("", microtime ()); $ header. = "Message-ID: <". Date ("YMDHIS", $ sec). "." ($ msec * 1000000 "." $ mail_from. "> / r / n";

$ To = evLode (",", $ this-> Strip_comment ($ to));

IF ($ cc! = "") {

$ To = array_merge ($ TO, EXPLODE (",", $ THIS-> Strip_comment ($ CC)))));

}

IF ($ BCC! = "") {

$ To = array_merge ($ TO, EXPLODE (",", $ this-> Strip_comment ($ BCC)));

}

$ SENT = True;

Foreach ($ TO AS $ RCPT_TO) {

$ RCPT_TO = $ this-> get_address ($ RCPT_TO);

IF (! $ this-> smtp_sockopen ($ rcpt_to)) {

$ this-> log_write ("error: cannot send email to". $ RCPT_TO. "/ n");

$ SENT = FALSE;

CONTINUE;

}

IF ($ this-> smtp_send ($ this-> Host_name, $ mail_from, $ rcpt_to, $ header, $ body)) {

$ this-> log_write ("E-mail Has Been Sent TO <". $ RCPT_TO. "> / n");

} else {

$ this-> log_write ("error: cannot send email to <". $ RCPT_TO. "> / n");

$ SENT = FALSE;

}

Fclose ($ this-> suck);

$ this-> log_write ("Disconnected from Remote Host / N);

}

Return $ SENT;

}

/ * Private functions * /

Function SMTP_SEND ($ HELO, $ from, $ To, $ Header, $ Body = "")

{

IF (! $ this-> smtp_putcmd ("helo", $ helo) {

Return $ this-> SMTP_ERROR ("Sending Helo Command");

}

#aut

IF ($ this-> auth) {

IF (! $ this-> smtp_putcmd ("auth login", base64_encode ($ this-> user))) {

Return $ this-> SMTP_ERROR ("Sending Helo Command");

}

IF ($ this-> smtp_putcmd ("", base64_encode ($ this-> pass)) {

Return $ this-> SMTP_ERROR ("Sending Helo Command");

}

}

#

IF ($ this-> smtp_putcmd ("mail", "from: <". $ from. ">")) {Return $ this-> smtp_error ("Sending Mail from Command);

}

IF ($ this-> smtp_putcmd ("rcpt", "to: <". $ to. ">")) {

Return $ this-> SMTP_ERROR ("Sending Rcpt To Command);

}

IF ($ this-> smtp_putcmd ("data")) {

Return $ this-> SMTP_ERROR ("Sending Data Command");

}

IF (! $ this-> smtp_message) {

Return $ this-> SMTP_ERROR ("Sending Message");

}

IF (! $ this-> smtp_eom ()) {

Return $ this-> SMTP_ERROR ("Sending . [EOM]");

}

IF ($ this-> smtp_putcmd ("quit)) {

Return $ this-> SMTP_ERROR ("Sending Quit Command");

}

Return True;

}

Function SMTP_SOCKOPON ($ Address)

{

IF ($ this-> relay_host == "") {

Return $ this-> SMTP_SOPOPEN_MX ($ address);

} else {

Return $ this-> SMTP_SOCKOPEN_RELAY ();

}

}

Function SMTP_SOCKOPOPEN_RELAY ()

{

$ this-> log_write ("trying to". $ this-> relay_host. ":". $ this-> smtp_port. "/ n");

$ this-> sock = @fsockopen ($ this-> relay_host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out);

IF ($ this-> SOCK && $ this-> smtp_ok ​​())) {

$ this-> log_write ("Error: Cannot CONNENCT TO RELAY HOST". $ this-> relay_host. "/ n");

$ this-> log_write ("Error:". $ Errstr. "(". $ errno. ") / n");

Return False;

}

$ this-> log_write ("Connected to Relay Host". $ this-> relay_host. "/ n");

Return True ;;

}

Function SMTP_SOCKOPEN_MX ($ Address)

{

$ domain = EREG_REPLACE ("^. @ ([^@] ) $", "/ 1", $ address;

IF (! @getmxrr ($ domain, $ mxhosts)) {$ this-> log_write ("Error: cannot resolve mx /""""""") ;/n ");

Return False;

}

Foreach ($ MXHOSTS AS $ Host) {

$ this-> log_write ("trying to". $ host. ":". $ this-> SMTP_PORT. "/ n");

$ this-> sock = @fsockopen ($ Host, $ this-> SMTP_Port, $ Errno, $ Errstr, $ this-> time_out);

IF ($ this-> SOCK && $ this-> smtp_ok ​​())) {

$ this-> log_write ("Warning: Cannot Connect To MX Host". $ host. "/ n");

$ this-> log_write ("Error:". $ Errstr. "(". $ errno. ") / n");

CONTINUE;

}

$ this-> log_write ("Connected to MX Host". $ host. "/ n");

Return True;

}

$ this-> log_write ("Error: Cannot CONNECT To Any MX Hosts (". IMPLODE (",", $ MXHOSTS). ") / n");

Return False;

}

Function SMTP_MESSAGE ($ HEADER, $ BODY)

{

FPUTS ($ this-> Sock, $ header. "/ r / n". $ body);

$ this-> smtp_debug (">" .str_replace ("/ r / n", "/ n". ">", $ header. "/ n>". $ body. "/ n>"));

Return True;

}

Function SMTP_EOM ()

{

FPUTS ($ THIS-> SOCK, "/ R);

$ this-> SMTP_DEBUG (". [eom] / n");

Return $ this-> SMTP_OK ();

}

Function SMTP_OK ()

{

$ response = str_replace ("/ r / n", ", fgets ($ this-> suck, 512));

$ this-> smtp_debug ($ response. "/ n");

IF (! EREG ("^ [23]", $ response)) {

FPUTS ($ THIS-> SOCK, "QUIT / R / N");

FGETS ($ THIS-> SOCK, 512);

$ this-> log_write ("Error: Remote Host Returned / """""""-");

Return False;

}

Return True;

}

Function SMTP_PUTCMD ($ CMD, $ Arg = "")

{

IF ($ arg! = "") {

IF ($ cmd == ") $ cmd = $ arg; Else $ cmd = $ cmd." "" $ arg;

}

FPUTS ($ THIS-> SOCK, $ CMD. "/ r / n");

$ this-> SMTP_DEBUG (">". $ cmd. "/ n");

Return $ this-> SMTP_OK ();

}

Function SMTP_ERROR ($ String)

{

$ this-> log_write ("error: error opcurred while". $ string. "./ n");

Return False;

}

Function log_write ($ Message)

{

$ this-> SMTP_DEBUG ($ Message);

IF ($ this-> log_file == "") {

Return True;

}

$ Message = Date ("m D h: i: s") .GET_CURRENT_USER (). "[". getmypid (). "]:". $ message;

IF (! @file_exists ($ this-> log_file) ||! ($ fp = @fopen ($ this-> log_file, "a")))))

$ this-> smtp_debug ("Warning: Cannot Open Log file /""""""/1is->log_file."/"/n");

Return False ;;

}

FLOCK ($ fp, lock_ex);

FPUTS ($ FP, $ Message);

Fclose ($ fp);

Return True;

}

Function Strip_Comment ($ address)

{

$ comment = "/ ([^ ()] * /);

While (EREG ($ Comment, $ Address) {

$ address = EREG_REPLACE ($ Comment, ", $ address;

}

Return $ address;

}

Function Get_address ($ address)

{

$ address = EREG_REPLACE ("([/ T / R / N]) ", ", $ address);

$ address = EREG_REPLACE ("^. * <( )>. * $", "/ 1", $ address;

Return $ address;

}

Function SMTP_DEBUG ($ Message)

{

IF ($ THIS-> Debug) {

Echo $ Message;

}

}

}

?>

Test.php

/ *

This is a test program !!!

Follow the instructions to set the following parameters, the following is set as an example of Tom.com.

* /

Require ("Class, SMTP.PHP");

$ SMTPSERVER = "SMTP.TOM.com"; // SMTP server

$ SMTPSERVERPORT = 25; // SMTP server port

$ SMTPUSERMAIL = "someone@tom.com"; // SMTP server user mailbox

$ SMTPEMAILTO = "someone@hotmail.com"; // Who sent?

$ SMTPUSER = "someone"; // SMTP server user account

$ SMTPASS = "somePass"; // SMTP server user password

$ mailsubject = "test subject"; // mail topic

$ mailbody = "

this is a test mail "; // message content

$ mailtype = "html"; // mail format (html / txt), txt is text messaging

$ SMTP = New SMTP ($ SMTPSERVER, $ SMTPSERVERPORT, TRUE, $ SMTPUSER, $ SMTPASS); // This TRUE inside is a use of authentication, otherwise it does not use authentication.

$ SMTP-> Debug = true; // Whether to display the sending debugging information

$ SMTP-> Sendmail ($ SMTPEMAILTO, $ SMTPUSERMAIL, $ MAILSUBJECT, $ MAILBODY, $ MAILTYPE

?>

转载请注明原文地址:https://www.9cbs.com/read-113826.html

New Post(0)