multipart.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2002-2006 Python Software Foundation
  2. # Author: Barry Warsaw
  3. # Contact: email-sig@python.org
  4. """Base class for MIME multipart/* type messages."""
  5. from __future__ import unicode_literals
  6. from __future__ import division
  7. from __future__ import absolute_import
  8. __all__ = ['MIMEMultipart']
  9. from future.backports.email.mime.base import MIMEBase
  10. class MIMEMultipart(MIMEBase):
  11. """Base class for MIME multipart/* type messages."""
  12. def __init__(self, _subtype='mixed', boundary=None, _subparts=None,
  13. **_params):
  14. """Creates a multipart/* type message.
  15. By default, creates a multipart/mixed message, with proper
  16. Content-Type and MIME-Version headers.
  17. _subtype is the subtype of the multipart content type, defaulting to
  18. `mixed'.
  19. boundary is the multipart boundary string. By default it is
  20. calculated as needed.
  21. _subparts is a sequence of initial subparts for the payload. It
  22. must be an iterable object, such as a list. You can always
  23. attach new subparts to the message by using the attach() method.
  24. Additional parameters for the Content-Type header are taken from the
  25. keyword arguments (or passed into the _params argument).
  26. """
  27. MIMEBase.__init__(self, 'multipart', _subtype, **_params)
  28. # Initialise _payload to an empty list as the Message superclass's
  29. # implementation of is_multipart assumes that _payload is a list for
  30. # multipart messages.
  31. self._payload = []
  32. if _subparts:
  33. for p in _subparts:
  34. self.attach(p)
  35. if boundary:
  36. self.set_boundary(boundary)