/Doc/library/email.mime.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 198 lines · 137 code · 61 blank · 0 comment · 0 complexity · c4e9685338e591e2cdf0570619803525 MD5 · raw file

  1. :mod:`email`: Creating email and MIME objects from scratch
  2. ----------------------------------------------------------
  3. .. module:: email.mime
  4. :synopsis: Build MIME messages.
  5. Ordinarily, you get a message object structure by passing a file or some text to
  6. a parser, which parses the text and returns the root message object. However
  7. you can also build a complete message structure from scratch, or even individual
  8. :class:`~email.message.Message` objects by hand. In fact, you can also take an
  9. existing structure and add new :class:`~email.message.Message` objects, move them
  10. around, etc. This makes a very convenient interface for slicing-and-dicing MIME
  11. messages.
  12. You can create a new object structure by creating :class:`~email.message.Message`
  13. instances, adding attachments and all the appropriate headers manually. For MIME
  14. messages though, the :mod:`email` package provides some convenient subclasses to
  15. make things easier.
  16. Here are the classes:
  17. .. currentmodule:: email.mime.base
  18. .. class:: MIMEBase(_maintype, _subtype, **_params)
  19. Module: :mod:`email.mime.base`
  20. This is the base class for all the MIME-specific subclasses of
  21. :class:`~email.message.Message`. Ordinarily you won't create instances
  22. specifically of :class:`MIMEBase`, although you could. :class:`MIMEBase`
  23. is provided primarily as a convenient base class for more specific
  24. MIME-aware subclasses.
  25. *_maintype* is the :mailheader:`Content-Type` major type (e.g. :mimetype:`text`
  26. or :mimetype:`image`), and *_subtype* is the :mailheader:`Content-Type` minor
  27. type (e.g. :mimetype:`plain` or :mimetype:`gif`). *_params* is a parameter
  28. key/value dictionary and is passed directly to :meth:`Message.add_header`.
  29. The :class:`MIMEBase` class always adds a :mailheader:`Content-Type` header
  30. (based on *_maintype*, *_subtype*, and *_params*), and a
  31. :mailheader:`MIME-Version` header (always set to ``1.0``).
  32. .. currentmodule:: email.mime.nonmultipart
  33. .. class:: MIMENonMultipart()
  34. Module: :mod:`email.mime.nonmultipart`
  35. A subclass of :class:`~email.mime.base.MIMEBase`, this is an intermediate base
  36. class for MIME messages that are not :mimetype:`multipart`. The primary
  37. purpose of this class is to prevent the use of the :meth:`attach` method,
  38. which only makes sense for :mimetype:`multipart` messages. If :meth:`attach`
  39. is called, a :exc:`~email.errors.MultipartConversionError` exception is raised.
  40. .. versionadded:: 2.2.2
  41. .. currentmodule:: email.mime.multipart
  42. .. class:: MIMEMultipart([_subtype[, boundary[, _subparts[, _params]]]])
  43. Module: :mod:`email.mime.multipart`
  44. A subclass of :class:`~email.mime.base.MIMEBase`, this is an intermediate base
  45. class for MIME messages that are :mimetype:`multipart`. Optional *_subtype*
  46. defaults to :mimetype:`mixed`, but can be used to specify the subtype of the
  47. message. A :mailheader:`Content-Type` header of :mimetype:`multipart/_subtype`
  48. will be added to the message object. A :mailheader:`MIME-Version` header will
  49. also be added.
  50. Optional *boundary* is the multipart boundary string. When ``None`` (the
  51. default), the boundary is calculated when needed.
  52. *_subparts* is a sequence of initial subparts for the payload. It must be
  53. possible to convert this sequence to a list. You can always attach new subparts
  54. to the message by using the :meth:`Message.attach` method.
  55. Additional parameters for the :mailheader:`Content-Type` header are taken from
  56. the keyword arguments, or passed into the *_params* argument, which is a keyword
  57. dictionary.
  58. .. versionadded:: 2.2.2
  59. .. currentmodule:: email.mime.application
  60. .. class:: MIMEApplication(_data[, _subtype[, _encoder[, **_params]]])
  61. Module: :mod:`email.mime.application`
  62. A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
  63. :class:`MIMEApplication` class is used to represent MIME message objects of
  64. major type :mimetype:`application`. *_data* is a string containing the raw
  65. byte data. Optional *_subtype* specifies the MIME subtype and defaults to
  66. :mimetype:`octet-stream`.
  67. Optional *_encoder* is a callable (i.e. function) which will perform the actual
  68. encoding of the data for transport. This callable takes one argument, which is
  69. the :class:`MIMEApplication` instance. It should use :meth:`get_payload` and
  70. :meth:`set_payload` to change the payload to encoded form. It should also add
  71. any :mailheader:`Content-Transfer-Encoding` or other headers to the message
  72. object as necessary. The default encoding is base64. See the
  73. :mod:`email.encoders` module for a list of the built-in encoders.
  74. *_params* are passed straight through to the base class constructor.
  75. .. versionadded:: 2.5
  76. .. currentmodule:: email.mime.audio
  77. .. class:: MIMEAudio(_audiodata[, _subtype[, _encoder[, **_params]]])
  78. Module: :mod:`email.mime.audio`
  79. A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
  80. :class:`MIMEAudio` class is used to create MIME message objects of major type
  81. :mimetype:`audio`. *_audiodata* is a string containing the raw audio data. If
  82. this data can be decoded by the standard Python module :mod:`sndhdr`, then the
  83. subtype will be automatically included in the :mailheader:`Content-Type` header.
  84. Otherwise you can explicitly specify the audio subtype via the *_subtype*
  85. parameter. If the minor type could not be guessed and *_subtype* was not given,
  86. then :exc:`TypeError` is raised.
  87. Optional *_encoder* is a callable (i.e. function) which will perform the actual
  88. encoding of the audio data for transport. This callable takes one argument,
  89. which is the :class:`MIMEAudio` instance. It should use :meth:`get_payload` and
  90. :meth:`set_payload` to change the payload to encoded form. It should also add
  91. any :mailheader:`Content-Transfer-Encoding` or other headers to the message
  92. object as necessary. The default encoding is base64. See the
  93. :mod:`email.encoders` module for a list of the built-in encoders.
  94. *_params* are passed straight through to the base class constructor.
  95. .. currentmodule:: email.mime.image
  96. .. class:: MIMEImage(_imagedata[, _subtype[, _encoder[, **_params]]])
  97. Module: :mod:`email.mime.image`
  98. A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
  99. :class:`MIMEImage` class is used to create MIME message objects of major type
  100. :mimetype:`image`. *_imagedata* is a string containing the raw image data. If
  101. this data can be decoded by the standard Python module :mod:`imghdr`, then the
  102. subtype will be automatically included in the :mailheader:`Content-Type` header.
  103. Otherwise you can explicitly specify the image subtype via the *_subtype*
  104. parameter. If the minor type could not be guessed and *_subtype* was not given,
  105. then :exc:`TypeError` is raised.
  106. Optional *_encoder* is a callable (i.e. function) which will perform the actual
  107. encoding of the image data for transport. This callable takes one argument,
  108. which is the :class:`MIMEImage` instance. It should use :meth:`get_payload` and
  109. :meth:`set_payload` to change the payload to encoded form. It should also add
  110. any :mailheader:`Content-Transfer-Encoding` or other headers to the message
  111. object as necessary. The default encoding is base64. See the
  112. :mod:`email.encoders` module for a list of the built-in encoders.
  113. *_params* are passed straight through to the :class:`~email.mime.base.MIMEBase`
  114. constructor.
  115. .. currentmodule:: email.mime.message
  116. .. class:: MIMEMessage(_msg[, _subtype])
  117. Module: :mod:`email.mime.message`
  118. A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
  119. :class:`MIMEMessage` class is used to create MIME objects of main type
  120. :mimetype:`message`. *_msg* is used as the payload, and must be an instance
  121. of class :class:`~email.message.Message` (or a subclass thereof), otherwise
  122. a :exc:`TypeError` is raised.
  123. Optional *_subtype* sets the subtype of the message; it defaults to
  124. :mimetype:`rfc822`.
  125. .. currentmodule:: email.mime.text
  126. .. class:: MIMEText(_text[, _subtype[, _charset]])
  127. Module: :mod:`email.mime.text`
  128. A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
  129. :class:`MIMEText` class is used to create MIME objects of major type
  130. :mimetype:`text`. *_text* is the string for the payload. *_subtype* is the
  131. minor type and defaults to :mimetype:`plain`. *_charset* is the character
  132. set of the text and is passed as a parameter to the
  133. :class:`~email.mime.nonmultipart.MIMENonMultipart` constructor; it defaults
  134. to ``us-ascii``. No guessing or encoding is performed on the text data.
  135. .. versionchanged:: 2.4
  136. The previously deprecated *_encoding* argument has been removed. Encoding
  137. happens implicitly based on the *_charset* argument.