aspineux / pyzmail

Pyzmail is a high level mail library for Python, providing functions to read, compose and send emails
60 stars 31 forks source link

easy way to do content replacement #5

Open thestick613 opened 9 years ago

thestick613 commented 9 years ago

Assuming i'd be using pyzmail as an email processor which replaces all url's with a shortener (for tracking), is the following approach correct?

for part in msg.mailparts:
    payload = part.get_payload() (this also decodes it and trasnforms it into unicode)
    if payload.type in ["text/plain", "text/html"]:
        # process payload in some way, replace links, etc..
        payload = payload.encode(part.charset)
        cte = part.part.get("Content-Transfer-Encoding") or "7bit"
        part.part.set_payload(encode payload according to cte, quopri, base64, etc..)

If this is correct, do you support a patch for a set_payload function?

aspineux commented 9 years ago

Hello Generating and parsing emails was already a big challenge. Modifying an existing email is a level harder. Pyzmail was very well tested and reliable when I released it some years ago. I don't have time to imagine and write tests for a set_payload() function to keep the high quality. This is something I can't support. My first concern is that I don't know if python email parser and generator are "invariant". In other word if

email.message_from_string(any_raw_message).as_string()==any_raw_message

or at least consistent

It looks like you have a good understanding of the problem and are already close of the solution. You can look at generate.py about how I use set_payload() to generate payload.

Hope this help Alain