每周都要把打包好的文件发送给领导,搞来搞去嫌麻烦,查询网上各种资料后记录代码在此,参考资料:) 参考网址: https://www.freesion.com/article/83371338835/
1、基础知识:
python 发送邮件主要用到两个模块:
email 模块下有 mime 包,mime 包下常用的有三个模块:(text、image、multipart)
① MIMETex:(纯文本、HTML页面)
参数如下:
_text:文本内容
_subtype:文本类型:plain(默认)、html
_chartset:文本编码:中文为’utf-8’
② MIMEImage:(图片)
③ IMEMultipart:(组合,可包含文本和附件)
‘mixed’ :纯文本正文(text/plain)、超文本正文(text/html)、内嵌资源(图片,声音)、附件
‘related’ : 内嵌资源(图片,声音)
‘alternative’ : 纯文本正文(text/plain)、超文本正文(text/html
2、发送邮件:
(1)没有安装包的需要先安装:stmplib、email
pip install stmplib
pip install email
(2)导入包:
import smtplib
from email.header import Header
from email.utils import formataddr
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
(3)SMTP 服务器以及相关配置信息,以 QQ 邮箱为例:
开启POP3/STMP服务:设置 --> 账户 --> POP3/STMP服务 --> 开启
在 python 中设置 STMP 服务器信息
host = 'smtp.qq.com' # 发件人邮箱的SMTP服务器
sender = 'x@qq.com' # 发件人邮箱账号
password = 'password ' # 发件人邮箱密码(不是qq密码,通过设置--》账户--》开启--》授权码)
receivers = ['xx@qq.com'] # 接收人邮箱账号(可以多个,逗号隔开)
(4)创建邮件内容(包含:邮件内容、发件人、收件人、邮件主题)
只有邮件正文版:
# 简单邮件
msg = MIMEText('I love you nanW ~ ', 'plain', 'utf-8') # 邮件正文的内容、设置文本格式:plain、设置编码格式:utf-8
msg['From'] = formataddr(['j', sender]) # 发件人邮箱昵称、发件人邮箱账号
msg['To'] = formataddr(['sir', ','.join(receivers)]) # 收件人邮箱昵称、收件人邮箱账号
msg['Subject'] = "每周照片到啦,自动发送,有疑问请联系发件人" # 邮件主题
多形式组合版:
# 创建多形式组合邮件
msg = MIMEMultipart('mixed')
msg['From'] = formataddr(['j', sender]) # 发件人邮箱昵称、发件人邮箱账号
msg['To'] = formataddr(['sir', ','.join(receivers)]) # 收件人邮箱昵称、收件人邮箱账号
设置要发送的文件
# msg['To'] = ','.join(receivers)
msg['Subject'] = "每周照片到啦,自动发送,有疑问请联系发件人" # 邮件主题
#
# ######## 添加附件 - txt
# att_txt = MIMEText(open(r'D:\学习\python\email_file\I.txt', 'rb').read(),'base64','utf-8')
# att_txt["Content-Type"] = 'application/octet-stream'
# att_txt["Content-Disposition"] = 'attachment; filename="I.txt"'
# msg.attach(att_txt)
#
# ######## 添加附件- 图片
# att_img2 = MIMEText(open(r'D:\学习\python\email_file\love.jpg', 'rb').read(), 'base64', 'utf-8')
# att_img2['Content-disposition'] = 'attachment;filename="love.jpg"'
# msg.attach(att_img2)# 添加图片文件到邮件-附件中去
#
# ######## 添加附件 - excel
# att_excel = MIMEText(open(r'D:\学习\python\email_file\Love.xlsx', 'rb').read(),'base64','utf-8')
# att_excel["Content-Type"] = 'application/octet-stream'
# att_excel["Content-Disposition"] = 'attachment; filename="Love.xlsx"'
# msg.attach(att_excel)
######## 添加附件 -rar,zip
att_zip = MIMEText(open(r'C:/Users/Desktop/每周照片.zip', 'rb').read(),'base64','utf-8')
att_zip["Content-Type"] = 'application/octet-stream'
# att_zip["Content-Disposition"] = 'attachment; filename="中1901.zip"' #Content-disposition:设置文件在浏览器打开还是下载
# 附件名称为中文时的写法
att_zip.add_header("Content-Disposition", "attachment", filename=("gbk", "", "每周照片.zip"))
msg.attach(att_zip)
(5)发送邮件
try:
# 创建并登录SMTP服务器
server = smtplib.SMTP_SSL(host, 465) # 创建一个STMP对象,SSL加密(也可选择明文发送server = smtplib.SMTP(host, 25))
server.login(sender, password) # 登陆需要认证的SMTP服务器(发件人邮箱账号、密码)
# 发送邮件
server.sendmail(sender, receivers, msg.as_string()) # 发件人邮箱账号、收件人邮箱账号、发送邮件内容,as_string()是将msg(MIMEText对象或者MIMEMultipart对象)变为str
server.quit() # 断开STMP服务器链接
print ('邮件发送成功!')
except smtplib.SMTPException as e:
print ('Error: 邮件发送失败!', e)