smtplib of python provides a very convenient way to send E-mail. It simply encapsulates the smtp protocol.
The basic commands of the smtp protocol include:
HELO identifies the user to the server
MAIL initializes mail transfer mail from:
RCPT identifies a single mail recipient; Often followed by the MAIL command, there may be more than one rcpt to:
After a single or multiple RCPT commands, DATA indicates that all mail recipients have been identified and the data transfer has been initiated, ending with
VRFY is used to verify the existence of the specified user/mailbox; The server often disables this command for security reasons
EXPN validates the existence of a given mailbox list, and extending the mailbox list is often disabled
What commands does the HELP query server support
NOOP no operation, the server should respond OK
QUIT ends the session
RSET resets the session and the current transfer is cancelled
MAIL FROM specifies the sender address
RCPT TO specified recipient address
There are two ways to use smtp, one is direct mail delivery, that is, if you want to send email to the 163.com 163.com email server, then directly connect to the 163.com email server and send the message to the 163.com email server. The other one is a verified email. The process is, for example, if you want to send an email to email:.zzz@163.com, you don’t send it directly to 163.com. This means first connecting to sina.com’s smtp server, then authenticating, and then Posting the letter to sina.com before sending the letter to 163.com. sina.com will deliver the letter to 163.com.
The command flow of type 1 is basically like this:
1. helo
2. mail from
3. rcpt to
4. data
5. quit
However, the first mode of sending, rcpt to, is limited in that the recipient specified by rcpt to must exist on the server or it will not be received. First look at the code:
Note that 163.com has an anti-spam function, and this method of mail delivery is not guaranteed to pass the detection of anti-spam system. So 1 is generally not recommended for individuals to send in this way.
2 is a bit different:
1.ehlo
2.auth login
3.mail from
4.rcpt to
5.data
6.quit
Compared to the first one, there is one more authentication process, which is auth login.
What is mentioned above is the most common case, but what can’t be ignored is that many enterprise emails now support secure emails, that is, emails sent via SSL. How to send this? SMTP supports SSL secure mail in two ways. One is to open 1 465 port to receive ssl mail, and the other is to add 1 starttls command to the standard smtp port 25.
Let’s see how the first one works:
Here I have derived the new SMTP_SSL class from the original smtplib.SMTP, which handles ssl connections. The 192.168.2.10 I tested here is my own test server.
The second is the new starttls command, which is very simple. smtplib has this method called smtplib.starttls (). Of course, not all mail systems support secure mail, this needs to be confirmed from the return value of ehlo, if there is starttls, then support. Compared to the second method of sending regular mail, it only needs to add 1 line of code:
Note: in order to facilitate the above code, I did not judge the return value. Strictly speaking, I should judge the code returned under 1. In smtp protocol, only the return code is 2xx or 3xx can continue the next step.
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/267020.html