The original code is as follows:
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; // source email box
Var $ to; // target mailbox
Var $ subject; // topic
Var $ body; // content
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: <font color = # cc0000>". socket_read ($ this-> socket, 1024). "</font> ";
$ This-> debug_show ($ this-> result_str );
}
Function debug_show ($ str)
{
If ($ this-> debug)
{
Echo $ str. "<p> rn ";
}
}
Function send ($ from, $ to, $ subject, $ body)
{
If ($ from = "" | $ to = "")
{
Exit ("Enter the mailbox address ");
}
If ($ subject = "") $ sebject = "no title ";
If ($ body = "") $ body = "no content ";
$ This-> from = $ from;
$ This-> to = $;
$ This-> subject = $ subject;
$ This-> body = $ body;
$ All = "from:". $ this-> from. "n ";
$ All. = "to:". $ this-> to. "n ";
$ All. = "subject:". $ this-> subject. "n ";
$ All. = $ this-> body;
/*
For example, you can send mime mail after adding $ all content.
However, you still need to add a lot of programs.
*/
// The following is a session with the server
$ This-> in = "ehlo helorn ";
$ This-> docommand ();
$ This-> in = "auth loginrn ";
$ This-> docommand ();
$ This-> in = $ this-> user. "rn ";
$ This-> docommand ();
$ This-> in = $ this-> pass. "rn ";
$ This-> docommand ();
$ This-> in = "mail from:". $ this-> from. "rn ";
$ This-> docommand ();
$ This-> in = "rcpt to:". $ this-> to. "rn ";
$ This-> docommand ();
$ This-> in = "datarn ";
$ This-> docommand ();
$ This-> in = $ all. "rn. rn ";
$ This-> docommand ();
$ This-> in = "quitrn ";
$ This-> docommand ();
// End and close the connection
}
Function docommand ()
{
Socket_write ($ this-> socket, $ this-> in, strlen ($ this-> in ));
$ This-> debug_show ("client command:". $ this-> in );
$ This-> result_str = "server response: <font color = # cc0000>". socket_read ($ this-> socket, 1024). "</font> ";
$ This-> debug_show ($ this-> result_str );
}
}
// This is my test. I use smtp.163.com, and your mailbox must be 163.com. Otherwise, you will not be able to send it !!
// When you use this class, you can change it to your own mailbox.
$ Smtp = new smtp_mail ("smtp.163.com", "25", "t_design", "000000", true );
// If you need to display the session information, modify the above
// $ Smtp = new smtp_mail ("smtp.163.com", "25", "your 163.com account", "your password", true );
$ Smtp-> send ("t_design@163.com", "t_beijing@yahoo.com.cn", "Hello", "Hello ");
?>