Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51411 > unrolled thread

email 8bit encoding

Started byrurpy@yahoo.com
First post2013-07-28 16:41 -0700
Last post2013-08-02 14:12 +0200
Articles 7 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  email 8bit encoding rurpy@yahoo.com - 2013-07-28 16:41 -0700
    Re: [python] email 8bit encoding "W. Trevor King" <wking@tremily.us> - 2013-07-28 23:16 -0700
      Re: [python] email 8bit encoding rurpy@yahoo.com - 2013-07-29 20:56 -0700
    Re: email 8bit encoding Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-07-29 22:52 +0200
      Re: email 8bit encoding rurpy@yahoo.com - 2013-08-01 08:20 -0700
        Re: email 8bit encoding Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-08-02 13:19 +0200
        Re: email 8bit encoding Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-08-02 14:12 +0200

#51411 — email 8bit encoding

Fromrurpy@yahoo.com
Date2013-07-28 16:41 -0700
Subjectemail 8bit encoding
Message-ID<36aa5535-a821-4d7e-8414-1e40820705e4@googlegroups.com>
How, using Python-3.3's email module, do I "flatten" (I think
that's the right term) a Message object to get utf-8 encoded
body with the headers:
 Content-Type: text/plain; charset=UTF-8
 Content-Transfer-Encoding: 8bit
when the message payload was set to a python (unicode) string?

[toc] | [next] | [standalone]


#51474 — Re: [python] email 8bit encoding

From"W. Trevor King" <wking@tremily.us>
Date2013-07-28 23:16 -0700
SubjectRe: [python] email 8bit encoding
Message-ID<mailman.5252.1375124803.3114.python-list@python.org>
In reply to#51411

[Multipart message — attachments visible in raw view] — view raw

On Sun, Jul 28, 2013 at 04:41:27PM -0700, rurpy@yahoo.com wrote:
> How, using Python-3.3's email module, do I "flatten" (I think
> that's the right term) a Message object to get utf-8 encoded
> body with the headers:
>  Content-Type: text/plain; charset=UTF-8
>  Content-Transfer-Encoding: 8bit
> when the message payload was set to a python (unicode) string?

I asked about this a while back [1], but never got a response.  My
current best-guess is here [2].  My fallback flattening works for
everything except the 8-bit encoded messages using the UTF-16 charset,
but it's pretty ugly.

Let me know if you figure out something cleaner :).

Cheers,
Trevor

[1]: http://thread.gmane.org/gmane.comp.python.general/725425
[2]: https://github.com/wking/rss2email/blob/master/rss2email/email.py#L226

-- 
This email may be signed or encrypted with GnuPG (http://www.gnupg.org).
For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy

[toc] | [prev] | [next] | [standalone]


#51541 — Re: [python] email 8bit encoding

Fromrurpy@yahoo.com
Date2013-07-29 20:56 -0700
SubjectRe: [python] email 8bit encoding
Message-ID<eaea5140-8873-44d8-988f-a64413dca2eb@googlegroups.com>
In reply to#51474
On 07/29/2013 12:16 AM, W. Trevor King wrote:
> On Sun, Jul 28, 2013 at 04:41:27PM -0700, rurpy@yahoo.com wrote:
>> How, using Python-3.3's email module, do I "flatten" (I think
>> that's the right term) a Message object to get utf-8 encoded
>> body with the headers:
>>  Content-Type: text/plain; charset=UTF-8
>>  Content-Transfer-Encoding: 8bit
>> when the message payload was set to a python (unicode) string?
> 
> I asked about this a while back [1], but never got a response.  My
> current best-guess is here [2].  My fallback flattening works for
> everything except the 8-bit encoded messages using the UTF-16 charset,
> but it's pretty ugly.
> 
> Let me know if you figure out something cleaner :).
> 
> Cheers,
> Trevor
> 
> [1]: http://thread.gmane.org/gmane.comp.python.general/725425
> [2]: https://github.com/wking/rss2email/blob/master/rss2email/email.py#L226

Thanks, that was very helpful.

My code is just a quick utility tool for internal use so
I am able to be fairly hackish without shame. :-)

Since I am synthesizing the mails from scratch and the 
encoding requirements fairly simple, I could probably
dispense with the email/smtplib packages altogether and 
just pipe my text directly to sendmail.  But I thought
using Python's email package would be easier.  Silly me.

Thanks again.

[toc] | [prev] | [next] | [standalone]


#51642

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-07-29 22:52 +0200
Message-ID<mailman.12.1375272464.1251.python-list@python.org>
In reply to#51411
Op 29-07-13 01:41, rurpy@yahoo.com schreef:
> How, using Python-3.3's email module, do I "flatten" (I think
> that's the right term) a Message object to get utf-8 encoded
> body with the headers:
>   Content-Type: text/plain; charset=UTF-8
>   Content-Transfer-Encoding: 8bit
> when the message payload was set to a python (unicode) string?
>

I am just trying out some things for my self on python 3.2 so
be sure to test this out but you could try the following.

msg.set_charset('utf-8')
msg['Content-Transfer-Encoding'] = '8bit'

-- 
Antoon Pardon

[toc] | [prev] | [next] | [standalone]


#51739

