19.1.email — An email and MIME handling package¶
Source code:Lib/email/__init__.py
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:
- 19.1.1.
email.message: Representing an email message - 19.1.2.
email.parser: Parsing email messages - 19.1.3.
email.generator: Generating MIME documents - 19.1.4.
email.policy: Policy Objects - 19.1.5.
email.headerregistry: Custom Header Objects - 19.1.6.
email.contentmanager: Managing MIME Content - 19.1.7.
email.mime: Creating email and MIME objects from scratch - 19.1.8.
email.header: Internationalized headers - 19.1.9.
email.charset: Representing character sets - 19.1.10.
email.encoders: Encoders - 19.1.11.
email.errors: Exception and Defect classes - 19.1.12.
email.utils: Miscellaneous utilities - 19.1.13.
email.iterators: Iterators - 19.1.14.
email: Examples
19.1.15. Package History¶
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 and Python 2.5 | Python 2.3 to 2.6 |
4.0 | Python 2.5 to Python 2.7 | Python 2.3 to 2.7 |
5.0 | Python 3.0 and Python 3.1 | Python 3.0 to 3.2 |
5.1 | Python 3.2 | Python 3.2 |
After Version 5.1 (Python 3.2), the email package no longer has a version thatis separate from the Python version. (See theWhat’s New in Python documentsfor the respective Python versions for details on changes.)
Here are the major differences betweenemail version 5.1 andversion 5.0:
- It is once again possible to parse messages containing non-ASCII bytes,and to reproduce such messages if the data containing the non-ASCIIbytes is not modified.
- New functions
message_from_bytes()andmessage_from_binary_file(),and new classesBytesFeedParserandBytesParserallow binary message data to be parsedinto model objects. - Given bytes input to the model,
get_payload()will by default decode a message body that has aContent-Transfer-Encoding of8bitusing the charsetspecified in the MIME headers and return the resulting string. - Given bytes input to the model,
Generatorwillconvert message bodies that have aContent-Transfer-Encoding of8bit to instead have a 7bit Content-Transfer-Encoding. - New class
BytesGeneratorproduces bytesas output, preserving any unchanged non-ASCII data that waspresent in the input used to build the model, including message bodieswith aContent-Transfer-Encoding of 8bit.
Here are the major differences betweenemail version 5.0 and version 4:
- All operations are on unicode strings. Text inputs must be strings,text outputs are strings. Outputs are limited to the ASCII characterset and so can be encoded to ASCII for transmission. Inputs are alsolimited to ASCII; this is an acknowledged limitation of email 5.0 andmeans it can only be used to parse email that is 7bit clean.
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 module
email.Messagewas renamed toemail.messageinversion 4.A new subpackage
email.mimewas added and all the version 3email.MIME*modules were renamed and situated into theemail.mimesubpackage. For example, the version 3 moduleemail.MIMETextwas renamedtoemail.mime.text.Note that the version 3 names will continue to work until Python 2.6.
The
email.mime.applicationmodule was added, which contains theMIMEApplicationclass.Methods that were deprecated in version 3 have been removed. These include
Generator.__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 for
Message.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:
- The
FeedParserclass was introduced, and theParserclass was implemented in terms of theFeedParser. All parsing therefore isnon-strict, and parsing will make a best effort never to raise an exception.Problems found while parsing messages are stored in the message’sdefectattribute. - All aspects of the API which raised
DeprecationWarnings in version 2have been removed. These include the_encoder argument to theMIMETextconstructor, theMessage.add_payload()method, theUtils.dump_address_pair()function, and the functionsUtils.decode()andUtils.encode(). - New
DeprecationWarnings have been added to:Generator.__call__(),Message.get_type(),Message.get_main_type(),Message.get_subtype(), and thestrictargument to theParserclass. These are expected tobe removed in future versions. - Support for Pythons earlier than 2.3 has been removed.
Here are the differences betweenemail version 2 and version 1:
The
email.Headerandemail.Charsetmodules have been added.The pickle format for
Messageinstances has changed.Since this was never (and still isn’t) formally defined, this isn’tconsidered a backward incompatibility. However if your application picklesand unpicklesMessageinstances, be aware that inemailversion 2,Messageinstances now haveprivate variables_charset and_default_type.Several methods in the
Messageclass have beendeprecated, or their signatures changed. Also, many new methods have beenadded. See the documentation for theMessageclassfor details. The changes should be completely backward compatible.The object structure has changed in the face ofmessage/rfc822content types. In
emailversion 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 asingleMessageinstance.This structure was inconsistent with the rest of the package, so the objectrepresentation formessage/rfc822 content types was changed. In
emailversion 2, the containerdoes returnTruefromis_multipart(), andget_payload()returns a list containing a singleMessageitem.Note that this is one place that backward compatibility could not becompletely maintained. However, if you’re already testing the return type of
get_payload(), you should be fine. You just needto make sure your code doesn’t do aset_payload()with aMessageinstance on a container with a contenttype ofmessage/rfc822.The
Parserconstructor’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.flatteninstead. TheGeneratorclass has also grown theclone()method.The
DecodedGeneratorclass in theemail.generatormodule was added.The intermediate base classes
MIMENonMultipartandMIMEMultiparthave been added, and interposedin the class hierarchy for most of the other MIME-related derived classes.The_encoder argument to the
MIMETextconstructorhas been deprecated. Encoding now happens implicitly based on the_charset argument.The following functions in the
email.Utilsmodule 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 function
email.Iterators._structure()was added.
19.1.16. Differences frommimelib¶
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:
messageFromString()has been renamed tomessage_from_string().messageFromFile()has been renamed tomessage_from_file().
TheMessage class has the following differences:
- The method
asString()was renamed toas_string(). - The method
ismultipart()was renamed tois_multipart(). - The
get_payload()method has grown adecodeoptional argument. - The method
getall()was renamed toget_all(). - The method
addheader()was renamed toadd_header(). - The method
gettype()was renamed toget_type(). - The method
getmaintype()was renamed toget_main_type(). - The method
getsubtype()was renamed toget_subtype(). - The method
getparams()was renamed toget_params(). Also, whereasgetparams()returned a list of strings,get_params()returnsa list of 2-tuples, effectively the key/value pairs of the parameters, spliton the'='sign. - The method
getparam()was renamed toget_param(). - The method
getcharsets()was renamed toget_charsets(). - The method
getfilename()was renamed toget_filename(). - The method
getboundary()was renamed toget_boundary(). - The method
setboundary()was renamed toset_boundary(). - The method
getdecodedpayload()was removed. To get similarfunctionality, pass the value 1 to thedecode flag of theget_payload()method. - The method
getpayloadastext()was removed. Similar functionality issupported by theDecodedGeneratorclass in theemail.generatormodule. - The method
getbodyastext()was removed. You can get similarfunctionality by creating an iterator withtyped_subpart_iterator()in theemail.iteratorsmodule.
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:
The
MIMEBaseclass constructor arguments_majorand_minor have changed to_maintype and_subtype respectively.The
Imageclass/module has been renamed toMIMEImage. The_minorargument has been renamed to_subtype.The
Textclass/module has been renamed toMIMEText. The_minorargument has been renamed to_subtype.The
MessageRFC822class/module has been renamed toMIMEMessage. Notethat an earlier version ofmimelibcalled this class/moduleRFC822,but that clashed with the Python standard library modulerfc822on somecase-insensitive file systems.Also, the
MIMEMessageclass 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. |
