【笔记】Python3发送邮件

前言

Python3发送邮件学习笔记

发送文本邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
host = "smtp.qq.com"
port = 465
login_email = "[email protected]"
login_password = "xxxxxxxxxxxxxxxx"
send_from = "[email protected]"
send_to = "[email protected]"
email_title = "标题"
email_content = "正文"

# 建立连接
con = smtplib.SMTP_SSL(host, port)

# 登录
con.login(login_email, login_password)

# 配置邮件
msg = MIMEMultipart()
msg["Subject"] = Header(email_title, "utf-8").encode()
msg["From"] = f"{send_from} <{send_from}>"
msg["To"] = send_to

# 插入文本内容
msg.attach(MIMEText(email_content, "plain", "utf-8"))

# 插入文件附件内容
#f = MIMEImage(open("src/filename.zip", "rb").read())
#f["Content-Disposition"] = 'attachment; filename = "newfilename.zip"'
#msg.attach(f)

# 发送邮件
con.sendmail(send_from, send_to, msg.as_string())

# 关闭连接
con.quit()

完成

参考文献

CSDN——酒坛坛儿^_^
哔哩哔哩——Python编程语言