/Doc/library/email.util.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 166 lines · 107 code · 59 blank · 0 comment · 0 complexity · 507f96ccffa86da047e350284ac2ca18 MD5 · raw file

  1. :mod:`email`: Miscellaneous utilities
  2. -------------------------------------
  3. .. module:: email.utils
  4. :synopsis: Miscellaneous email package utilities.
  5. There are several useful utilities provided in the :mod:`email.utils` module:
  6. .. function:: quote(str)
  7. Return a new string with backslashes in *str* replaced by two backslashes, and
  8. double quotes replaced by backslash-double quote.
  9. .. function:: unquote(str)
  10. Return a new string which is an *unquoted* version of *str*. If *str* ends and
  11. begins with double quotes, they are stripped off. Likewise if *str* ends and
  12. begins with angle brackets, they are stripped off.
  13. .. function:: parseaddr(address)
  14. Parse address -- which should be the value of some address-containing field such
  15. as :mailheader:`To` or :mailheader:`Cc` -- into its constituent *realname* and
  16. *email address* parts. Returns a tuple of that information, unless the parse
  17. fails, in which case a 2-tuple of ``('', '')`` is returned.
  18. .. function:: formataddr(pair)
  19. The inverse of :meth:`parseaddr`, this takes a 2-tuple of the form ``(realname,
  20. email_address)`` and returns the string value suitable for a :mailheader:`To` or
  21. :mailheader:`Cc` header. If the first element of *pair* is false, then the
  22. second element is returned unmodified.
  23. .. function:: getaddresses(fieldvalues)
  24. This method returns a list of 2-tuples of the form returned by ``parseaddr()``.
  25. *fieldvalues* is a sequence of header field values as might be returned by
  26. :meth:`Message.get_all`. Here's a simple example that gets all the recipients
  27. of a message::
  28. from email.utils import getaddresses
  29. tos = msg.get_all('to', [])
  30. ccs = msg.get_all('cc', [])
  31. resent_tos = msg.get_all('resent-to', [])
  32. resent_ccs = msg.get_all('resent-cc', [])
  33. all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)
  34. .. function:: parsedate(date)
  35. Attempts to parse a date according to the rules in :rfc:`2822`. however, some
  36. mailers don't follow that format as specified, so :func:`parsedate` tries to
  37. guess correctly in such cases. *date* is a string containing an :rfc:`2822`
  38. date, such as ``"Mon, 20 Nov 1995 19:12:08 -0500"``. If it succeeds in parsing
  39. the date, :func:`parsedate` returns a 9-tuple that can be passed directly to
  40. :func:`time.mktime`; otherwise ``None`` will be returned. Note that indexes 6,
  41. 7, and 8 of the result tuple are not usable.
  42. .. function:: parsedate_tz(date)
  43. Performs the same function as :func:`parsedate`, but returns either ``None`` or
  44. a 10-tuple; the first 9 elements make up a tuple that can be passed directly to
  45. :func:`time.mktime`, and the tenth is the offset of the date's timezone from UTC
  46. (which is the official term for Greenwich Mean Time) [#]_. If the input string
  47. has no timezone, the last element of the tuple returned is ``None``. Note that
  48. indexes 6, 7, and 8 of the result tuple are not usable.
  49. .. function:: mktime_tz(tuple)
  50. Turn a 10-tuple as returned by :func:`parsedate_tz` into a UTC timestamp. It
  51. the timezone item in the tuple is ``None``, assume local time. Minor
  52. deficiency: :func:`mktime_tz` interprets the first 8 elements of *tuple* as a
  53. local time and then compensates for the timezone difference. This may yield a
  54. slight error around changes in daylight savings time, though not worth worrying
  55. about for common use.
  56. .. function:: formatdate([timeval[, localtime][, usegmt]])
  57. Returns a date string as per :rfc:`2822`, e.g.::
  58. Fri, 09 Nov 2001 01:08:47 -0000
  59. Optional *timeval* if given is a floating point time value as accepted by
  60. :func:`time.gmtime` and :func:`time.localtime`, otherwise the current time is
  61. used.
  62. Optional *localtime* is a flag that when ``True``, interprets *timeval*, and
  63. returns a date relative to the local timezone instead of UTC, properly taking
  64. daylight savings time into account. The default is ``False`` meaning UTC is
  65. used.
  66. Optional *usegmt* is a flag that when ``True``, outputs a date string with the
  67. timezone as an ascii string ``GMT``, rather than a numeric ``-0000``. This is
  68. needed for some protocols (such as HTTP). This only applies when *localtime* is
  69. ``False``.
  70. .. versionadded:: 2.4
  71. .. function:: make_msgid([idstring])
  72. Returns a string suitable for an :rfc:`2822`\ -compliant
  73. :mailheader:`Message-ID` header. Optional *idstring* if given, is a string used
  74. to strengthen the uniqueness of the message id.
  75. .. function:: decode_rfc2231(s)
  76. Decode the string *s* according to :rfc:`2231`.
  77. .. function:: encode_rfc2231(s[, charset[, language]])
  78. Encode the string *s* according to :rfc:`2231`. Optional *charset* and
  79. *language*, if given is the character set name and language name to use. If
  80. neither is given, *s* is returned as-is. If *charset* is given but *language*
  81. is not, the string is encoded using the empty string for *language*.
  82. .. function:: collapse_rfc2231_value(value[, errors[, fallback_charset]])
  83. When a header parameter is encoded in :rfc:`2231` format,
  84. :meth:`Message.get_param` may return a 3-tuple containing the character set,
  85. language, and value. :func:`collapse_rfc2231_value` turns this into a unicode
  86. string. Optional *errors* is passed to the *errors* argument of the built-in
  87. :func:`unicode` function; it defaults to ``replace``. Optional
  88. *fallback_charset* specifies the character set to use if the one in the
  89. :rfc:`2231` header is not known by Python; it defaults to ``us-ascii``.
  90. For convenience, if the *value* passed to :func:`collapse_rfc2231_value` is not
  91. a tuple, it should be a string and it is returned unquoted.
  92. .. function:: decode_params(params)
  93. Decode parameters list according to :rfc:`2231`. *params* is a sequence of
  94. 2-tuples containing elements of the form ``(content-type, string-value)``.
  95. .. versionchanged:: 2.4
  96. The :func:`dump_address_pair` function has been removed; use :func:`formataddr`
  97. instead.
  98. .. versionchanged:: 2.4
  99. The :func:`decode` function has been removed; use the
  100. :meth:`Header.decode_header` method instead.
  101. .. versionchanged:: 2.4
  102. The :func:`encode` function has been removed; use the :meth:`Header.encode`
  103. method instead.
  104. .. rubric:: Footnotes
  105. .. [#] Note that the sign of the timezone offset is the opposite of the sign of the
  106. ``time.timezone`` variable for the same timezone; the latter variable follows
  107. the POSIX standard while this module follows :rfc:`2822`.