Using Python 3.1.2 I am having a problem sending binary attachment fi开发者_运维知识库les (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))
solution from 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())
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
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)
精彩评论