New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
email.generator.Generator object bytes/str crash - b64encode() bug? #49018
Comments
The email.generator.Generator class does not work correctly message >>> from email.mime.image import MIMEImage
>>> data = open("IMG.jpg","rb").read()
>>> m = MIMEImage(data,'jpeg')
>>> s = m.as_string()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/lib/python3.0/email/message.py", line 136, in as_string
g.flatten(self, unixfrom=unixfrom)
File "/tmp/lib/python3.0/email/generator.py", line 76, in flatten
self._write(msg)
File "/tmp/lib/python3.0/email/generator.py", line 101, in _write
self._dispatch(msg)
File "/tmp/lib/python3.0/email/generator.py", line 127, in _dispatch
meth(msg)
File "/tmp/lib/python3.0/email/generator.py", line 155, in
_handle_text
raise TypeError('string payload expected: %s' % type(payload))
TypeError: string payload expected: <class 'bytes'>
>>> The source of the problem is rather complicated, but here is the gist of
Suggested fix: I think the library function base64.b64encode() should return a string, Other fixes: Modify the Generator class in email.generator to properly detect bytes |
Yes, in the email module, the payload is an unicode string, not a Attached patch implements this fix: encode_base64() returns str (and |
Hello, This patch has never been commited. I tested today with the 3.1 branch (and checked in the lib code). Is there a better way to attach images in an email ? Thanks in advance for your help, Regards, |
Hi, I think the bug is also present in the function encode_quopri which should look like this : def encode_quopri(msg):
"""Encode the message's payload in quoted-printable.
|
I wrote a patch for base64.b64encode() to accept str (str is encoded to utf-8): patch attached to bpo-4768. It should fix this issue, but we can add the tests of email_base64_bytes.patch. |
Attaching patch from reported duplicate bpo-8896. |
Note that my patch is roughly the same as the original posted by haypo. |
Yes, but yours was better formatted, so I used it :) Thanks for the patch. Applied in r81685 to py3k, and r81686. |
@garazi111: if you have an example where quopri fails, please open a new issue for it. I suspect you are right that there is a problem there. |
For the record, encode_quopri was fixed in bpo-14360. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: