Use python to send and receive email example code
Mar 21, 2017 pm 01:44 PMAbout email
Before college, I basically didn’t use email, so I basically didn’t feel its existence and didn’t know its use; however, after college, as I got to know more and more people, my knowledge increased. It is becoming more and more widespread, and email has become a very important communication tool. Some university coursework requires an email to be sent to teachers, registering for a website requires an email, and looking for a job also requires an email. So what is the principle of an email?
Sending mail
SMTP protocol
SMTP (Simple Mail Transfer Protocol) is a simple mail transfer protocol. It is a set of protocols used to transfer mail from the source address to the destination address. Rules that govern how letters are relayed. The SMTP protocol belongs to the TCP/IP protocol suite, which helps each computer find the next destination when sending or relaying letters. Through the server specified by the SMTP protocol, you can send the E-mail to the recipient's server in just a few minutes.
SMTP module in python
Basic steps for using SMTP
Connecting to the server
Log in
Send service request
Log out
import smtplib from email import encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr def send_email(from_addr, to_addr, subject, password): msg = MIMEText("邮件正文",'html','utf-8') msg['From'] = u'<%s>' % from_addr msg['To'] = u'<%s>' % to_addr msg['Subject'] = subject smtp = smtplib.SMTP_SSL('smtp.163.com', 465) smtp.set_debuglevel(1) smtp.ehlo("smtp.163.com") smtp.login(from_addr, password) smtp.sendmail(from_addr, [to_addr], msg.as_string()) if name == "main": # 这里的密码是开启smtp服务时输入的客户端登录授权码,并不是邮箱密码 # 现在很多邮箱都需要先开启smtp才能这样发送邮件 send_email(u"from_addr",u"to_addr",u"主题",u"password")
The above demonstrates using smtplib to send emails, and uses SSL encryption, which is relatively safe. Email is used to construct the content of the email. What is sent here is plain text content. I think the most important thing to pay attention to is the password of the email here. . In addition, each company's mail server and port may be different. You can check it before using it.
Here are some commonly used ones:
SMTP server | SSL protocol port | Non-SSL protocol port | |
---|---|---|---|
smtp .163.com | 465 or 994 | 25 | |
smtp.11.com | 465 or 587 | 25 |
- Connect to the server
- Login
- Issue a service request
- Exit
login(user, pass) | |
list() | |
select() | |
search() | |
The above is the detailed content of Use python to send and receive email example code. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

A complete guide to golang function debugging and analysis

Share several .NET open source AI and LLM related project frameworks
