MIMEMultipart, MIMEText, MIMEBase, and payloads for sending email with file attachment in Python

E-mail messages

An e-mail message consists of headers (e.g. “From”, “To”, “Subject” etc.) and body (see RFC 822, section 3.1).

The body of the message is, by default, treated as plain ASCII text. MIME (RFC 2045, RFC 2046, RFC 2047, RFC 2048, RFC 2049) defines extensions which allow specifying different types of email content.

One very useful thing you are able to with MIME is specify a Content-Type (e.g. text/html or application/octet-stream).

Another useful thing is that you can create a message with multiple parts (for instance, if you want to have both HTML and an image within the HTML). This is done by specifying a multipart Content-Type (RFC 2046, section 5.1).

Multipart messages

If a message has a multipart Content-Type, that means it consists of multiple messages and each of them defines its own Content-Type (which can again be multipart or something else). Multipart messages are in Python represented by MIMEMultipart class.

So, to answer question 3: When MIMEMultipart is used, then yes, it is a tree-like structure, but if only MIMEText is used, then it is not a tree.

Question 4 asks on which class to set the headers (“To”, “From” etc.) – that is done the the Message class, but all MIME classes inherit from Message, so it can be done on any of them, but those headers only make sense on the root part of a multipart message.

In other words, if a message consists of only one MIME part, specify headers on that part. If it consists of mutiple parts, then the root is a MIMEMultipart – specify the headers on that part.

Question 2 asks “when to use MIMEMultipart, MIMEText and MIMEBase”.

  • MIMEBase is just a base class. As the specification says: “Ordinarily you won’t create instances specifically of MIMEBase
  • MIMEText is for text (e.g. text/plain or text/html), if the whole message is in text format, or if a part of it is.
  • MIMEMultipart is for saying “I have more than one part”, and then listing the parts – you do that if you have attachments, you also do it to provide alternative versions of the same content (e.g. a plain text version plus an HTML version)

Question 5 “What exactly is a “payload”?” – that is just a fancy word for the content of the message (or message part)

Question 6 There is a limitation to using only 7 bits in SMTP. See this answer for more details.

I did not completely understand Question 1, but it seems that the chart is more or less correct. BTW, I would not use MIMEBase here, because there is MIMEApplication which seems more appropriate for the intended purpose.

Leave a Comment

casinojojobetpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casinocanlı bahis casino siteleriOnwin Güncel Girişcasinoholiganbet girişholiganbet güncel giriş