email -- An email and MIME handling package

::-- zhuyj [2008-07-07 14:32:33]

New in version 2.2.

email包是一个用来管理email消息的库,email消息包含MIME和其他基于RFC 2822的信息文档。它汇集了几个陈旧的标准模块,例如rfc822,mimetools,multifile,和其他非标准的包例如mimecntl的大多数功能。很明显它不是被设计用来做例如向SMTP(RFC 2821), NNTP,或其他服务发送邮件,那是smtplib和nntplib模块的功能。email包试图尽可能的适应RFC,支持除RFC 2822外,与MIME有关联的RFC,比如RFC 2045, RFC 2046, RFC 2047, 和 RFC 2231。

email包最主要的区别特性是通过email的内部对象模型表示法来拆分分析和生成email消息。应用程序利用email包主要处理对象;你可以给消息增加子对象,从消息中删除子对象,完全的重新整理目录等等。有一个独立的分析器和独立的生器用来完成单调的文本到对象模型,然后再到单调的文本的转换。并且还有便捷的子类针对一些通用的MIME对象类型,和一些多种功能用来进行一些通用任务例如提取和分析消息域的值,创建适用于RFC的日期等等。

See Also: Module smtplib:

SMTP protocol client.

Module nntplib:

NNTP protocol client.

1. Representing an email message

The central class in the email package is the Message class, imported from the email.message module. It is the base class for the email object model. Message provides the core functionality for setting and querying header fields, and for accessing message bodies.

Message objects provide a mapping style interface for accessing the message headers, and an explicit interface for accessing both the headers and the payload. It provides convenience methods for generating a flat text representation of the message object tree, for accessing commonly used header parameters, and for recursively walking over the object tree.

class Message( )

as_string( [unixfrom])

   1 from cStringIO import StringIO
   2 from email.generator import Generator
   3 fp = StringIO()
   4 g = Generator(fp, mangle_from_=False, maxheaderlen=60)
   5 g.flatten(msg)
   6 text = fp.getvalue()

str( )

is_multipart( )

set_unixfrom( unixfrom)

get_unixfrom( )

attach( payload)

get_payload( [i[, decode]])

set_payload( payload[, charset])

Changed in version 2.2.2: charset argument added.

set_charset( charset)

New in version 2.2.2.

get_charset( )

len( )

contains( name)

   1 if 'message-id' in myMessage:
   2     print 'Message-ID:', myMessage['message-id']

getitem( name)

setitem( name, val)

   1 del msg['subject']
   2 msg['subject'] = 'Python roolz!'

delitem( name)

has_key( name)

keys( )

values( )

items( )

get( name[, failobj])

get_all( name[, failobj])

add_header( _name, _value, **_params)

   1 msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')

Content-Disposition: attachment; filename="bud.gif"

replace_header( _name, _value)

New in version 2.2.2.

get_content_type( )

RFC 2045 defines a message's default type to be text/plain unless it appears inside a multipart/digest container, in which case it would be message/rfc822. If the Content-Type: header has an invalid type specification, RFC 2045 mandates that the default type be text/plain.

New in version 2.2.2.

get_content_maintype( )

New in version 2.2.2.

get_content_subtype( )

New in version 2.2.2.

get_default_type( )

New in version 2.2.2.

set_default_type( ctype)

New in version 2.2.2.

get_params( [failobj[, header[, unquote]]])

Changed in version 2.2.2: unquote argument added.

get_param( param[, failobj[, header[, unquote]]])

   1 rawparam = msg.get_param('foo')
   2 param = email.Utils.collapse_rfc2231_value(rawparam)

Changed in version 2.2.2: unquote argument added, and 3-tuple return value possible. set_param( param, value[, header[, requote[, charset[, language]]]])

New in version 2.2.2.

del_param( param[, header[, requote]])

New in version 2.2.2.

set_type( type[, header][, requote])

New in version 2.2.2.

get_filename( [failobj])

get_boundary( [failobj])

set_boundary( boundary)

get_content_charset( [failobj])

New in version 2.2.2.

get_charsets( [failobj])

walk( )

>>> for part in msg.walk():
...     print part.get_content_type()
multipart/report
text/plain
message/delivery-status
text/plain
text/plain
message/rfc822

Changed in version 2.5: The previously deprecated methods get_type(), get_main_type(), and get_subtype() were removed.

Message objects can also optionally contain two instance attributes, which can be used when generating the plain text of a MIME message.

preamble

epilogue

Changed in version 2.5: You do not need to set the epilogue to the empty string in order for the Generator to print a newline at the end of the file.

defects

New in version 2.4.

2. 交流

email (last edited 2009-12-25 07:10:13 by localhost)