Use an SMTP. Class. php class to complete the SMTP Email sending task.
<?
Set_time_limit (120 );
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;
VaR $ sender;
/* Private variables */
VaR $ sock;
/* Constractor */
Function SMTP ($ relay_host = "", $ smtp_port = 25, $ auth = false, $ user, $ pass, $ sender)
{
$ 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-> sender = $ sender;
$ This-> host_name = "localhost"; // is used in HELO command
$ This-> LOG_FILE = "";
$ This-> logs = ""; // record the interaction process with the server
$ This-> sock = false;
}
/* Main function */
Function Sendmail ($ to, $ from, $ subject = "", $ body = "", $ mailtype, $ cc = "", $ BCC = "", $ additional_headers = "")
{
$ Sent = true;
$ 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. = "from:". $ from. "/R/N ";
$ Header. = "Subject:". $ subject. "/R/N ";
$ Header. = $ additional_headers;
$ Header. = "Date:". date ("R"). "/R/N ";
$ Header. = "X-mailer: 72e.net (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 )));
}
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 ");
}
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 AUTH command ");
}
If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass ))){
Return $ this-> smtp_error ("sending AUTH command ");
}
}
#
// If (! $ This-> smtp_putcmd ("mail", "From:". $ from ."")){
If (! $ This-> smtp_putcmd ("mail", "From: <". $ this-> sender. "> ")){
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 ));
// Echo "response =". $ response. "/R/N ";
$ This-> smtp_debug ($ response. "/N ");
// Echo "ereg 23 =". ereg ("^ [23]", $ response). "/N ";
If (! Ereg ("^ [23]", $ response )){
// Echo "@@@@@";
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 ");
// Echo "cmd =". $ cmd. "/R/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-> logs. = $ 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;
}
}
} // End class
?>
-----------------
Test. php
<?
// Send an OK email.
Include "SMTP. Class. php ";
// $ Smtpserver = "smtp.163.com"; // address of your SMTP Server
$ Smtpserver = "smtp.163.com ";
$ Port = 25; // The SMTP server port, usually 25
$ Smtpuser = "XXXXX"; // User Name of your SMTP Server
$ Smtppwd = "XXX"; // password for your logon to the SMTP server
$ Mailtype = "TXT"; // mail type. Optional values: txt or HTML. txt indicates a plain text email. html indicates an HTML email.
$ Sender = "usr@163.com ";
// Sender, which is generally the same as the username ($ smtpuser) for logging on to the SMTP server. Otherwise, sending may fail due to the setting of the SMTP server.
$ SMTP = new SMTP ($ smtpserver, $ port, true, $ smtpuser, $ smtppwd, $ sender );
$ SMTP-> DEBUG = true; // whether to enable debugging. It is only used for testing the program. Please comment out this line during official use.
$ To = "usr@163.com"; // recipient
$ Subject = "hello ";
$ Body = "the content you sent ";
$ Send = $ SMTP-> Sendmail ($ to, $ sender, $ subject, $ body, $ mailtype );
If ($ send = 1 ){
Echo "email sent successfully ";
} Else {
Echo "email sending failed <br/> ";
// Echo "Reason:". $ this-> SMTP-> logs;
}
?>