Problems with email.Generator.Generator
Chris Withers
chris at simplistix.co.uk
Mon Sep 11 17:39:14 EDT 2006
Chris Withers wrote:
> ...except it gets the transfer encoding wrong, which means Thunderbird
> shows =A3 instead of the pound sign that it should :-(
>
> ...this is down to a pretty lame bit of code in Encoders.py which
> basically checks for a unicode error *sigh*
OK, slight progress... here a new version that actually works:
from email.Charset import Charset,QP
from email.MIMEText import MIMEText
charset = Charset('utf-8')
charset.body_encoding = QP
msg = MIMEText('','plain',None)
msg.set_payload(u'Some text with chars that need encoding:\xa3',charset)
print msg.as_string()
MIME-Version: 1.0
Content-Type: text/plain; charset; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Some text with chars that need encoding:=A3
Okay, so this actually does the right thing... wahey!
...but hold your horses, if Charset isn't set to quoted printable, then
you end up with problems:
charset = Charset('utf-8')
msg = MIMEText('','plain',None)
msg.set_payload(u'Some text with chars that need encoding:\xa3',charset)
Traceback (most recent call last):
File "C:\test_encoding.py", line 5, in ?
msg.set_payload(u'Some text with chars that need
encoding:\xa3',charset)
File "c:\python24\lib\email\Message.py", line 218, in set_payload
self.set_charset(charset)
File "c:\python24\lib\email\Message.py", line 260, in set_charset
self._payload = charset.body_encode(self._payload)
File "c:\python24\lib\email\Charset.py", line 366, in body_encode
return email.base64MIME.body_encode(s)
File "c:\python24\lib\email\base64MIME.py", line 136, in encode
enc = b2a_base64(s[i:i + max_unencoded])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in
position 40: ordinal not in range(128)
Now what?
*sigh*
Chris
--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk
More information about the Python-list
mailing list