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

Without much knowledge of MIME, I tried to learn how to write a Python script to send email with an attached file. After cross-referencing Python documentation, questions, and a general Internet search, I installed the following code [1] and tested it for work.

import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEBase import MIMEBase from email import encoders fromaddr = "YOUR EMAIL" toaddr = "EMAIL ADDRESS YOU SEND TO" msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "SUBJECT OF THE EMAIL" body = "TEXT YOU WANT TO SEND" msg.attach(MIMEText(body, 'plain')) filename = "NAME OF THE FILE WITH ITS EXTENSION" attachment = open("PATH OF THE FILE", "rb") part = MIMEBase('application', 'octet-stream') part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= %s" % filename) msg.attach(part) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(fromaddr, "YOUR PASSWORD") text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit() 
  • I have a general idea of ​​how this script works and developed the following workflow. Please let me know how accurate my flowchart is (?).

      as.string() | +------------MIMEMultipart | |---content-type | +---header---+---content disposition +----.attach()-----+----MIMEBase----| | +---payload (to be encoded in Base64) +----MIMEText 
  • How to know when to use MIMEMultipart, MIMEText and MIMEBase? This seems like a tricky question, so maybe just suggest me some general rules?

  • I read that MIME has a tree structure [2] does this mean that MIMEMultipart is always at the top?
  • In the first block of code, MIMEMultipart encodes ['From'], ['To'] and ['Subject'], but in the Python documentation, MIMEText can also be used to encode ['From'], ['To'] and ['Subject' ]. How can I decide which one to use?
  • What is a payload? Is this some kind of content to transport? If so, what content does this include (I noticed that the body and attachment are considered payloads)? I thought it would be a simple question, but I just could not find a satisfactory, reliable and simple answer.
  • Is it true that although MIME can attach file formats much easier than some texts, in the end all encoding data, header and useful information are turned into strings so that they can be passed to .sendmail ()?

[1] http://naelshiab.com/tutorial-send-email-python/
[2] http://blog.magiksys.net/generate-and-send-mail-with-python-tutorial

+10
source share
1 answer

Emails

An e-mail message consists of headers (for example, From, To, Subject, etc.) and the main body (see RFC 822, section 3.1 ).

By default, the body of the message is treated as plain ASCII text. MIME ( RFC 2045 , RFC 2046 , RFC 2047 , RFC 2048 , RFC 2049 ) defines extensions that allow you to specify various types of email content.

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

Another useful thing is that you can create a message from several parts (for example, if you want the HTML and the image to be inside the HTML). This is done by specifying multipart content ( RFC 2046, section 5.1 ).

Compound Messages

If the message has a multipart type of content, then this means that it consists of several messages, and each of them defines its own type of content (which again can be composite or something else). Compound messages in Python are represented by the MIMEMultipart class.

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

Question 4 asks which class to set the headers for (To, From, etc.) - this is done for the Message class, but all MIME classes MIME inherited from Message , so this can be done for any of them, except for those which headers make sense only at the root of the compound message.

In other words, if the message consists of only one MIME part, specify the headers for this part. If it consists of several parts, then the root is MIMEMultipart - specify the headers for this part.

Question 2 asks "when to use MIMEMultipart, MIMEText and MIMEBase." - MIMEBase is just a base class. As stated in the specification : "Usually you will not create instances specifically for MIMEBase " - MIMEText intended for text (for example, text/plain or text/html ) if the entire message is in text format or part of it. - MIMEMultipart in order to say "I have more than one part" and then list the parts - you do this if you have attachments, you also do this to provide alternative versions of the same content (for example, text version plus version HTML)

Question 5 "What is a payload?" is just a buzzword for the content of the message (or part of the message)

Question 6 There is a restriction on the use of only 7 bits in SMTP. See this answer for more details.

I did not quite understand Question 1 , but it seems that the schedule is more or less correct. By the way, I would not use MIMEBase here, because there is MIMEApplication which seems more suitable for the intended purpose.

+2
source

All Articles