Satya
Satya

Reputation: 5907

how to send a email body part through MIMEMultipart

I am just curious to know if MIMEMultipart has any attribute in it where I can pass my email body part ... Only part I have come up with is

msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "hi find the attached file"
msg.preamble = "please PFA"

Is there anything like

 msg["Body"] = I will add a string or a text file

I googled for it and found

body = MIMEMultipart('alternative')
body.attach(MIMEText(text))

but it's not working in my case.

And one more thing - how does MIMEMultipart('alternative') work? What functionality does it provide?

Upvotes: 17

Views: 32863

Answers (3)

tripleee
tripleee

Reputation: 189487

The purpose of a multipart container is to contain other MIME parts. If you only have one part, by definition it's not multipart.

multipart/alternative is useful when you have the same content in different renderings. A common case is to have the message body in both text/plain (no fonts, colors, images, or other "rich" content) and text/html. Typically, the user will have configured their client to prefer one over the other, and so it will then display whatever the user prefers. Somewhat less commonly, the user has a client which can display one type and not the other, so then it's a matter of technical necessity, not user preference, to display the supported version.

multipart/related is useful when you have multiple parts constituting a message. For example, the text/html part in the multipart/alternative might want to pull in images which are supplied as "related" parts. So a common structure in fact is

multipart/alternative
    +---- text/plain
    +---- multipart/related
              +---- text/html
              +---- image/png
              +---- image/png

or even another multipart/related above that if there is an attachment which is independent of the multipart/alternative renderings.

For your concrete example, just declare the body part as text/plain instead:

msg = MIMEText(text)
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "hi find the attached file"

For what it's worth, you should normally not need to mess with the MIME preamble, or imagine that a client will display it. (There will only be a preamble when there are multiple parts.)

If you do have an actual attachment you want to include, then this:

msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "hi find the attached file"
msg.attach(MIMEText(text))
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(data)
msg.attach(attachment)

Upvotes: 18

PascalVKooten
PascalVKooten

Reputation: 21453

I would advise to just use some email package that does not "bother" you with knowing anything about types.

Introducing yagmail (I'm the developer).

It will automatically do what you want (attachments, images, html code, fall back and many other features).

pip install yagmail

then

import yagmail
yag = yagmail.SMTP('myemail', 'mypass')
mixed_contents = ['some text', '/path/to/local/img', '/path/to/mp3', 
                  '<h1>a big header text</h1>']
yag.send('[email protected]', 'subject', mixed_contents)

You will end up with some text, some header, inline image and an attached file.

Upvotes: 3

Nobilis
Nobilis

Reputation: 7448

This works for me:

msg = MIMEMultipart()
msg['From'], msg['To'], msg['Subject'] = ... # specify your sender, receiver, subject attributes
body = 'This is the body of the email.'
body = MIMEText(body) # convert the body to a MIME compatible string
msg.attach(body) # attach it to your main message

You attach the body to the msg, and body in your case should be the MIMEText object.

Upvotes: 21

Related Questions