使用MIMEBase将CSV附加到电子邮件,仅附加"bin"文档 [英] Attach CSV to E-Mail with MIMEBase, only attaches 'bin' document

查看:47
本文介绍了使用MIMEBase将CSV附加到电子邮件,仅附加"bin"文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,将csv文件附加到带有HTML文本的电子邮件中.一切正常.我发送带有文本的电子邮件和带有正确信息的附件.仅数据格式错误.我在 MIMEBASE('application','octet-stream')中尝试了不同的品种.这给了我'.bin'数据,我无法在macOS上打开它.其他人给了我纯文本数据,其中包括正确的数据,但是我不想手动将其复制到csv中.有人解决我如何以'.csv'的形式获取数据吗?我在这里找到的大多数解决方案就像下面的代码一样.

代码:

  def send_mail(收件人,主题,文件路径,附加名称):#登录信息端口= 123smtp_server ="test.server.com";login ="testlogin"#您的登录名密码="12345678910"# 你的密码#指定发件人和收件人的电子邮件地址和HTML文本发件人="testmail@test.com";接收者=接收者消息= MIMEMultipart()消息[主题"] =主题message [发件人"] =发件人message ["To"] = COMMASPACE.join([receiver])html_text =""" \< html><身体>< p> Hi,< br>< p>种问候</p>< p>这是一个自动的Mail</p>.</body></html>"#将两个部分都转换为MIMEText对象,并将它们添加到MIMEMultipart消息中文字= MIMEText(html_text," html")message.attach(文本)#添加文件作为附件文件=打开(文件路径,"rb")att = MIMEBase('application','octet-stream')att.set_payload(file.read())encoders.encode_base64(att)att.add_header("Content_Disposition",'附件',文件名=附件名)message.attach(att)尝试:#使用上面指定的凭据发送邮件使用smtplib.SMTP(smtp_server,port)作为服务器:server.connect(smtp_server,端口)server.ehlo()server.starttls()server.ehlo()server.login(登录名,密码)server.sendmail(发件人,收件人,message.as_string())#告诉脚本报告您的消息是否已发送或需要修复哪些错误打印('已发送')除了(gaierror,ConnectionRefusedError):打印(无法连接到服务器.连接设置错误?")除了smtplib.SMTPServerDisconnected:打印(无法连接到服务器.错误的用户名/密码?")除了smtplib.SMTPException作为e:打印(发生SMTP错误:'+ str(e))send_mail('testmail@test.com','TEST','/Users/Changer/Desktop/test.csv','test.csv') 

解决方案

电子邮件客户端可以使用附件部分的内容类型来确定用于打开附件的程序,因此指定合适的内容类型可能会有所帮助.>

此代码使用标准库的mimetypes模块根据其名称来尝试猜测附件的正确内容类型:

 导入模仿类型mimetypes.init()def send_mail(收件人,主题,文件路径,附加名称):...mimetype,_ = mimetypes.guess_type(附件名称)如果mimetype为None:mimetype ='应用程序/八位字节流'type_,_,subtype = mimetype.partition('/')att = MIMEBase(type_,子类型)... 

上面的代码将为附件生成以下标头:

  b'Content-Type:text/x-逗号分隔值'b'MIME版本:1.0'b'Content-Transfer-Encoding:base64'b'Content_Disposition:附件;filename ="test.csv" 

mac是否会通过电子表格应用程序打开csv(我想这就是您想要的)取决于机器的配置(请参见 解决方案

Email clients may use the content type of the attachment part to determine which program is used to open the attachment, so specifying a suitable content type may help.

This code uses the standard library's mimetypes module to try to guess the correct content type of the attachment, based on its name:

import mimetypes

mimetypes.init()

def send_mail(receiver, subject, filepath, attachname):
    ...

    mimetype, _ = mimetypes.guess_type(attachname)
    if mimetype is None:
        mimetype = 'application/octet-stream'
    type_, _, subtype = mimetype.partition('/')
    att = MIMEBase(type_, subtype)
    ...

The above code will generate these headers for the attachment:

b'Content-Type: text/x-comma-separated-values'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: base64'
b'Content_Disposition: attachment; filename="test.csv"'

Whether a mac will open a csv with a spreadsheet application (I assume this is what you want) depends on the configuration of the machine (See this blog for an example). If you want to be sure open the file in a spreadsheet it might be more effective to convert it to a spreadsheet format and send the spreadsheet.

这篇关于使用MIMEBase将CSV附加到电子邮件,仅附加"bin"文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