[Php] smtp. class. php Set_time_limit (120 );
Class smtp_mail
{
Var $ host; // host
Var $ port; // The port is generally 25.
Var $ user; // SMTP-authenticated account
Var $ pass; // authentication password
Var $ debug = false; // do you want to display the server session information?
Var $ conn;
Var $ result_str; // result
Var $ in; // The Command sent by the client
Var $ from_r; // The real source email box, which is generally the same as the smtp server's user name; otherwise, it may fail to be sent due to the setting of the smtp server.
Var $ mailformat = 0; // mail format 0 = plain text 1 = html mail
Function smtp_mail ($ host, $ port, $ user, $ pass, $ debug = false)
{
$ This-> host = $ host;
$ This-> port = $ port;
$ This-> user = base64_encode ($ user );
$ This-> pass = base64_encode ($ pass );
$ This-> debug = $ debug;
$ This-> socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // for detailed usage, see the manual.
If ($ this-> socket)
{
$ This-> result_str = "create SOCKET:". socket_strerror (socket_last_error ());
$ This-> debug_show ($ this-> result_str );
}
Else
{
Exit ("initialization failed, please check your network connection and parameters ");
}
$ This-> conn = socket_connect ($ this-> socket, $ this-> host, $ this-> port );
If ($ this-> conn)
{
$ This-> result_str = "create a SOCKET connection:". socket_strerror (socket_last_error ());
$ This-> debug_show ($ this-> result_str );
}
Else
{
Exit ("initialization failed, please check your network connection and parameters ");
}
$ This-> result_str = "server response:". socket_read ($ this-> socket, 1024 )."";
$ This-> debug_show ($ this-> result_str );
}
Function debug_show ($ str)
{
If ($ this-> debug)
{
Echo $ str ."
\ R \ n ";
}
}
Function send ($ from, $ to, $ subject, $ body)
{
If ($ from = "" | $ to = "")
{
Exit ("Enter the mailbox address ");
}
If ($ subject = "") $ sebject = "no title ";
If ($ body = "") $ body = "no content ";
$ All = "From:". $ from. "\ r \ n ";
$ All. = "To:". $ to. "\ r \ n ";
$ All. = "Subject:". $ subject. "\ r \ n ";
If ($ this-> mailformat = 1) $ All. = "Content-Type: text/html; \ r \ n ";
Else $ All. = "Content-Type: text/plain; \ r \ n ";
$ All. = "charset = gb2312 \ r \ n ";
$ All. = $ body;
/*
If you add $ All to the content, you can send MIME emails.
However, you still need to add a lot of programs.
*/
// The following is a session with the server
$ This-> in = "ehlo helo \ r \ n ";
$ This-> docommand ();
$ This-> in = "auth login \ r \ n ";
$ This-> docommand ();
$ This-> in = $ this-> user. "\ r \ n ";
$ This-> docommand ();
$ This-> in = $ this-> pass. "\ r \ n ";
$ This-> docommand ();
If (! Eregi ("235", $ this-> result_str )){
$ This-> result_str = "smtp authentication failed ";
$ This-> debug_show ($ this-> result_str );
Return 0;
}
$ This-> in = "mail from:". $ from. "\ r \ n ";
$ This-> docommand ();
$ This-> in = "rcpt to:". $ to. "\ r \ n ";
$ This-> docommand ();
$ This-> in = "DATA \ r \ n ";
$ This-> docommand ();
$ This-> in = $ All. "\ r \ n. \ r \ n ";
$ This-> docommand ();
If (! Eregi ("250", $ this-> result_str )){
$ This-> result_str = "email sending failed ";
$ This-> debug_show ($ this-> result_str );
Return 0;
}
$ This-> in = "QUIT \ r \ n ";
$ This-> docommand ();
// End and close the connection
Return 1;
}
Function docommand ()
{
Socket_write ($ this-> socket, $ this-> in, strlen ($ this-> in ));
$ This-> debug_show ("client command:". $ this-> in );
$ This-> result_str = "server response:". socket_read ($ this-> socket, 1024 )."";
$ This-> debug_show ($ this-> result_str );
}
} // End class
?>
First fill in the content in the above program in the file, such:
Var $ host = "smtp.sina.com.cn"; // host
Var $ port = "25"; // The port is generally 25
Var $ user = "users"; // SMTP-authenticated account
Var $ pass = "users123; // authentication password
Put this file under your website, as long as your website program accesses it ,.
Then you need to use the mail program to include such as: include ('smtp. class. php ');
Then use
$ Mails = new smtp_mail ();
If ($ mail-> send ("sender address", "recipient email address", "topic", "content ")){
Echo "sent successfully! ";
} Else {
Echo "the email server is busy. please try again later ";
}