Python email sending tutorial I searched out a lot when I searched on the website,But all said a lot of principles and then came out the implementation code,I tested sending emails with the code given without success,It took me a long time to find out why,This is all caused without a detailed environment debugging,So today I have a detailed tutorial,Step-by-step tutorial from environment debugging to code implementation,I hope to help those who are still struggling to find a solution but have not been able to solve it effectively.
First understand smtp (Simple Mail Transfer Protocol). The mail transfer agent uses the smtp protocol to send emails to the recipient's mail server.smtp protocol can only be used to send mail,Cannot be used to receive mail,And most mail sending servers use smtp protocol. The default TCP port number for the smtp protocol is 25.
The above said is the mail sent using the smtp protocol,So you need to check whether your sender's mailbox has the smtp protocol enabled.I tested using the email of 163.com as the sender's email.The smtp protocol is enabled as shown below.
1. Login to 163.com mailbox (Netease mailbox), as shown below
2. See that there is a "Set up"option, click it,Then selectfrom the drop-down menu"Pop3/smtp/imap"
3. As shown in the figure below,The two in the red box above must be checked.If unchecked,If you choose to enable it, you can check it.Then you can seein the red box below:smtp server:smtp.163.com
After the above settings, the environment is debugged.The code can be implemented below,It is recommended to use pycharm4.5.3 editor to write code,The following code works in both python2.7 and python3.4 tests.
#coding:utf-8 #Force the UTF-8 encoding format import smtplib #Load smtplib module from email.mime.text import mimetext from email.utils import formataddr my_sender="Sender email account" #Sender email account,For easy maintenance later,So written as a variable my_user="recipient email account" #recipient email account,For easy maintenance later,So written as a variable def mail (): ret=true try: msg=mimetext ("Fill in the email content", "plain", "utf-8") msg ["from"]=formataddr (["Sender's email nickname", my_sender]) #Corresponding sender's email nickname and sender's email account in parentheses msg ["to"]=formataddr (["Recipient email nickname", my_user]) #Corresponding recipient email nickname and recipient email account number in brackets msg ["subject"]="Subject" #Subject of the message,Can also be said to be the title server=smtplib.smtp ("smtp.xxx.com", 25) #The smtp server in the sender's mailbox, the port is 25 server.login (my_sender, "sender email password") #The email address and email password of the sender are in parentheses. server.sendmail (my_sender, [my_user,], msg.as_string ()) #The brackets correspond to the sender's email account, the recipient's email account, and the sending email server.quit () #This sentence means to close the connection except exception:#If the statement in try is not executed,Will execute the following ret=false ret=false return ret ret=mail () if ret: print ("ok") #If the sending is successful, it will return ok, wait for about 20 seconds to receive the mail else: print ("filed") #If sending fails, it will return filed
If the sending is successful, it will return ok, otherwise the execution is unsuccessful.As shown below:
Although the code is ready to use,But some people are unaware that they are blind tossing,Play and break the code,So here are a few points of knowledge to solve your doubts.
1. Seeing "#coding:utf-8" in the first line of the code above, I believe that many people who read this code do not pay much attention to this line.Anyway, this has nothing to do with the code implementation,Just select the utf-8 format when saving;Let me tell you, this line of code is very important in python,It is best to write a mandatory character encoding for each page,Because the python2 version recognizes the ascii encoding by default, it is not recognized when Chinese characters appear in python,The error message appears as shown below:
Error messageExample:file "f:/python/s12/pymail.py", line2
syntaxerror:non-asciicharacter "\ xe5" infilef:/python/s12/pymail.pyonline2, butnoencodingdeclared;see http://www.python.org/peps/pep-0263.htmlfordetails
So when the above coding error message appears,Can be added to the first line of the page
#coding:utf-8 #Force encoding to UTF-8
#coding:gbk #Force encoding to gbk
- Python implements a method for sending email to qq mailbox
- Python implements automatic mail sending
- Method for automatically monitoring website and sending email alert using Python
- Python implements asynchronous sending mail service based on smtplib
- Method for monitoring abnormal website running and sending mail
- Python implementation of automatic sending mail to send multiple people, group sending, multiple attachments example
- Send mail template in python [normal mail, mail with attachments, mail with pictures]
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- dart - flutter: the instance member'stars' can't be accessed in an initializer error