Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a email body part through MIMEMultipart

Tags:

python

email

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?

like image 699
Satya Avatar asked Nov 18 '15 09:11

Satya


People also ask

What is Mimetext?

Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the format of email messages to support text in character sets other than ASCII, as well as attachments of audio, video, images, and application programs.

What is email MIME multipart?

Multipurpose Internet Mail Extensions (MIME) is an Internet standard that is used to support the transfer of single or multiple text and non-text attachments. Non-text attachments can include graphics, audio, and video files.

How do I use MIMEMultipart in Python?

MIMEMultipart is used when we have attachments or want to provide alternative versions of the same content (e.g. a plain text/HTML version). We have a simple text file. The example sends an email with a text file attachment to Mailtrap. We read the contents of the text file.


2 Answers

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)
like image 184
tripleee Avatar answered Nov 02 '22 11:11

tripleee


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.

like image 41
Nobilis Avatar answered Nov 02 '22 10:11

Nobilis