How to modify the content of an email

A

alejandro.valdez

Hello, I'm trying to make a python script that take an email in (raw)
text format, and add a footer to the text (or html) body of the email.

I'm aware of the email and email.mime modules, but I can't figure out
how to identify 'the main text (or html) content' from the email, and
how to be sure that I don't incorrectly identify a txt (or html)
attach as the main content of the email.

By 'main text (or html) content' I mean the text (or html) that is
showed by a Mail User Agent when it display the email to the
recipient.

Thanks in advance.
 
G

Gabriel Genellina

Hello, I'm trying to make a python script that take an email in (raw)
text format, and add a footer to the text (or html) body of the email.

I'm aware of the email and email.mime modules, but I can't figure out
how to identify 'the main text (or html) content' from the email, and
how to be sure that I don't incorrectly identify a txt (or html)
attach as the main content of the email.
By 'main text (or html) content' I mean the text (or html) that is
showed by a Mail User Agent when it display the email to the
recipient.

I suggest you read or overview the MIME specification (RFC 2045 and a few
others), or some introductory text, in order to understand the terminology
and what the email package does.
Simple messages have is_multipart()==False and get_payload() gives you the
message text.
Multipart messages (e.g. having attachments, or an html/plaintext
alternative) have is_multipart()==False and get_payload() returns a list
of its parts. The parts may be Messages too, and can be multipart also.
HTML messages usually have Content-Type: multipart/alternative, coming
first the text part and later the HTML part. You probably will have to
modify both, because it's up to the MUA to decide which part to show.
When you modify an existing part you have to *remove* some headers like
Content-Transfer-Encoding if you don't honor them in the replaced part. By
example, the original may have been encoded in base64 or quoted-printable
(but you didn't notice that because Python decoded the part for you).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top