Use Socket to send an email-continue a good article and turn it online

Source: Internet
Author: User
Use Socket to send emails-continued author: limodou I have previously written an article about how to use socket programming to send emails to solve the problem that web servers do not support mail () function problems. It can also be used after my tests. However, many free email providers (starting from 263, SyntaxHighlighter.

Use Socket to send emails-continued author: limodou I have previously written an article about how to use socket programming to send emails to solve the problem that web servers do not support mail () function problems. It can also be used after my tests. Currently, many free email providers (starting from 263, 163, and xinlang net) have added the authentication function to the smtp function, making the original mail sending class unusable. After studying the corresponding smtp rfc, after many tests, I finally succeeded. So I would like to introduce it to you in an urgent mood. The SMTP authentication function is described here. I do not want to introduce the SMTP authentication function in detail, because I am not clear about it. for details, refer to [RFC 2554. The SMTP authentication function mainly adds the AUTH command. The AUTH command has multiple usage methods and multiple authentication mechanisms. Authentication mechanisms supported by AUTH mainly include LOGIN, CRAM-MD5 [note 1] and so on. LOGIN should be supported by most free email servers, both 263 and Sina. Sina also supports the CRAM-MD5 mechanism. The authentication mechanism is generally performed only once before the email is actually sent. After successful authentication, you can send emails as normal. The principle is Challenge-Response, that is, the server sends a command to ask the client to answer, and the client sends a Response based on the server's message. if the Response passes, the authentication succeeds, to continue processing. Let's give a brief introduction to these two types. S: indicates that the server returns, and C: indicates that the client sends messages. LOGIN should be relatively simple. The password-response process is as follows: 1 C: auth login 2 S: 334 dXNlcm5hbWU6 3 C: running 4 S: 334 cGFzc3dvcmQ6 5 C: cGFzc3dvcmQ6 6 S: 235 Authentication successful. 1. the client sends authentication commands to the server. 2. the server returns a base64 encoded string with a success code of 334. The encoded string is decoded as "username:", indicating that the client needs to send the user name. 3. the user name sent by the client using base64 encoding. "username:" is used here :". 4. the server returns a base64 encoded string with a success code of 334. The decoded string is "password:", which indicates that the client needs to send the user password. 5. the client sends a base64-encoded password, which is "password :". 6. if the authentication succeeds, the server returns a 235 code, indicating that the email can be sent successfully. For LOGIN authentication, the user name and password are base64 encoded and sent separately according to the server's requirements. (In my opinion, because base64 is a public encoding standard, it does not play much protection .) CRAM-MD5 mechanisms for CRAM-MD5 mechanisms can be referred to the [RFC 2195] specification, which is not described here. The password-answer mechanism is used to send an information string from the server, which consists of a random number, a timestamp, and a server address, and is Base64-encoded. After receiving the message, the client sends a string consisting of a user name, a space, and a summary, encoded in Base64. The Digest is obtained through the MD5 algorithm. This mechanism requires that the server and the client have the same encrypted string. After the client sends a summary, the server verifies its validity. if the summary is successfully sent, 235 is returned. How do I know what authentication is supported by the email server? In [RFC 821] of smtp, after successfully connecting to the mail server, the first command is generally "HELO ". However, in the email server that supports authentication, the first command should be changed to "EHLO" [note 2]. After the command is successful, the returned result of 263 may be EHLO hello 250-smtp.263.net [note 3] 250-PIPELINING 250-SIZE 10240000 250-ETRN 250-AUTH LOGIN 250 8 BITMIME, and you can see that 263 supports LOGIN authentication. Of course, if you already know how the email server is, there is no need to automatically judge it, but if you do not know it, you need to analyze the returned results. However, most email servers support the simplest LOGIN method. Now, modify sendmail. class. php3. It doesn't matter if you don't. This article provides the sendmail. class. php3 package file at the end, which can be downloaded. As for the example, you can write it according to this article. Modify sendmail. class. php3. here, only the focus of the modification is stated, rather than the overall analysis. First, let's review the idea of sendmail. class. php3 to let everyone know. Sendmail. class. php3 has four functions: the send_mail constructor class, which is used to initialize the send mail sending function for information, execute the socket command, send the do_command command to execute the function, and execute an smtp command, the show_debug function displays the display information. First, you must call the class constructor to initialize necessary parameters. For example, the smtp server address ($ smtp), the welcome message ($ welcome), and whether the call information ($ debug) is displayed ). At the same time, it also initializes some internal variables, such as the final execution command ($ lastact), the final response information ($ lastmessage), and the port number ($ port = 25 ). Then, the user generates the Mail information and calls the send () function to send the mail. In the send () function, a command is followed by a command according to smtp specifications (for details, refer to the previous article ). The command is executed by calling do_command. If do_command () fails to be executed, the program returns immediately; otherwise, the program continues to run down. If the display indicator is set, do_command () returns the display information when the command is sent and the message is responding. Now, you have an understanding of its operation. here is how to modify it. Modify the constructor (send_mail) because the previous send_mail class does not support the authentication function, you must first add the authentication information. Three parameters are added: $ auth, $ authuser, and $ authpasswd. $ Auth indicates whether to use the authentication function. $ Authuser and $ authpasswd are smtp Authentication usernames and passwords. they are consistent with pop3 according to the requirements of the corresponding email service provider, for example, 263. Most of them should be the same. In this way, three internal variables must be added to the internal variable table of the class: $ auth, $ user, and $ passwd. Modify the sending function (send) to change the sending command HELO to send EHLO. At the same time, you need to add the command to determine whether to perform authentication: // Change to Support esmtp ehlo command if ($ this-> auth) {$ this-> lastact = "EHLO ";} else $ this-> lastact = "HELO"; that is, if authentication is required, the EHLO command is sent; otherwise, the HELO command is also sent. Then, add authentication processing: // 200000002.28 add authentication processing if ($ this-> auth) {$ this-> lastact = "auth login". ""; if (! $ This-> do_command ($ this-> lastact, "334") {fclose ($ this-> fp); return false;} // return the user name, encode $ this-> lastact = base64_encode ($ this-> user) with base64 ). ""; if (! $ This-> do_command ($ this-> lastact, "334") {fclose ($ this-> fp); return false;} // return password, encode $ this-> lastact = base64_encode ($ this-> passwd) with base64 ). ""; if (! $ This-> do_command ($ this-> lastact, "235") {fclose ($ this-> fp); return false ;}} note, here only the auth login mechanism is implemented and the CRAM-MD5 is not implemented. The user name and password are required for the first time by default. The original function of modifying the command execution function (do_command) cannot display the multi-line response string. Change to/* 200000002.28. the returned information is displayed completely $ this-> lastmessage = fgets ($ this-> fp, 512 ); $ this-> show_debug ($ this-> lastmessage, "in"); */while (true) {$ this-> lastmessage = fgets ($ this-> fp, 512); $ this-> show_debug ($ this-> lastmessage, "in"); if ($ this-> lastmessage [3] =) or (empty ($ this-> lastmessage) break;} this class is changed. Under the send_mail Test class, I wrote a test applet to send a letter. However, for the sake of security, I did not use the username and password with real information, if you want to test it, change it to your own information. The program is as follows (send. php ): Send ("toemail," fromemail "," test "," This is a test! ");?> Conclusion The Test of 263 is smooth and fast. However, Sina is not easy to succeed, mainly because it times out and fails to receive the message. why? Note: Because sending smtp requires a user name and password, most smtp Authentication uses the same user name and password as pop3. Therefore, if you use this method, you may write the user name and password into the program and upload them to the server. However, this is not safe. Encryption is not necessarily easy to use, because the information is stored on the server, and the corresponding decryption information is also stored on the server. My suggestion is to apply for a mailbox dedicated to sending credit, so that others will not be afraid to know it. Hope this program is useful to you. Download sendmail. class. php3. Appendix: RFC 1869 SMTP Service Extensions RFC 2195 IMAP/POP AUTHorize Extension) RFC 2222 Simple Authentication and Security Layer RFC 2554 SMTP Service Extension for Authentication [note 1] CRAM = Challenge-Response Authentication Mechanism password-answer Authentication Mechanism MD5 is a digest algorithm, it is mainly used in RSA and PGP. [Note 2] for EHLO instructions, refer to [RFC 1869]. [Note 3] In the response string of the email server, if the response code is followed by a space (), the response string has only one line. if it is a minus sign (-), multiple lines exist, the last line of response code is followed by a space (). The ownership in this article belongs to limodou. Keep this information if you want to reprint it. Note: sendmail. class. php3: http://www.zphp.com/files/sendmail.class

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.