python 发送邮件带附件(txt、图片、excel、压缩包等) | 您所在的位置:网站首页 › 电脑上怎么发送邮件以附件的形式发送 › python 发送邮件带附件(txt、图片、excel、压缩包等) |
(1)python 发送邮件主要用到两个模块: ① smtplib: 发送邮件(连接邮箱服务器、登录邮箱、发送邮件) ② email: 构造邮件内容(发件人、收件人、主题、正文、附件等) (2)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服务 --> 开启,图示:
(4)创建邮件内容(包含:邮件内容、发件人、收件人、邮件主题) ① 只有邮件正文 msg = MIMEText('I love you nanW ~ ', 'plain', 'utf-8') # 邮件正文的内容、设置文本格式:plain、设置编码格式:utf-8 msg['From'] = formataddr(['dddd', sender]) # 发件人邮箱昵称、发件人邮箱账号 msg['To'] = formataddr(['wwww', ','.join(receivers)]) # 收件人邮箱昵称、收件人邮箱账号 msg['Subject'] = "❤ love ❤" # 邮件主题② 添加附件:txt、图片、excel、压缩包 # 创建多形式组合邮件 msg = MIMEMultipart('mixed') msg['From'] = formataddr(['dddd', sender]) # 发件人邮箱昵称、发件人邮箱账号 msg['To'] = formataddr(['wwww', ','.join(receivers)]) # 收件人邮箱昵称、收件人邮箱账号 # msg['To'] = ','.join(receivers) msg['Subject'] = "❤ love ❤" # 邮件主题 ######## 添加附件 - 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'D:\学习\python\email_file\You.rar', 'rb').read(),'base64','utf-8') att_zip["Content-Type"] = 'application/octet-stream' att_zip["Content-Disposition"] = 'attachment; filename="You.rar"' 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) 3、附录:(1)各类邮箱对应的服务器信息 邮箱STMP服务器登录口令支持加密方式对应端口号163stmp.163.com个人设置授权码明文 / SSL加密25/465126smtp.126.com个人设置授权码明文 / SSL加密25/465QQsmtp.qq.com系统分配授权码明文 / SSL加密 / TLS加密25/465/587Gmailsmtp.gmail.com邮箱登录密码TLS加密587(2) Content-disposition:设置文件在浏览器打开还是下载 ① attachment: 作为附件下载 ② inline: 在线打开 Content-Type:MIME类型,在Http协议消息头中,表示具体请求中的媒体类型信息,常见的媒体格式类型如下: text/html : HTML格式 text/plain :纯文本格式 text/xml : XML格式 image/gif :gif图片格式 image/jpeg :jpg图片格式 image/png:png图片格式 application/xhtml+xml:XHTML格式 application/xml: XML数据格式 application/atom+xml:Atom XML聚合格式 application/json: JSON数据格式 application/pdf:pdf格式 application/msword: Word文档格式 application/octet-stream: 二进制流数据(如常见的文件下载) application/x-www-form-urlencoded: 中默认的encType,form表单数据被编码为 key/value格式发送到服务器(表单默认的提交数据的格式) (3)msg.add_header(_name,_value,**_params): 添加邮件头字段 msg.as_string(): 将 msg 变为 str msg.attach(MIMEText 对象或 MIMEImage 对象): 将 MIMEText 对象或 MIMEImage 对象添加到 MIMEMultipart 对象中 想知道更多,请关注我的公众号~ |
CopyRight 2018-2019 实验室设备网 版权所有 |