二进制文件的电子邮件附件的问题 [英] Binary file email attachment problem
问题描述
使用Python 3.1.2,我有发送二进制附件文件(JPEG,PDF等)的问题 - MimeText用于附件的正常工作。有问题的code是如下...
在self.attachments文件:
部分= MIMEBase('应用',八位字节流)
part.set_payload(打开(文件,RB)。阅读())
EN coders.en code_base64(部分)
part.add_header(内容处置,附件;文件名=%S'%文件)
msg.attach(部分)#msg是MimeMultipart的实例()服务器= smtplib.SMTP(主机,端口)
server.login(用户名,密码)
server.sendmail(from_addr,all_recipients,msg.as_string())
然而,在调用堆栈的方式向下(见下面回溯),它看起来好像msg.as_string()已收到它创建的字节型的,而不是串有效负载的附件。
有没有人任何想法可能会导致什么问题?任何帮助将是AP preciated。
阿伦
builtins.TypeError:字符串有效载荷预期:其中,类的字节'>
文件C:\\开发\\ CommonPY \\脚本\\ email_send.py,线路147,在发送
server.sendmail(self.from_addr,all_recipients,msg.as_string())
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ message.py,线路136,在as_string
g.flatten(个体经营,unixfrom = unixfrom)
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ generator.py,76行,在压平
self._write(MSG)
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ generator.py,线路101,在_write
self._dispatch(MSG)
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ generator.py,第127行,在_Dispatch
甲基(MSG)
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ generator.py,181线,在_handle_multipart
g.flatten(部分unixfrom = FALSE)
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ generator.py,76行,在压平
self._write(MSG)
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ generator.py,线路101,在_write
self._dispatch(MSG)
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ generator.py,第127行,在_Dispatch
甲基(MSG)
文件C:\\ Program Files文件\\ Python31 \\ LIB \\电子邮件\\ generator.py,155线,在_handle_text
提高类型错误('字符串有效载荷预期:%s'的类型%(负载))
确定 - 太多的无奈和网络搜索之后,我发现这个问题是一个已知的bug适用于Python的3.x中,EN codeRS的.py,功能连接code_base64,应读作如下...
高清连接code_base64(MSG):
恩code为Base64消息的有效载荷。 此外,添加适当的Content-Transfer-Encoding头。
原稿= msg.get_payload()
encdata = _ben code(原稿) 插入,保证所有字节字符#换行转换为ASCII
encdata = STR(encdata,ASCII) msg.set_payload(encdata)
味精['内容传输编码'] ='的base64
该错误已被提出的问题#4768,并升级到临界状态
在2010-05-10。希望这将是固定在下一版本(3.1.3?)
问候,阿伦
Using Python 3.1.2 I am having a problem sending binary attachment files (jpeg, pdf, etc.) - MIMEText attachments work fine. The code in question is as follows...
for file in self.attachments:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(file,"rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
msg.attach(part) # msg is an instance of MIMEMultipart()
server = smtplib.SMTP(host, port)
server.login(username, password)
server.sendmail(from_addr, all_recipients, msg.as_string())
However, way down in the calling-stack (see traceback below), it looks as though msg.as_string() has received an attachment which creates a payload of 'bytes' type instead of string.
Has anyone any idea what might be causing the problem? Any help would be appreciated.
Alan
builtins.TypeError: string payload expected: <class 'bytes'>
File "c:\Dev\CommonPY\Scripts\email_send.py", line 147, in send
server.sendmail(self.from_addr, all_recipients, msg.as_string())
File "c:\Program Files\Python31\Lib\email\message.py", line 136, in as_string
g.flatten(self, unixfrom=unixfrom)
File "c:\Program Files\Python31\Lib\email\generator.py", line 76, in flatten
self._write(msg)
File "c:\Program Files\Python31\Lib\email\generator.py", line 101, in _write
self._dispatch(msg)
File "c:\Program Files\Python31\Lib\email\generator.py", line 127, in _dispatch
meth(msg)
File "c:\Program Files\Python31\Lib\email\generator.py", line 181, in _handle_multipart
g.flatten(part, unixfrom=False)
File "c:\Program Files\Python31\Lib\email\generator.py", line 76, in flatten
self._write(msg)
File "c:\Program Files\Python31\Lib\email\generator.py", line 101, in _write
self._dispatch(msg)
File "c:\Program Files\Python31\Lib\email\generator.py", line 127, in _dispatch
meth(msg)
File "c:\Program Files\Python31\Lib\email\generator.py", line 155, in _handle_text
raise TypeError('string payload expected: %s' % type(payload))
Ok - after much frustration and web-searching, I have found that the problem is a known bug that applies to Python 3.x, encoders.py, function encode_base64, which should read as follows...
def encode_base64(msg):
"""Encode the message's payload in Base64.
Also, add an appropriate Content-Transfer-Encoding header.
"""
orig = msg.get_payload()
encdata = _bencode(orig)
# new line inserted to ensure all bytes characters are converted to ASCII
encdata = str(encdata, "ASCII")
msg.set_payload(encdata)
msg['Content-Transfer-Encoding'] = 'base64'
The bug has been raised as issue #4768, and was escalated to critical status on 2010-05-10. Hopefully it will be fixed in the next version (3.1.3?)
Regards, Alan
这篇关于二进制文件的电子邮件附件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!