Python simple SMTP send mail detailed tutorial with code, pythonsmtp send mail
Introduction
I searched for a lot of Python mail sending tutorials during website search, but I talked about a lot of principles and then launched the implementation code, I failed to send emails using the code I provided. I found the cause for a long time later. This is due to the absence of a detailed environment debugging. So I am going to issue a detailed tutorial today, one step from environment debugging to code implementation is a complete tutorial, hoping to help those who are still struggling to find a solution but are still unable to get an effective solution.
SMTP protocol
First, understand SMTP (Simple Mail Transfer Protocol). The mail transfer agent uses the SMTP protocol to send an email to the recipient's mail server. The SMTP protocol can only be used to send mails, but not to receive mails. Most mail sending servers use the SMTP protocol. The default TCP port number of SMTP protocol is 25.
Environment debugging
As mentioned above, the SMTP protocol is used to send emails. Therefore, you must first check whether the SMTP protocol is enabled in your sender's mailbox. If you do not need to enable the SMTP protocol, I tested the use of 163.com as the sender's mailbox and enabled the SMTP protocol, as shown in.
1. log on to the 163.com mailbox (Netease mailbox), as shown in figure
2. You can see a "set" option in the function bar on the mailbox. Click this option and select "POP3/SMTP/IMAP" from the drop-down menu"
3. As shown in, the two values in the red box above must be checked. If not selected, you can check them if you want to enable them. Then, you can see the SMTP server: smtp.163.com in the red box below.
Implementation Code
After the above settings, the environment has been debugged and the code can be implemented below. We recommend that you use the pycharm4.5.3 editor to compile the code. The following code can be used in python2.7 and python3.4 tests.
1 # coding: UTF-8 # force the use of UTF-8 encoding format 2 import smtplib # load the smtplib Module 3 from email. mime. text import MIMEText 4 from email. utils import formataddr 5 my_sender = 'sender's email account' # sender's email account. To facilitate subsequent maintenance, the variable 6 my_user = 'recipient's email account '# recipient's email account is written, for later maintenance convenience, the variable 7 def mail (): 8 ret = True 9 try: 10 msg = MIMEText ('fill in the mail content', 'plain ', 'utf-8') 11 msg ['from'] = formataddr (["sender's email nickname", my_sender]) # corresponding sender's email nickname and sender's email account 12 msg ['to'] = formataddr (["recipient's email nickname", my_user]) # corresponding recipient's email nickname and recipient's email account 13 msg ['subobject'] = "topic" # Subject of the email, which can also be said to be title 14 15 server = smtplib. SMTP ("smtp.xxx.com", 25) # The SMTP server in the sender's mailbox. The port is 2516 server. login (my_sender, "sender's email password") # It corresponds to the sender's email account and password 17 server. sendmail (my_sender, [my_user,], msg. as_string () # It corresponds to the sender's email account, recipient's email account, and email sending 18 server. quit () # This sentence indicates that the connection is closed. 19 'texception: # If the statement in try is not executed, the following ret = False20 ret = False21 return ret22 23 ret = mail () 24 if ret: 25 print ("OK") is executed. # if the message is sent successfully, OK is returned, wait about 20 seconds to receive the email 26 else: 27 print ("filed") # If the email fails to be sent, filed will be returned.
If the message is sent successfully, OK is returned. Otherwise, execution fails, for example:
Broaden horizons
Although the code can be used, it is inevitable that some people will be confused and play with the code, so here are a few knowledge points to solve your doubts.
1. the first line of the code above does not contain "# coding: UTF-8". I believe many people who read this Code do not pay much attention to this line. It has nothing to do with code implementation, you just need to select the UTF-8 format for saving. It is important to know that this line of code is very important in python. It is best to add mandatory character encoding to every page, because python2 recognizes ASCII encoding by default, it does not recognize Chinese Characters in python, as shown in the error message:
Error message example: File "F:/python/s12/pymail. py", line 2
SyntaxError: Non-ASCII character '\ xe5' in file F:/python/s12/pymail. py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
So when the above Encoding Error prompt appears, you can add
# Coding: UTF-8 # forcibly encoded as UTF-8
Or
# Coding: GBK # forcibly encoded as GBK