Theemail package is a library for managing email messages, includingMIME and otherRFC 2822-based message documents. It is specificallynotdesigned to do any sending of email messages to SMTP (RFC 2821), NNTP, orother servers; those are functions of modules such assmtplib andnntplib. Theemail package attempts to be as RFC-compliant aspossible, supporting in addition toRFC 2822, such MIME-related RFCs asRFC 2045,RFC 2046,RFC 2047, andRFC 2231.
The primary distinguishing feature of theemail package is that it splitsthe parsing and generating of email messages from the internalobject modelrepresentation of email. Applications using theemail package dealprimarily with objects; you can add sub-objects to messages, remove sub-objectsfrom messages, completely re-arrange the contents, etc. There is a separateparser and a separate generator which handles the transformation from flat textto the object model, and then back to flat text again. There are also handysubclasses for some common MIME object types, and a few miscellaneous utilitiesthat help with such common tasks as extracting and parsing message field values,creating RFC-compliant dates, etc.
The following sections describe the functionality of theemail package.The ordering follows a progression that should be common in applications: anemail message is read as flat text from a file or other source, the text isparsed to produce the object structure of the email message, this structure ismanipulated, and finally, the object tree is rendered back into flat text.
It is perfectly feasible to create the object structure out of whole cloth —i.e. completely from scratch. From there, a similar progression can be taken asabove.
Also included are detailed specifications of all the classes and modules thattheemail package provides, the exception classes you might encounterwhile using theemail package, some auxiliary utilities, and a fewexamples. For users of the oldermimelib package, or previous versionsof theemail package, a section on differences and porting is provided.
Contents of theemail package documentation:
This table describes the release history of the email package, corresponding tothe version of Python that the package was released with. For purposes of thisdocument, when you see a note about change or added versions, these refer to thePython version the change was made in,not the email package version. Thistable also describes the Python compatibility of each version of the package.
| email version | distributed with | compatible with |
|---|---|---|
| 1.x | Python 2.2.0 to Python 2.2.1 | no longer supported |
| 2.5 | Python 2.2.2+ and Python 2.3 | Python 2.1 to 2.5 |
| 3.0 | Python 2.4 | Python 2.3 to 2.5 |
| 4.0 | Python 2.5 | Python 2.3 to 2.5 |
| 5.0 | Python 3.0 and Python 3.1 | Python 3.0 to 3.2 |
| 5.1 | Python 3.2 | Python 3.0 to 3.2 |
Here are the major differences betweenemail version 5.1 andversion 5.0:
Here are the major differences betweenemail version 5.0 and version 4:
Here are the major differences betweenemail version 4 and version 3:
All modules have been renamed according toPEP 8 standards. For example,the version 3 moduleemail.Message was renamed toemail.message inversion 4.
A new subpackageemail.mime was added and all the version 3email.MIME* modules were renamed and situated into theemail.mimesubpackage. For example, the version 3 moduleemail.MIMEText was renamedtoemail.mime.text.
Note that the version 3 names will continue to work until Python 2.6.
Theemail.mime.application module was added, which contains theMIMEApplication class.
Methods that were deprecated in version 3 have been removed. These includeGenerator.__call__(),Message.get_type(),Message.get_main_type(),Message.get_subtype().
Fixes have been added forRFC 2231 support which can change some of thereturn types forMessage.get_paramand friends. Under somecircumstances, values which used to return a 3-tuple now return simple strings(specifically, if all extended parameter segments were unencoded, there is nolanguage and charset designation expected, so the return type is now a simplestring). Also, %-decoding used to be done for both encoded and unencodedsegments; this decoding is now done only for encoded segments.
Here are the major differences betweenemail version 3 and version 2:
Here are the differences betweenemail version 2 and version 1:
Theemail.Header andemail.Charset modules have been added.
The pickle format forMessage instances has changed.Since this was never (and still isn’t) formally defined, this isn’tconsidered a backward incompatibility. However if your application picklesand unpicklesMessage instances, be aware that inemail version 2,Message instances now haveprivate variables_charset and_default_type.
Several methods in theMessage class have beendeprecated, or their signatures changed. Also, many new methods have beenadded. See the documentation for theMessage classfor details. The changes should be completely backward compatible.
The object structure has changed in the face ofmessage/rfc822content types. Inemail version 1, such a type would be representedby a scalar payload, i.e. the container message’sis_multipart() returned false,get_payload() was not a list object, but asingleMessage instance.
This structure was inconsistent with the rest of the package, so the objectrepresentation formessage/rfc822 content types was changed. Inemail version 2, the containerdoes returnTrue fromis_multipart(), andget_payload() returns a list containing a singleMessage item.
Note that this is one place that backward compatibility could not becompletely maintained. However, if you’re already testing the return type ofget_payload(), you should be fine. You just needto make sure your code doesn’t do aset_payload()with aMessage instance on a container with a contenttype ofmessage/rfc822.
TheParser constructor’sstrict argument was added,and itsparse() andparsestr() methods grew aheadersonly argument.Thestrict flag was also added to functionsemail.message_from_file()andemail.message_from_string().
Generator.__call__() is deprecated; useGenerator.flatten instead. TheGenerator class has also grown theclone() method.
TheDecodedGenerator class in theemail.generator module was added.
The intermediate base classesMIMENonMultipart andMIMEMultipart have been added, and interposedin the class hierarchy for most of the other MIME-related derived classes.
The_encoder argument to theMIMEText constructorhas been deprecated. Encoding now happens implicitly based on the_charset argument.
The following functions in theemail.Utils module have been deprecated:dump_address_pairs(),decode(), andencode(). The followingfunctions have been added to the module:make_msgid(),decode_rfc2231(),encode_rfc2231(), anddecode_params().
The non-public functionemail.Iterators._structure() was added.
Theemail package was originally prototyped as a separate library calledmimelib. Changes have been made so that method namesare more consistent, and some methods or modules have either been added orremoved. The semantics of some of the methods have also changed. For the mostpart, any functionality available inmimelib is still available in theemail package, albeit often in a different way. Backward compatibilitybetween themimelib package and theemail package was not apriority.
Here is a brief description of the differences between themimelib andtheemail packages, along with hints on how to port your applications.
Of course, the most visible difference between the two packages is that thepackage name has been changed toemail. In addition, the top-levelpackage has the following differences:
TheMessage class has the following differences:
TheParser class has no differences in its publicinterface. It does have some additional smarts to recognizemessage/delivery-status type messages, which it represents as aMessage instance containing separateMessage subparts for each header block in the deliverystatus notification[1].
TheGenerator class has no differences in its publicinterface. There is a new class in theemail.generator module though,calledDecodedGenerator which provides most of thefunctionality previously available in theMessage.getpayloadastext()method.
The following modules and classes have been changed:
TheMIMEBase class constructor arguments_majorand_minor have changed to_maintype and_subtype respectively.
TheImage class/module has been renamed toMIMEImage. The_minorargument has been renamed to_subtype.
TheText class/module has been renamed toMIMEText. The_minorargument has been renamed to_subtype.
TheMessageRFC822 class/module has been renamed toMIMEMessage. Notethat an earlier version ofmimelib called this class/moduleRFC822,but that clashed with the Python standard library modulerfc822 on somecase-insensitive file systems.
Also, theMIMEMessage class now represents anykind of MIME messagewith main typemessage. It takes an optional argument_subtypewhich is used to set the MIME subtype._subtype defaults torfc822.
mimelib provided some utility functions in itsaddress anddate modules. All of these functions have been moved to theemail.utils module.
TheMsgReader class/module has been removed. Its functionality is mostclosely supported in thebody_line_iterator() functionin theemail.iterators module.
Footnotes
| [1] | Delivery Status Notifications (DSN) are defined inRFC 1894. |
19.1.1.email.message: Representing an email message
Enter search terms or a module, class or function name.