python 发送带各种附件的邮件示例
简述下如何使用python发送各种附件的邮件,比如word、excel、pdf、txt,以及在正文插入图片等等
# coding=utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header from smtplib import SMTP_SSL from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.mime.text import MIMEText from email.mime.base import MIMEBase from email.encoders import encode_base64 import os import traceback def send_mail(mail_title, mail_content=None, attachment_img=None, attachment_txt=None, attachment_pdf=None, attachment_excel=None, attachment_word=None): # qq邮箱smtp服务器 host_server = 'smtp.qq.com' # sender_qq为发件人的qq号码 sender_qq = '947118251' # pwd为qq邮箱的授权码 pwd = 'tvjl******zpbebb' # 发件人的邮箱 sender_qq_mail = '947118251@qq.com' # 收件人邮箱 # receiver = 'znwindy@gmail.com' receiver = '947118251@qq.com' try: # ssl登录 smtp = SMTP_SSL(host_server) # set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式 smtp.set_debuglevel(1) smtp.ehlo(host_server) smtp.login(sender_qq, pwd) # msg = MIMEText(mail_content, "plain", 'utf-8') msg = MIMEMultipart('related') msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender_qq_mail msg["To"] = receiver msgAlternative = MIMEMultipart('alternative') msg.attach(msgAlternative) # image attach if attachment_img: mail_body = '<b>%s</b><br><img src="cid:%s"><br>' % (mail_content, attachment_img) msgText = (MIMEText(mail_body, 'html', 'utf-8')) msgAlternative.attach(msgText) with open(attachment_img, 'rb') as fp: msgImage = MIMEImage(fp.read()) msgImage.add_header('Content-ID', '<{}>'.format(attachment_img)) msg.attach(msgImage) # txt attach if attachment_txt: file_name = os.path.split(attachment_txt)[1] att1 = MIMEText(open(attachment_txt, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = f'attachment; filename="{file_name}"' msg.attach(att1) # pdf attach if attachment_pdf: with open(attachment_pdf, "rb") as fp: fileMsg = MIMEBase('application', 'pdf') fileMsg.set_payload(fp.read()) encode_base64(fileMsg) fileMsg.add_header('Content-Disposition', f'attachment;filename={os.path.split(attachment_pdf)[1]}') msg.attach(fileMsg) # excel attach if attachment_excel: part = MIMEBase('application', "vnd.ms-excel") with open(attachment_excel, "rb") as fp: part.set_payload(fp.read()) encode_base64(part) part.add_header('Content-Disposition', f'attachment; filename="{os.path.split(attachment_excel)[1]}"') msg.attach(part) # word attach if attachment_word: with open(attachment_word, "rb") as fp: part = MIMEApplication(fp.read()) part.add_header('Content-Disposition', f'attachment; filename="{os.path.split(attachment_word)[1]}"') part.set_charset('utf-8') msg.attach(part) smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) smtp.quit() print('Success!') except: print('Error!') traceback.print_exc() if __name__ == '__main__': send_mail(mail_title='爬虫结束了,正常退出!', mail_content='你好,这是使用python登录qq邮箱发邮件的测试', attachment_img='../data/test.jpg', attachment_txt='../data/start_urls.txt', attachment_pdf='../data/Gmail - How to add images in the product description_.pdf', attachment_excel='../data/shops.xlsx', attachment_word='../data/asdasd.docx')
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人