<?
* Name: Use SOCKET to send an email
* Description: This class allows you to directly send emails using the SMTP server to be verified. For more information, seeArticleBy sending an email via socket: limodou
* This article is earlier. It uses the SMTP server to send emails without verification. Now, basically all SMTP servers need verification, so the class in this article
It doesn't make a lot of sense! For more information, see [RFC 1869] And the PHP manual !! What is different from the above is that I am not using the fsockopen () function.
Let's take a look !! I just passed the test. It's amazing !!
In fact, if you rewrite this class, you can directly send emails with attachments... I will write it for you in a few days!
I just passed the debugging. If you don't try to tease thisProgramHe is still obedient. In a few days, I will improve the function of sending attachments to him !!
Even if your VM does not support the mail function, you are not afraid of it now! Try this now!
Please respect others' successful work. Please keep this copyright information. Thank you!
Author: Small dew 3.3, welcome to contact me for communication PHP qq6550382
Mail: cleverzhang@tom.com
*/
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> \ 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 ";
$ 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 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 ();
$ This-> In = "mail from:". $ this-> from. "\ r \ n ";
$ This-> docommand ();
$ This-> In = "rcpt to:". $ this-> to. "\ r \ n ";
$ This-> docommand ();
$ This-> In = "data \ r \ n ";
$ This-> docommand ();
$ This-> In = $ all. "\ r \ n. \ r \ n ";
$ This-> docommand ();
$ This-> In = "Quit \ r \ n ";
$ 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", "Your 163.com account", "your password ");
// 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 ("Your 163.com account @ 163.com"," target address "," hello "," hello ");
?>