Forwarding messages with the email package

Gerhard Häring gh_pythonlist at gmx.de
Mon Feb 4 05:17:15 CET 2002


I'm having some problems with the new email package in Python 2.2. I
guess the reason is that I don't really understand MIME and I fear my
head will explode when I try to make sense of the source code of the
email package.

What I'm trying to do is forward an email message with MIME. I. e. not
inline. I'm trying to duplicate what my MUA does. It creates a
multipart/mixed MIME doc with a text/plain part and a message/rfc822
part, that contains the email to forward. So, my current code is:


#!/usr/bin/env python2.2
import sys, email
from email.Message import Message
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMessage import MIMEMessage

forwarded_mail = sys.stdin.read()

me = "gerhard at bigfoot.de"
to = ["ghmail at gargamel.hqd-internal"]
msg = MIMEBase('multipart', 'mixed')
msg['Subject'] = 'Re: (no subject)'
msg['From'] = me
msg['To'] = ", ".join(to)

msg.attach(MIMEText("I send you the following in order to have your advice."))

# Hmm. Do I really need to do this in this two steps?
raw = Message()
raw.set_payload(forwarded_mail)
mail = MIMEMessage(raw)

msg.attach(mail)
print msg


This doesn't seem to work quite right when the message I'm trying to
forward is itself mime/multipart. Does I *really* need to parse the
forwarded message into some MIME object and then attach it to the
"container" message?

Has anybody code to share that does this already (forward an arbitrary
message with the email module)?

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 9.0 °C      Wind: 3.3 m/s




More information about the Python-list mailing list