阅读本篇文章大概需要 6 分钟。
SMTP:是发送邮件的协议,Python 内置对 SMTP 的支持,可以发送纯文本、html和带有附件的邮件。
Python 对 SMTP 的支持有 smtplib 和 email 两个模块,email 负责构造邮件,smtplib 负责发送邮件。
第一个参数为要发送邮件的内容,必须为 str 格式。
第二个参数为 subtype,传入 plain 为纯文本格式邮件,传入 html 为 html 格式邮件。
第三个参数为编码格式,一般填写 utf-8 保证多语言兼容性。
1. MIMEText 编辑纯文本邮件
例:
1
2
3
with open('abc.txt', 'r') as file_read:
content = file_read.read()
msg_1 = MIMEText(content, 'plain', 'utf-8')
2. MIMEText 编辑 html 邮件
例:
1
2
3
with open('abc.html', 'r') as file_read:
content = file_read.read()
msg_2 = MIMEText(content, 'html', 'utf-8')
3. MIMTImage 编辑图片邮件
例:
1
2
3
with open('1.jpg', 'r') as file_read:
content = file_read.read()
msg_3 = MIMEImage(content)
4. MIMEMultipart 编辑带附件邮件
例:
# 构建邮件信息
message = MIMEMultipart()
message['From'] = '发件箱地址'
message['To'] = '收件箱地址'
message['Subject'] = Header('邮件主题', 'utf-8').encode()
# 将内容附加到邮件主体中
message.attch(msg_1)
message.attch(msg_2)
message.attch(msg_3)
5. 登陆并发送邮件
实践出真知,我们结合一个小场景理解一下该过程:登录 163 邮箱向 qq 邮箱发送邮件。
代码实现分为三个步骤:
设置好服务器端信息
smtplib.SMTP(‘服务器地址’, 服务器端口号)
邮件主体信息
login() 登录 smtp 邮箱服务器
发送邮件
sendmail() 发送邮件,需要传入三个参数
参数一:发件箱 参数二:收件箱(注意需要[]包裹,这意味着你可以写多个邮件地址群发) 参数三:邮件正文信息
例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 设置登录及服务器信息
# SMTP 服务器地址 163为例
smtp_server = 'smtp.163.com'
# 邮箱登录用户名
login_mail_user = 'XXXX'
# 邮箱登录密码
login_mail_pass = 'XXXX'
# 发件箱
from_mail = 'XXXX@163.com'
# 收件箱
to_mail = ['XXXX@qq.com', ...]
# 获取服务器
# 网易 163 为例,端口号为 25
smtp = smtplib.SMTP(smtp_server, 25)
# 登陆邮箱
smtp.login(login_mail_user, login_mail_pass)
# 发送邮件
smtp.sendmail(from_mail, to_mail, message.as_string())
# 发送完毕,退出服务器
smtp.quit()
6. 项目实战
需求:
爬取百度首页 html 源代码
将源代码分别保存为 abc.txt 和 abc.html 文件到本地
将 abc.txt 和 abc.html 和本地 1.jpg 图片作为附件发送到指定邮箱内
项目代码:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# coding=utf-8
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import smtplib
import requests
# 爬取百度首页源码
response = requests.get('http://www.baidu.com')
response.encoding = 'utf-8'
with open('abc.html', 'w') as file_read:
file_read.write(response.text)
with open('abc.txt', 'w') as file_read:
file_read.write(response.text)
# 设置登录及服务器信息
mail_host = 'smtp.163.com'
mail_user = 'XXX'
mail_pass = 'XXX'
sender = 'XXX@163.com'
receivers = ['XXX@qq.com']
# 设置eamil信息
# 添加一个MIMEmultipart类,处理正文及附件
message = MIMEMultipart()
message['From'] = sender
message['To'] = receivers[0]
message['Subject'] = 'title'
# 推荐使用 html 格式的正文内容,这样比较灵活,可以附加图片地址,调整格式等
with open('abc.html', 'r') as f:
content = f.read()
# 设置html格式参数
part1 = MIMEText(content, 'html', 'utf-8')
# 添加一个txt文本附件
with open('abc.txt', 'r')as h:
content2 = h.read()
# 设置txt参数
part2 = MIMEText(content2, 'plain', 'utf-8')
# 附件设置内容类型,方便起见,设置为二进制流
part2['Content-Type'] = 'application/octet-stream'
# 设置附件头,添加文件名
part2['Content-Disposition'] = 'attachment;filename="abc.txt"'
# 添加照片附件
with open('1.jpg', 'rb')as fp:
picture = MIMEImage(fp.read())
# 与txt文件设置相似
picture['Content-Type'] = 'application/octet-stream'
picture['Content-Disposition'] = 'attachment;filename="1.jpg"'
# 将内容附加到邮件主体中
message.attach(part1)
message.attach(part2)
message.attach(picture)
# 登录并发送
try:
smtpObj = smtplib.SMTP(mail_host, 25)
# smtpObj.connect()
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(
sender, receivers, message.as_string())
print('success')
smtpObj.quit()
except smtplib.SMTPException as e:
print('error', e)
爱生活,爱 Coding!
领取专属 10元无门槛券
私享最新 技术干货