Re: Creating message parts

Oleg Broytmann <[email protected]>
Newsgroups gmane.comp.python.mime.devel
Message-ID <[email protected]>
On Wed, Sep 24, 2008 at 03:37:53PM +0100, Nicholas Cole wrote:
> E = email.mime.multipart.MIMEMultipart()
> print E.as_string()
> 
> Then E is given a "MIME-Version: 1.0" header, which I don't think it
> should have (the "parent" email message will have that header, of
> course).
> 
> I have a feeling, therefore, that I am doing something wrong!  Should
> I not be using the MIMEMultipart() calss for this purpose? And if not,
> what should I be using?

   MIMEMultipart is a class for the top-level part (the entire message).
For subparts use classes like MIMEText et al. Compare these 3 examples:

----- 1 -----
from email import MIMEText
msg = MIMEText.MIMEText("This is a simple message", _charset="latin-1")

msg["Subject"] = "Text"

print str(msg)
----- /1 -----

----- 2 -----
from email import MIMENonMultipart
msg = MIMENonMultipart.MIMENonMultipart("text", "plain", charset="latin-1")

msg["Subject"] = "Simple"
msg.set_payload("This is a simple message")

print str(msg)
----- /2 -----

----- 3 -----
from email import MIMEMultipart, MIMEText
msg = MIMEMultipart.MIMEMultipart()
msg["Subject"] = "Multipart"

part = MIMEText.MIMEText("This is a subpart", _charset="latin-1")
part["Subject"] = "Subpart"

msg.attach(part)
print str(msg)
----- /3 -----

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            [email protected]
           Programmers don't die, they just GOSUB without RETURN.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.