Printing UTF-8 mail to terminal
Peter J. Holzer
hjp-python at hjp.at
Tue Nov 5 15:39:32 EST 2024
On 2024-11-04 13:02:21 +0100, Loris Bennett via Python-list wrote:
> "Loris Bennett" <loris.bennett at fu-berlin.de> writes:
> > "Loris Bennett" <loris.bennett at fu-berlin.de> writes:
> >> Cameron Simpson <cs at cskk.id.au> writes:
> >>> On 01Nov2024 10:10, Loris Bennett <loris.bennett at fu-berlin.de> wrote:
> >>>>as expected. The non-UTF-8 text occurs when I do
> >>>>
> >>>> mail = EmailMessage()
> >>>> mail.set_content(body, cte="quoted-printable")
> >>>> ...
> >>>>
> >>>> if args.verbose:
> >>>> print(mail)
> >>>>
> >>>>which is presumably also correct.
> >>>>
> >>>>The question is: What conversion is necessary in order to print the
> >>>>EmailMessage object to the terminal, such that the quoted-printable
> >>>>parts are turned (back) into UTF-8?
[...]
> OK, so I can do:
>
> ######################################################################
> if args.verbose:
> for k in mail.keys():
> print(f"{k}: {mail.get(k)}")
> print('')
> print(mail.get_content())
> ######################################################################
>
> prints what I want and is not wildly clunky, but I am a little surprised
> that I can't get a string representation of the whole email in one go.
Mails can contain lots of stuff, so there is in general no suitable
human readable string representation of a whole email. You have to go
through it part by part and decide what you want to do with each. For
example, if you have a multipart/alternative with a text/plain and a
text/html part what should the "string representation" be? For some uses
the text/plain part might be sufficient. For some you might want the
HTML part or some rendering of it. Or what would you do with an image?
Omit it completely? Just use the filename (if any)? Try to convert it to
ASCII-Art? Use an AI to describe it?
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp at hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20241105/2c992772/attachment.sig>
More information about the Python-list
mailing list