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?
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.
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With