Decoding of EmailMessage text

Loris Bennett loris.bennett at fu-berlin.de
Mon Aug 23 06:00:00 EDT 2021


"Loris Bennett" <loris.bennett at fu-berlin.de> writes:

> Hi,
>
> I have written a command-line program to send email using
>
>   from email.message import EmailMessage
>
> which has an option '--verbose' which prints the email via
>
>   if args.verbose:
>       print(f"{mail.as_string()}")       
>
> If I run this with
>
>   $ poetry run send_email loris -l en -s "Another test" -t ~/tmp/blorp_bleep.txt --verbose
>
> the following is printed to the terminal:
>
>   Content-Type: text/plain; charset="utf-8"
>   Content-Transfer-Encoding: base64
>   MIME-Version: 1.0
>   From: hpc at zedat.fu-berlin.de
>   To: loris at zedat.fu-berlin.de
>   Subject: Another test
>
>   RGVhciBEci4gQmVubmV0dCwKCkJsb3JwISBCbGVlcCEKCgotLQpNYWlsYm90IEluYy4KMDEwMTAx                                           │
>   IEJvdCBCb3VsZXZhcmQKQm90aGFtIENpdHkKQsO2dGxhbmQK
>
> However I would like to see the text as
>
>   Dear Dr. Bennett,
>
>   Blorp! Bleep!
>   
>   Regards,
>
>   Mailbot
>
>   --
>   Mailbot Inc.
>   010101 Bot Boulevard
>   Botham City
>   Bötland
>   
> which is how the text appears in the mail.
>
> The 'ö' in 'Bötland' triggers the encoding - with 'Botland' the text
> printed to the terminal is the same way as that in the mail.
>
> How do I decode the message properly for an terminal which uss UTF-8?

If I do 

From: "Loris Bennett" <loris.bennett at fu-berlin.de>
Newsgroups: comp.lang.python
Subject: Re: Decoding of EmailMessage text
Gcc: "nnimap+fu_exchange:Gesendete Elemente"
--text follows this line--
"Loris Bennett" <loris.bennett at fu-berlin.de> writes:

> Hi,
>
> I have written a command-line program to send email using
>
>   from email.message import EmailMessage
>
> which has an option '--verbose' which prints the email via
>
>   if args.verbose:
>       print(f"{mail.as_string()}")       
>
> If I run this with
>
>   $ poetry run send_email loris -l en -s "Another test" -t ~/tmp/blorp_bleep.txt --verbose
>
> the following is printed to the terminal:
>
>   Content-Type: text/plain; charset="utf-8"
>   Content-Transfer-Encoding: base64
>   MIME-Version: 1.0
>   From: hpc at zedat.fu-berlin.de
>   To: loris at zedat.fu-berlin.de
>   Subject: Another test
>
>   RGVhciBEci4gQmVubmV0dCwKCkJsb3JwISBCbGVlcCEKCgotLQpNYWlsYm90IEluYy4KMDEwMTAx                                           │
>   IEJvdCBCb3VsZXZhcmQKQm90aGFtIENpdHkKQsO2dGxhbmQK
>
> However I would like to see the text as
>
>   Dear Dr. Bennett,
>
>   Blorp! Bleep!
>   
>   Regards,
>
>   Mailbot
>
>   --
>   Mailbot Inc.
>   010101 Bot Boulevard
>   Botham City
>   Bötland
>   
> which is how the text appears in the mail.
>
> The 'ö' in 'Bötland' triggers the encoding - with 'Botland' the text
> printed to the terminal is the same way as that in the mail.
>
> How do I decode the message properly for an terminal which uss UTF-8?

If I do 

From: "Loris Bennett" <loris.bennett at fu-berlin.de>
Newsgroups: comp.lang.python
Subject: Re: Decoding of EmailMessage text
Gcc: "nnimap+fu_exchange:Gesendete Elemente"
--text follows this line--
"Loris Bennett" <loris.bennett at fu-berlin.de> writes:

> Hi,
>
> I have written a command-line program to send email using
>
>   from email.message import EmailMessage
>
> which has an option '--verbose' which prints the email via
>
>   if args.verbose:
>       print(f"{mail.as_string()}")       
>
> If I run this with
>
>   $ poetry run send_email loris -l en -s "Another test" -t ~/tmp/blorp_bleep.txt --verbose
>
> the following is printed to the terminal:
>
>   Content-Type: text/plain; charset="utf-8"
>   Content-Transfer-Encoding: base64
>   MIME-Version: 1.0
>   From: hpc at zedat.fu-berlin.de
>   To: loris at zedat.fu-berlin.de
>   Subject: Another test
>
>   RGVhciBEci4gQmVubmV0dCwKCkJsb3JwISBCbGVlcCEKCgotLQpNYWlsYm90IEluYy4KMDEwMTAx                                           │
>   IEJvdCBCb3VsZXZhcmQKQm90aGFtIENpdHkKQsO2dGxhbmQK
>
> However I would like to see the text as
>
>   Dear Dr. Bennett,
>
>   Blorp! Bleep!
>   
>   Regards,
>
>   Mailbot
>
>   --
>   Mailbot Inc.
>   010101 Bot Boulevard
>   Botham City
>   Bötland
>   
> which is how the text appears in the mail.
>
> The 'ö' in 'Bötland' triggers the encoding - with 'Botland' the text
> printed to the terminal is the same way as that in the mail.
>
> How do I decode the message properly for an terminal which uss UTF-8?

If instead of 

  mail.set_content(body)

I do 

  mail.set_content(body, cte="quoted-printable")

then I get 

  Dear Dr. Bennett,

  Blorp! Bleep!


  --
  Mailbot Inc.
  010101 Bot Boulevard
  Botham City
  B=C3=B6tland

That a bit better, but still not what I want since the main text could
also be in German and thus also contain umlauts, etc.

Cheers,

Loris

-- 
This signature is currently under construction.


More information about the Python-list mailing list