二进制文件电子邮件附件问题

时间:2010-05-09 17:17:24

标签: python email binary smtp attachment

使用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()已经收到一个附件,它创建了一个'bytes'类型的有效载荷而不是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))

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

好的 - 经过多次挫折和网络搜索之后,我发现问题是一个已知错误,适用于Python 3.x,encoders.py,function 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?)

中修复

问候,艾伦

答案 2 :(得分: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)