Printing UTF-8 mail to terminal

Cameron Simpson cs at cskk.id.au
Thu Oct 31 16:50:56 EDT 2024


On 31Oct2024 16:33, Loris Bennett <loris.bennett at fu-berlin.de> wrote:
>I have a command-line program which creates an email containing German
>umlauts.  On receiving the mail, my mail client displays the subject and
>body correctly:
[...]
>So far, so good.  However, when I use the --verbose option to print
>the mail to the terminal via
>
>  if args.verbose:
>      print(mail)
>
>I get:
>
>  Subject: Übungsbetreff
>
>  Sehr geehrter Herr Dr. Bennett,
>
>  Dies ist eine =C3=9Cbung.
>
>What do I need to do to prevent the body from getting mangled?

That looks to me like quoted-printable. This is an encoding for binary 
transport of text to make it robust against not 8-buit clean transports.  
So your Unicode text is encodings as UTF-8, and then that is encoded in 
quoted-printable for transport through the email system.

Your terminal probably accepts UTF-8 - I imagine other German text 
renders corectly?

You need to get the text and undo the quoted-printable encoding.

If you're using the Python email module to parse (or construct) the 
message as a `Message` object I'd expect that to happen automatically.

If you're just dealing with this directly, use the `quopri` stdlib 
module: https://docs.python.org/3/library/quopri.html

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list