使用 Python 3.1.2 我在发送二进制附件文件(jpeg、pdf 等)时遇到问题 - MIMEText 附件工作正常.有问题的代码如下...
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())
但是,在调用堆栈的下方(请参阅下面的回溯),似乎 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.
艾伦
builtins.TypeError: string payload expected: <class 'bytes'>
File "c:DevCommonPYScriptsemail_send.py", line 147, in send
server.sendmail(self.from_addr, all_recipients, msg.as_string())
File "c:Program FilesPython31Libemailmessage.py", line 136, in as_string
g.flatten(self, unixfrom=unixfrom)
File "c:Program FilesPython31Libemailgenerator.py", line 76, in flatten
self._write(msg)
File "c:Program FilesPython31Libemailgenerator.py", line 101, in _write
self._dispatch(msg)
File "c:Program FilesPython31Libemailgenerator.py", line 127, in _dispatch
meth(msg)
File "c:Program FilesPython31Libemailgenerator.py", line 181, in _handle_multipart
g.flatten(part, unixfrom=False)
File "c:Program FilesPython31Libemailgenerator.py", line 76, in flatten
self._write(msg)
File "c:Program FilesPython31Libemailgenerator.py", line 101, in _write
self._dispatch(msg)
File "c:Program FilesPython31Libemailgenerator.py", line 127, in _dispatch
meth(msg)
File "c:Program FilesPython31Libemailgenerator.py", line 155, in _handle_text
raise TypeError('string payload expected: %s' % type(payload))
好的 - 经过很多挫折和网络搜索,我发现问题是适用于 Python 3.x、encoders.py、函数的已知错误encode_base64,应该是这样的……
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'
该错误已作为问题 #4768 提出,并已升级为严重状态在 2010 年 5 月 10 日.希望它会在下一个版本(3.1.3?)中得到修复
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?)
问候,艾伦
这篇关于二进制文件电子邮件附件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)