python 发送带各种附件的邮件示例!

python 发送带各种附件的邮件示例!,第1张

概述简述下如何使用python发送各种附件的邮件,比如word、excel、pdf、txt,以及在正文插入图片等等

简述下如何使用python发送各种附件的邮件,比如word、excel、pdf、txt,以及在正文插入图片等等

如下所示,

加群:960410445  即可获取数十套pdf!

# Coding=utf-8import smtplibfrom email.mime.text import MIMETextfrom email.header import headerfrom smtplib import SMTP_SSLfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.mime.application import MIMEApplicationfrom email.mime.text import MIMETextfrom email.mime.base import MIMEBasefrom email.encoders import encode_base64import osimport tracebackdef 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 = '%s
' % (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')
总结

以上是内存溢出为你收集整理的python 发送带各种附件的邮件示例!全部内容,希望文章能够帮你解决python 发送带各种附件的邮件示例!所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1208368.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存