Fromrurpy@yahoo.com
Date2013-08-01 08:20 -0700
Message-ID<1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com>
In reply to#51642
On 07/29/2013 02:52 PM, Antoon Pardon wrote:
> Op 29-07-13 01:41, rurpy@yahoo.com schreef:
>> How, using Python-3.3's email module, do I "flatten" (I think
>> that's the right term) a Message object to get utf-8 encoded
>> body with the headers:
>>   Content-Type: text/plain; charset=UTF-8
>>   Content-Transfer-Encoding: 8bit
>> when the message payload was set to a python (unicode) string?
>>
> 
> I am just trying out some things for my self on python 3.2 so
> be sure to test this out but you could try the following.
> 
> msg.set_charset('utf-8')
> msg['Content-Transfer-Encoding'] = '8bit'

You can do that but the problem occurs when you call 
email.generator.flatten (or it is called on your behalf by 
somthing like smtplib.send_message) with such a message.  
flatten always assumes a 7bit encoding and uses the ascii 
codec to encode the message resulting in a UnicodeEncode 
exception when it hits an 8 bit character.  So gymnastics 
like W. Trevor King implemented are necessary.

[toc] | [prev] | [next] | [standalone]


#51795

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-08-02 13:19 +0200
Message-ID<mailman.113.1375442359.1251.python-list@python.org>
In reply to#51739
Op 01-08-13 17:20, rurpy@yahoo.com schreef:
> On 07/29/2013 02:52 PM, Antoon Pardon wrote:
>> Op 29-07-13 01:41, rurpy@yahoo.com schreef:
>>> How, using Python-3.3's email module, do I "flatten" (I think
>>> that's the right term) a Message object to get utf-8 encoded
>>> body with the headers:
>>>   Content-Type: text/plain; charset=UTF-8
>>>   Content-Transfer-Encoding: 8bit
>>> when the message payload was set to a python (unicode) string?
>>>
>>
>> I am just trying out some things for my self on python 3.2 so
>> be sure to test this out but you could try the following.
>>
>> msg.set_charset('utf-8')
>> msg['Content-Transfer-Encoding'] = '8bit'
> 
> You can do that but the problem occurs when you call 
> email.generator.flatten (or it is called on your behalf by 
> somthing like smtplib.send_message) with such a message.  
> flatten always assumes a 7bit encoding and uses the ascii 
> codec to encode the message resulting in a UnicodeEncode 
> exception when it hits an 8 bit character.  So gymnastics 
> like W. Trevor King implemented are necessary.

Well this works for me. I had a little look in the code
and it seems buggy to me, at least the 3.2 version is.
There is a _encode but (1) The bytes generator version
is defined to allways use us-ascii as encoding and (2)
I couldn't find it actually being called on a mesg part.

------------------------------------------------------------

import smtplib

from email.mime.text import MIMEText

txt = '''\
Het adres is

Fréderic Boëven
Frère Orbanstraat 17
'''

recipient = "..."
sender = "..."

msg = MIMEText(txt)
msg.set_charset("utf-8")

msg['Subject'] = 'Python email tets'
msg['From'] = sender
msg['To'] = recipient

s = smtplib.SMTP('localhost')
s.sendmail(sender, [recipient], msg.as_string().encode("utf-8"))
s.quit()

[toc] | [prev] | [next] | [standalone]


#51798

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-08-02 14:12 +0200
Message-ID<mailman.118.1375445526.1251.python-list@python.org>
In reply to#51739
Op 01-08-13 17:20, rurpy@yahoo.com schreef:
> On 07/29/2013 02:52 PM, Antoon Pardon wrote:
>> Op 29-07-13 01:41, rurpy@yahoo.com schreef:
>>> How, using Python-3.3's email module, do I "flatten" (I think
>>> that's the right term) a Message object to get utf-8 encoded
>>> body with the headers:
>>>   Content-Type: text/plain; charset=UTF-8
>>>   Content-Transfer-Encoding: 8bit
>>> when the message payload was set to a python (unicode) string?
>>>
>>
>> I am just trying out some things for my self on python 3.2 so
>> be sure to test this out but you could try the following.
>>
>> msg.set_charset('utf-8')
>> msg['Content-Transfer-Encoding'] = '8bit'
> 
> You can do that but the problem occurs when you call 
> email.generator.flatten (or it is called on your behalf by 
> somthing like smtplib.send_message) with such a message.  
> flatten always assumes a 7bit encoding and uses the ascii 
> codec to encode the message resulting in a UnicodeEncode 
> exception when it hits an 8 bit character.  So gymnastics 
> like W. Trevor King implemented are necessary.

This works too, but I don't know how usefull it is with
multipart messages.

import smtplib
import os
import io

from email.mime.text import MIMEText
from email.generator import BytesGenerator

from email.generator import BytesGenerator

class EncodeGenerator(BytesGenerator):

    def flatten(self, msg, unixfrom=False, linesep='\n'):
        self._encoding = str(msg.get_charset())
        super().flatten(msg, unixfrom, linesep)

    def write(self, s):
        self._fp.write(s.encode(self._encoding))

    def _encode(self, s):
        return s.encode(self._encoding)


txt = '''\
Het adres is

Fréderic Boëven
Frère Orbanstraat 17
'''

recipient = " ... "
sender = " ... "

msg = MIMEText(txt)
msg.set_charset("utf-8")

msg['Subject'] = 'Python email test: %d' % os.getpid()
msg['From'] = sender
msg['To'] = recipient

print(msg['Subject'])

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')

with io.BytesIO() as bytesmsg:
    g = EncodeGenerator(bytesmsg)
    g.flatten(msg, linesep='\r\n')
    flat_msg = bytesmsg.getvalue()

print(flat_msg)
s.sendmail(sender, [recipient], flat_msg)
s.quit()

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web