Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

7.1.9 Miscellaneous utilities

There are several useful utilities provided in theemail.utilsmodule:

quote(str)
Return a new string with backslashes instr replaced by twobackslashes, and double quotes replaced by backslash-double quote.

unquote(str)
Return a new string which is anunquoted version ofstr.Ifstr ends and begins with double quotes, they are strippedoff. Likewise ifstr ends and begins with angle brackets, theyare stripped off.

parseaddr(address)
Parse address - which should be the value of some address-containingfield such asTo: orCc: - into its constituentrealname andemail address parts. Returns a tuple of thatinformation, unless the parse fails, in which case a 2-tuple of('', '') is returned.

formataddr(pair)
The inverse ofparseaddr(), this takes a 2-tuple of the form(realname, email_address) and returns the string value suitablefor aTo: orCc: header. If the first element ofpair is false, then the second element is returned unmodified.

getaddresses(fieldvalues)
This method returns a list of 2-tuples of the form returned byparseaddr().fieldvalues is a sequence of header fieldvalues as might be returned byMessage.get_all(). Here's asimple example that gets all the recipients of a message:

from email.utils import getaddressestos = msg.get_all('to', [])ccs = msg.get_all('cc', [])resent_tos = msg.get_all('resent-to', [])resent_ccs = msg.get_all('resent-cc', [])all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)

parsedate(date)
Attempts to parse a date according to the rules inRFC 2822.however, some mailers don't follow that format as specified, soparsedate() tries to guess correctly in such cases.date is a string containing anRFC 2822 date, such as"Mon, 20 Nov 1995 19:12:08 -0500". If it succeeds in parsingthe date,parsedate() returns a 9-tuple that can be passeddirectly totime.mktime(); otherwiseNone will bereturned. Note that fields 6, 7, and 8 of the result tuple are notusable.

parsedate_tz(date)
Performs the same function asparsedate(), but returnseitherNone or a 10-tuple; the first 9 elements make up a tuplethat can be passed directly totime.mktime(), and the tenthis the offset of the date's timezone from UTC (which is the officialterm for Greenwich Mean Time)7.3. If the inputstring has no timezone, the last element of the tuple returned isNone. Note that fields 6, 7, and 8 of the result tuple are notusable.

mktime_tz(tuple)
Turn a 10-tuple as returned byparsedate_tz() into a UTCtimestamp. It the timezone item in the tuple isNone, assumelocal time. Minor deficiency:mktime_tz() interprets thefirst 8 elements oftuple as a local time and then compensatesfor the timezone difference. This may yield a slight error aroundchanges in daylight savings time, though not worth worrying about forcommon use.

formatdate([timeval[, localtime][, usegmt]])
Returns a date string as perRFC 2822, e.g.:

Fri, 09 Nov 2001 01:08:47 -0000

Optionaltimeval if given is a floating point time value asaccepted bytime.gmtime() andtime.localtime(),otherwise the current time is used.

Optionallocaltime is a flag that whenTrue, interpretstimeval, and returns a date relative to the local timezoneinstead of UTC, properly taking daylight savings time into account.The default isFalse meaning UTC is used.

Optionalusegmt is a flag that whenTrue, outputs a date string with the timezone as an ascii stringGMT, ratherthan a numeric-0000. This is needed for some protocols (suchas HTTP). This only applies whenlocaltime isFalse.New in version 2.4.

make_msgid([idstring])
Returns a string suitable for anRFC 2822-compliantMessage-ID: header. Optionalidstring if given, isa string used to strengthen the uniqueness of the message id.

decode_rfc2231(s)
Decode the strings according toRFC 2231.

encode_rfc2231(s[, charset[, language]])
Encode the strings according toRFC 2231. Optionalcharset andlanguage, if given is the character set nameand language name to use. If neither is given,s is returnedas-is. Ifcharset is given butlanguage is not, thestring is encoded using the empty string forlanguage.

collapse_rfc2231_value(value[, errors[, fallback_charset]])
When a header parameter is encoded inRFC 2231 format,Message.get_param() may return a 3-tuple containing the characterset, language, and value.collapse_rfc2231_value() turns this intoa unicode string. Optionalerrors is passed to theerrorsargument of the built-inunicode() function; it defaults toreplace. Optionalfallback_charset specifies the character setto use if the one in theRFC 2231 header is not known by Python; it defaultstous-ascii.

For convenience, if thevalue passed tocollapse_rfc2231_value() is not a tuple, it should be a string andit is returned unquoted.

decode_params(params)
Decode parameters list according toRFC 2231.params is asequence of 2-tuples containing elements of the form(content-type, string-value).

Changed in version 2.4:Thedump_address_pair() function has been removed;useformataddr() instead.

Changed in version 2.4:Thedecode() function has been removed; use theHeader.decode_header() method instead.

Changed in version 2.4:Theencode() function has been removed; use theHeader.encode() method instead.



Footnotes

... Time)7.3
Note that the sign of the timezoneoffset is the opposite of the sign of thetime.timezonevariable for the same timezone; the latter variable follows thePOSIX standard while this module followsRFC 2822.


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp