Use Socket to send emails (using the SMTP server to be verified)

Source: Internet
Author: User

<?

* Name: Use Socket to send an email
* Description: This class allows you to directly send emails using the SMTP server to be verified. For details, see limodou, author of "sending emails with Socket ".
* 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 tease this program maliciously, he is still very 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 ");
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.