简介
当你有个on-the-fly的线上程序,需要定期获取结果或进程记录时,使用Python创建一个邮件发送程序难道不是一件非常有意思的事情吗?发送email就跟我们去邮局寄信是相似的。我们去寄信,当地必须要有一个邮局,同样,电子邮局需要给每个发email的人一个独立的邮箱,并配有账号和密码。通过电子邮局的中转将发件人的信息转达给收件人。
实现过程
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender = '734513xx7@qq.com' # 发件人账号
senderPSW = 'cjvabvrtlxxxxx' # 发件人账号的授权码
receivers = ['119871xxx1@qq.com'] # 收件人
msg = MIMEMultipart()
msg['From'] = Header(sender)
msg['To'] = Header('杰克', 'utf-8') # 收件人昵称
msg['Subject'] = Header('你有一份邮件等待查收', 'utf-8') # email主题
# 正文
mail_body = MIMEText('亲爱的XX,您好!恭喜您中了一等奖', 'plain', 'utf-8')
msg.attach(mail_body)
# 附件
# 文本
att1 = MIMEText(open('sample.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="sample.txt"'
msg.attach(att1)
# 图片
att2 = MIMEText(open('sample.png', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="sample.png"'
msg.attach(att2)
try:
smtpObj = smtplib.SMTP_SSL('smtp.qq.com', 465) # 465
smtpObj.login(sender, senderPSW)
smtpObj.sendmail(sender, receivers, msg.as_string())
smtpObj.quit()
print('邮件发送成功!')
except smtplib.SMTPException:
print("Error: 邮件发送失败!")
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
importsmtplib
fromemail.headerimportHeader
fromemail.mime.textimportMIMEText
fromemail.mime.multipartimportMIMEMultipart
sender='734513xx7@qq.com'# 发件人账号
senderPSW='cjvabvrtlxxxxx'# 发件人账号的授权码
receivers=['119871xxx1@qq.com']# 收件人
msg=MIMEMultipart()
msg['From']=Header(sender)
msg['To']=Header('杰克','utf-8')# 收件人昵称
msg['Subject']=Header('你有一份邮件等待查收','utf-8')# email主题
# 正文
mail_body=MIMEText('亲爱的XX,您好!恭喜您中了一等奖','plain','utf-8')
msg.attach(mail_body)
# 附件
# 文本
att1=MIMEText(open('sample.txt','rb').read(),'base64','utf-8')
att1["Content-Type"]='application/octet-stream'
att1["Content-Disposition"]='attachment; filename="sample.txt"'
msg.attach(att1)
# 图片
att2=MIMEText(open('sample.png','rb').read(),'base64','utf-8')
att2["Content-Type"]='application/octet-stream'
att2["Content-Disposition"]='attachment; filename="sample.png"'
msg.attach(att2)
try:
smtpObj=smtplib.SMTP_SSL('smtp.qq.com',465)# 465
smtpObj.login(sender,senderPSW)
smtpObj.sendmail(sender,receivers,msg.as_string())
smtpObj.quit()
print('邮件发送成功!')
exceptsmtplib.SMTPException:
print("Error: 邮件发送失败!")
首先,需要导入smtplib模块,专门负责发送邮件。MIMEText用于构建正文和附件内容,MIMEMultipart用于构建带附件的邮件对象,Header用于构建邮件的头部,包括发件人、收件人和主题。
senderPSW变量是发件人账号的授权码,而不是邮箱密码,对于qq邮箱,请看这里申请。
实例化一个MIMEMultipart邮件对象msg。使用Header()函数传入发件人、收件人和主题。
下一步,使用msg的attach()方法将正文mail_body附加上去。
接着构造两个附件att1和att2。其中使用MIMEText()函数将sample.txt和sample.png以base64的编码格式包装好。
MIMEText(open('sample.txt', 'rb').read(), 'base64', 'utf-8')
1
MIMEText(open('sample.txt','rb').read(),'base64','utf-8')
这三个参数的含义:
打开文件并读取得到的字节
字节编码格式为base64
显示时的编码格式:utf-8
需要注意的是:
att1["Content-Disposition"] = 'attachment; filename="sample.txt"'
1
att1["Content-Disposition"]='attachment; filename="sample.txt"'
filename为附件的名称,构造完成后,需要使用attach()方法将附件添加进msg中。
准备工作做好后,就可以向电子邮局投邮件了。
这里使用的是qq邮箱,它的电子邮局是
smtp.qq.com ,端口号为465。
运行程序,如果不出错误的话,几秒钟后就能收到反馈信息,提示“邮件发送成功!”。