2010-05-09 125 views
6

使用Python 3.1.2我有一个问题发送二进制附件文件(JPEG,PDF等) - MIMEText附件工作正常。有问题的代码如下...二进制文件电子邮件附件问题

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()) 

然而,在调用堆栈一路下滑(见下文回溯),它看起来好像msg.as_string()已收到其产生的有效载荷附件'字节'类型而不是字符串。

有没有人有任何想法可能会导致这个问题?任何帮助,将不胜感激。

艾伦


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)) 
+0

通过一个解决办法的方式解决(当然,可能的解决方法,我实际上不知道它是否会有帮助),你可以尝试使用'MIMEApplication'而不是'MIMEBase' – 2010-05-09 18:00:22

+0

大卫感谢您的回复。我尝试了MIMEApplication,但无济于事(即.msg.get_payload()仍然返回字节而不是字符串)。我怀疑这是编码不正确地将二进制文件转换为字符串,但我可能是错的。我也担心我已经尝试了很多来自不同网站的类似例子 - 它们都失败了,但是一定为作者工作过。 Regards – 2010-05-09 21:38:31

回答

3

好了 - 太多的无奈和网络搜索之后,我发现这个问题是一个已知的bug适用于Python的3.x中,encoders.py,功能encode_base64,这应改为...

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' 

的错误已被提出的问题#4768,并升级到临界状态 上2010-05-10。希望这将是固定在下一版本(3.1.3?)

问候,艾伦

+0

有没有可以使用的解决方法? – 2011-01-31 14:04:11

3
for file in self.attachments: 
    fp = open(file,"rb")  
    part = MIMEApplication(fp.read())  
    fp.close()  
    encoders.encode_base64(part) 

    # the miracle 
    part.set_payload(part.get_payload().decode('ASCII')) 

    part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)  
    msg.attach(part) 
+0

看起来很有希望 - 我会试一试 - 谢谢。 – 2010-06-11 02:37:41

+0

添加“从电子邮件进口编码器” – mastier 2016-10-20 14:56:40

2

this SO answer

from base64 import encodebytes 
for file in self.attachments: 
    fp = open(file, 'rb') 
    part = MIMEBase('application', "octet-stream") 
    part.set_payload(encodebytes(fp.read()).decode()) 
    fp.close() 
    part.add_header('Content-Transfer-Encoding', 'base64') 
    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()) 
+0

@bstpierre,感谢您的解决方案。我将把它存储起来,以防我目前的解决方案在未来的Python版本中出现。 – 2011-03-16 22:24:47