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

Source: Internet
Author: User
Use Socket to send emails (using the SMTP server to be verified)

* 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:". 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 ";
$ 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:". socket_read ($ this-> socket, 1024 )."";
$ 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.