HomePython Python Email Libraries: SMTP and Email Parsing
Python Email Libraries: SMTP and Email Parsing
The previous two articles in this series discussed connecting to the two major types of email servers and downloading messages. The task yet to be discussed is the actual sending of a message and how to represent an email message logically. In the first part of this article, we will discuss how to create the local data structure to represent an email message. The second part deals with actually connecting to an SMTP server and sending the message.
Python’s main library for dealing with email messages is called the email library, which contains many different objects. A good portion of these objects are concerned with creating and manipulating MIME objects. MIME stands for Multipurpose Internet Mail Extensions and is the standard for formatting email messages, especially email messages that carry attachments. A full discussion of MIME is quite extensive and beyond the scope of this article; however, some familiarity with the standard is useful for understanding how MIME messages are composed.
A MIME message is composed of many “parts,” each of which holds a separate piece of data. This data can be any number of different types, for example, it could be text, an image, a media file, or any number of other things. Most messages nowadays are classified overall as “multipart” which means they contain several different parts, each containing a different type of data. MIME defines several header fields to help with the task of classifying and separating the “parts” of an email message.
Particularly, the Python libraries for email wrap much of the details surrounding the creation of MIME messages. This includes things like how to encode specific types of data so other email clients know how to decode the data, and creating message part boundary descriptions, which are often annoying low level tasks that the typical Python programmer doesn’t want to deal with directly.