How to modify the content of an email

Gabriel Genellina gagsl-py2 at
Sun Jan 27 18:44:48 CET 2008

En Fri, 25 Jan 2008 20:59:41 -0200, <alejandro.valdez at> escribi�:

> 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).

Gabriel Genellina

More information about the Python-list mailing list