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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23171140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:23:47  来源:igfitidea点击:

How do I send an email with a .csv attachment using Python

pythonemail

提问by Jordan Starrk

Okay, I know there is a few questions out there addressing this, but I cannot find a way to make it work properly. I would assume it is as simple as the below code, but this does not attach my file. Any help would be greatly appreciated. I am also very new to Python. Is there a mail module that I should be importing to make the function work?

好的,我知道有几个问题可以解决这个问题,但我找不到让它正常工作的方法。我认为它和下面的代码一样简单,但这并没有附加我的文件。任何帮助将不胜感激。我对 Python 也很陌生。是否有我应该导入的邮件模块以使该功能正常工作?

import smtplib
fromaddr = "[email protected]
toaddrs = "[email protected]

msg = "help I cannot send an attachment to save my life"
attach = ("csvonDesktp.csv")

username = user
password = password

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg, attach)
server.quit()

采纳答案by Jamie Ivanov

Send a multipart email with the appropriate MIME types.

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

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

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

So possible something like this (I tested this):

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

import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText

emailfrom = "[email protected]"
emailto = "[email protected]"
fileToSend = "hi.csv"
username = "user"
password = "password"

msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["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()
    encoders.encode_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()

回答by s16h

There is a complete example in the Python documentation. I can copy and paste the relevant parts here but the whole page is not very long so it's better if you go and have a look at it there.

Python 文档中有一个完整的示例。我可以在这里复制和粘贴相关部分,但整个页面不是很长,所以最好去那里看看。