Use Socket to send an email -- continue smtp Authentication

Source: Internet
Author: User
Author: limodounbsp; 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 the mail () function. It can also be used after my tests. However, at present, many free email providers (starting from 263, 163, and xinlang net) all add smtp functions.

Author: limodou



I have previously written an article about how to use socket programming to send emails, so that the web server does not support

Question about using the mail () function. It can also be used after my tests. But currently many free email providers (from 263

Beginning, 163, and xinlang network is about to begin) the authentication function is added to the smtp function, making the original mail sending class unusable. In

After studying the corresponding smtp rfc, I finally succeeded after many tests. So with an urgent heart

I would like to introduce you.



SMTP Authentication

I do not want to introduce the SMTP authentication function in detail here, 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. AUTH support

The authentication mechanism mainly includes LOGIN, CRAM-MD5 [note 1] and so on. LOGIN should be supported by most free email servers, 263

And Sina. Sina also supports the CRAM-MD5 mechanism. The authentication mechanism is generally only performed before an email is actually sent, and only

Run the command once. After successful authentication, you can send emails as normal. The principle is password-response (Challenge-

Response), that is, the server sends a command to ask the client to answer, the client sends a Response based on the server's information, if

If the answer is received, the authentication is successful and you can proceed. Let's give a brief introduction to these two types. S: indicates that the server returns,

C: The client sends the message.





LOGIN

It should be relatively simple. The password-response process is as follows:



1 C: AUTH LOGIN

2 seconds: 334 dXNlcm5hbWU6

3 C: dXNlcm5hbWU6

4S: 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 decoded string is "username:", which indicates that the customer must

Sending 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:", indicating that the customer is required.

Client sends 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 encoded in base64 and sent separately according to the server requirements.

You can. (In my opinion, because base64 is a public encoding standard, it does not play much protection .)

The CRAM-MD5 mechanism

For more information about the CRAM-MD5 mechanism, see [RFC 2195. Password-answering machine

The server sends an information string consisting of a random number, timestamp, and server address, which 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. Abstract

It can be obtained through the MD5 algorithm. This mechanism requires that the server and the client have the same encrypted string. After the client sends the abstract

The server verifies its validity. after successful verification, 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 authenticated email server, the first command should be changed to "EHLO" [note 2]. After the command is successful, 263 may return:



EHLO hello

250-smtp.263.net [note 3]

250-PIPELINING

250-SIZE 10240000

250-ETRN

250-AUTH LOGIN

250 8 BITMIME

As a result, we can see that 263 supports LOGIN authentication. Of course, if you already know how the email server works

It is necessary to automatically determine, but if you do not know, you need to analyze the returned results. However, most email servers

All support the simplest LOGIN method.



Now, modify sendmail. class. php3. It doesn't matter if you don't. This article will be mentioned at the end.

You can download the package file sendmail. class. php3. As for the example, you can write it according to this article.



Modify sendmail. class. php3

Here, only the focus of the changes is stated, rather than a comprehensive analysis.



First, let's review the idea of sendmail. class. php3 to let everyone know.



Sendmail. class. php3 has four functions:



The constructor of the send_mail class for information initialization.

Send mail sending function, execute socket command, send mail

The do_command command executes the function, executes an smtp command, and processes the returned results.

Show_debug function

First, you should call the class constructor to initialize necessary parameters. Such as smtp server address ($ smtp), Huan

Welcome information ($ welcome) and whether to display the call information ($ debug ). At the same time, some internal variables need to be initialized, such as the last execution.

Command ($ lastact), the final response message ($ 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, according to smtp specifications,

One command followed by one command (see the previous article for details ). The command is executed by calling do_command ().

Current. If do_command () fails to be executed, the program returns immediately; otherwise, the program continues to run down. If you have set display

Information flag, do_command () will return the calling 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 send_mail class does not support authentication, you must add authentication information first. 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, according to the requirements of the corresponding mail service provider, for example, 263 is the same as pop3

. 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, $ passwd.



Modify the sending function)

Change the send command HELO to send EHLO. At the same time, it is necessary to determine whether to perform authentication:



// Esmtp ehlo command is supported.

If ($ this-> auth)

 

Else

$ This-> lastact = "HELO ";

That is, if authentication is required, the EHLO command is sent; otherwise, the HELO command is also sent.



Then, add the authentication process:



// Add authentication process for 200000002.28

If ($ this-> auth)

 



// Return the username, which is Base64-encoded

$ This-> lastact = base64_encode ($ this-> user )."

";

If (! $ This-> do_command ($ this-> lastact, "334 "))

 



// Return password, encoded in base64

$ This-> lastact = base64_encode ($ this-> passwd )."

";

If (! $ This-> do_command ($ this-> lastact, "235 "))

 

}

Note that here only the auth login mechanism is implemented and the CRAM-MD5 is not implemented. And no judgment on the information returned by the server

The user name and password are required for the first time by default.



Modify the command execution function (do_command)

The original function cannot display the case where the response string is multiple rows. To:



/* Changed. the returned information is displayed completely.
$ This-> lastmessage = fgets ($ this-> fp, 512 );

$ This-> show_debug ($ this-> lastmessage, "in ");

*/

While (true)

 

The class is changed.



Test the send_mail class

The following is a test applet that I have compiled to send a letter, but for security, I will use the user name and password

No real information is used. if you want to test it, change it to your own information. The program is as follows (send. php ):



<?

Include ("sendmail. class. php3 ");



$ Sendmail = new send_mail ("smtp.263.net", true, "username", "password", "hello ",

True );

$ Sendmail-> send ("toemail," fromemail "," test "," This is a test! ");

?>

Conclusion

263 of the tests are smooth and fast. However, Sina is not easy to succeed, mainly because it times out and receives messages successfully.

No. why?



Note: Because sending smtp requires a user name and password, most smtp Authentication uses the same user name and port 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

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: related RFC



RFC 1869 SMTP Service Extensions

RFC 2195 IMAP/POP AUTHorize Extension (which has instructions on CRAM-MD5)

RFC 2222 Simple Authentication and Security Layer

RFC 2554 SMTP Service Extension for Authentication



--------------------------------------------------------------------------------

[Note 1]

CRAM = Challenge-Response Authentication Mechanism password-Response Authentication Mechanism

MD5 is a digest algorithm mainly used in RSA and PGP.

[Note 2]

For more information about EHLO, see [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, and the response code of the last line 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.cl

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.