栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Python发送带有.csv附件的电子邮件[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Python发送带有.csv附件的电子邮件[重复]

发送包含适当MIME类型的多部分电子邮件。

https://docs.python.org/2/library/email-
examples.html

所以可能是这样的(我测试过):

import smtplibimport mimetypesfrom email.mime.multipart import MIMEMultipartfrom email import enprersfrom email.message import Messagefrom email.mime.audio import MIMEAudiofrom email.mime.base import MIMEbasefrom email.mime.image import MIMEImagefrom email.mime.text import MIMETextemailfrom = "sender@example.com"emailto = "destination@example.com"fileToSend = "hi.csv"username = "user"password = "password"msg = MIMEMultipart()msg["From"] = emailfrommsg["To"] = emailtomsg["Subject"] = "help I cannot send an attachment to save my life"msg.preamble = "help I cannot send an attachment to save my life"ctype, encoding = mimetypes.guess_type(fileToSend)if ctype is None or encoding is not None:    ctype = "application/octet-stream"maintype, subtype = ctype.split("/", 1)if maintype == "text":    fp = open(fileToSend)    # Note: we should handle calculating the charset    attachment = MIMEText(fp.read(), _subtype=subtype)    fp.close()elif maintype == "image":    fp = open(fileToSend, "rb")    attachment = MIMEImage(fp.read(), _subtype=subtype)    fp.close()elif maintype == "audio":    fp = open(fileToSend, "rb")    attachment = MIMEAudio(fp.read(), _subtype=subtype)    fp.close()else:    fp = open(fileToSend, "rb")    attachment = MIMEbase(maintype, subtype)    attachment.set_payload(fp.read())    fp.close()    enprers.enpre_base64(attachment)attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)msg.attach(attachment)server = smtplib.SMTP("smtp.gmail.com:587")server.starttls()server.login(username,password)server.sendmail(emailfrom, emailto, msg.as_string())server.quit()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/648869.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号