No problems tested
<?
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 = "", $ smtp_port = 25, $ auth = false, $ user, $ pass)
{
$ This-> DEBUG = false;
$ This-> smtp_port = $ smtp_port;
$ This-> relay_host = $ relay_host;
$ This-> time_out = 30; // is used in fsockopen ()
#
$ This-> auth = $ auth; // auth
$ This-> User = $ user;
$ This-> pass = $ pass;
#
$ This-> host_name = "localhost"; // is used in HELO 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 ($ from ));
$ Body = ereg_replace ("(^ | (/R/n) (//.)", "// 1. // 3", $ body );
$ Header. = "mime-version: 1.0/R/N ";
If ($ mailtype = "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) = explode ("", microtime ());
$ Header. = "message-ID: <". date ("ymdhis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. ">/R/N ";
$ To = explode (",", $ this-> strip_comment ($ ));
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-> sock );
$ This-> log_write ("disconnected from remote host/N ");
}
Echo "<br> ";
Echo $ header;
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 ");
}
# Auth
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 ($ header, $ body )){
Return $ this-> smtp_error ("sending message ");
}
If (! $ This-> smtp_eom ()){
Return $ this-> smtp_error ("sending <CR> <LF>. <CR> <LF> [Eom]");
}
If (! $ This-> smtp_putcmd ("quit ")){
Return $ this-> smtp_error ("sending quit command ");
}
Return true;
}
Function smtp_sockopen ($ address)
{
If ($ this-> relay_host = ""){
Return $ this-> smtp_sockopen_mx ($ address );
} Else {
Return $ this-> smtp_sockopen_relay ();
}
}
Function smtp_sockopen_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/" ". $ domain."/"/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/n./R/N ");
$ This-> smtp_debug (". [Eom]/n ");
Return $ this-> smtp_ OK ();
}
Function smtp_ OK ()
{
$ Response = str_replace ("/R/N", "", fgets ($ this-> sock, 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/" ". $ response."/"/N ");
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 occurred 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, ""))){
$ This-> smtp_debug ("Warning: cannot open log file/" ". $ this-> 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. "<br> ";
}
}
Function get_attach_type ($ image_tag ){//
$ Filedata = array ();
$ Img_file_con = fopen ($ image_tag, "R ");
Unset ($ image_data );
While ($ tem_buffer = addslashes (fread ($ img_file_con, filesize ($ image_tag ))))
$ Image_data. = $ tem_buffer;
Fclose ($ img_file_con );
$ Filedata ['context'] = $ image_data;
$ Filedata ['filename'] = basename ($ image_tag );
$ Extension = substr ($ image_tag, strrpos ($ image_tag, "."), strlen ($ image_tag)-strrpos ($ image_tag ,"."));
Switch ($ extension ){
Case ". GIF ":
$ Filedata ['type'] = "image/GIF ";
Break;
Case ". GZ ":
$ Filedata ['type'] = "application/X-gzip ";
Break;
Case ". htm ":
$ Filedata ['type'] = "text/html ";
Break;
Case ". html ":
$ Filedata ['type'] = "text/html ";
Break;
Case ". jpg ":
$ Filedata ['type'] = "image/JPEG ";
Break;
Case ". tar ":
$ Filedata ['type'] = "application/X-tar ";
Break;
Case ". txt ":
$ Filedata ['type'] = "text/plain ";
Break;
Case ". Zip ":
$ Filedata ['type'] = "application/zip ";
Break;
Default:
$ Filedata ['type'] = "application/octet-stream ";
Break;
}
Return $ filedata;
}
}
//###################################### ####
$ Smtpserver = "smtp.163.com"; // SMTP Server
$ Smtpserverport = 25; // SMTP server port
$ Smtpusermail = "siren_0203@163.com"; // user email address of the SMTP server
$ Smtpemailto = ""; // who to send
$ Smtpuser = ""; // User Account of the SMTP server
$ Smtppass = ""; // SMTP server user password
$ Mailsubject = "ASDFASDF"; // Email Subject
$ Mailbody = "$ Mailtype = "html"; // The email format (html/txt). txt is a text email.
######################################## ##
$ SMTP = new SMTP ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // here, true indicates that authentication is used; otherwise, authentication is not used.
$ SMTP-> DEBUG = true; // whether to display the sent debugging information
$ SMTP-> Sendmail ($ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody, $ mailtype)
?>