email.contentmanager
email.headerregistry : 自定义头对象
email.headerregistry
email
源代码: Lib/email/contentmanager.py
Added in version 3.6: [ 1 ]
内容管理器的基类。提供标准注册机制注册的转换器介于 MIME 内容和其它表示,及 get_content and set_content 分派方法。
get_content
set_content
查找处理程序函数基于 mimetype of msg (见下一段落),透过传递所有自变量调用它,并返回调用结果。预期处理程序会提取负载从 msg 并返回有关提取数据编码信息的对象。
mimetype
要找到处理程序,在注册表中查找以下键,发现首个键时停止:
字符串表示完整 MIME 类型 ( maintype/subtype )
maintype/subtype
字符串表示 maintype
maintype
空字符串
若这些键未产生处理程序,引发 KeyError 对于完整 MIME 类型。
KeyError
若 maintype is multipart ,引发 TypeError ; otherwise look up a handler function based on the type of obj (see next paragraph), call clear_content() 在 msg , and call the handler function, passing through all arguments. The expectation is that the handler will transform and store obj into msg , possibly making other changes to msg as well, such as adding various MIME headers to encode information needed to interpret the stored data.
multipart
TypeError
clear_content()
To find the handler, obtain the type of obj ( typ = type(obj) ), and look for the following keys in the registry, stopping with the first one found:
typ = type(obj)
类型本身 ( typ )
typ
类型的完全合格名称 ( typ.__module__ + '.' + typ.__qualname__ ).
typ.__module__ + '.' + typ.__qualname__
类型的 qualname ( typ.__qualname__ )
typ.__qualname__
类型的名称 ( typ.__name__ ).
typ.__name__
If none of the above match, repeat all of the checks above for each of the types in the MRO ( typ.__mro__ ). Finally, if no other key yields a handler, check for a handler for the key None . If there is no handler for None ,引发 KeyError for the fully qualified name of the type.
typ.__mro__
None
Also add a MIME-Version header if one is not present (see also MIMEPart ).
MIMEPart
Record the function handler as the handler for key . For the possible values of key ,见 get_content() .
get_content()
Record handler as the function to call when an object of a type matching typekey 被传递给 set_content() . For the possible values of typekey ,见 set_content() .
set_content()
Currently the email package provides only one concrete content manager, raw_data_manager , although more may be added in the future. raw_data_manager 是 content_manager provided by EmailPolicy and its derivatives.
raw_data_manager
content_manager
EmailPolicy
This content manager provides only a minimum interface beyond that provided by Message itself: it deals only with text, raw byte strings, and Message objects. Nevertheless, it provides significant advantages compared to the base API: get_content on a text part will return a unicode string without the application needing to manually decode it, set_content provides a rich set of options for controlling the headers added to a part and controlling the content transfer encoding, and it enables the use of the various add_ methods, thereby simplifying the creation of multipart messages.
Message
add_
Return the payload of the part as either a string (for text parts), an EmailMessage object (for message/rfc822 parts), or a bytes object (for all other non-multipart types). Raise a KeyError if called on a multipart . If the part is a text part and errors is specified, use it as the error handler when decoding the payload to unicode. The default error handler is replace .
text
EmailMessage
message/rfc822
bytes
replace
Add headers and payload to msg :
添加 Content-Type 头采用 maintype/subtype 值。
For str ,设置 MIME maintype to text , and set the subtype to subtype if it is specified, or plain if it is not.
str
plain
For bytes , use the specified maintype and subtype , or raise a TypeError if they are not specified.
For EmailMessage objects, set the maintype to message , and set the subtype to subtype if it is specified or rfc822 if it is not. If subtype is partial ,引发错误 ( bytes objects must be used to construct message/partial parts).
message
rfc822
partial
message/partial
若 charset is provided (which is valid only for str ), encode the string to bytes using the specified character set. The default is utf-8 。若指定 charset is a known alias for a standard MIME charset name, use the standard charset instead.
utf-8
若 cte is set, encode the payload using the specified content transfer encoding, and set the Content-Transfer-Encoding header to that value. Possible values for cte are quoted-printable , base64 , 7bit , 8bit ,和 binary . If the input cannot be encoded in the specified encoding (for example, specifying a cte of 7bit for an input that contains non-ASCII values), raise a ValueError .
quoted-printable
base64
7bit
8bit
binary
ValueError
For str objects, if cte is not set use heuristics to determine the most compact encoding.
For EmailMessage , per RFC 2046 , raise an error if a cte of quoted-printable or base64 is requested for subtype rfc822 , and for any cte 除了 7bit for subtype external-body 。对于 message/rfc822 ,使用 8bit if cte is not specified. For all other values of subtype ,使用 7bit .
external-body
注意
A cte of binary does not actually work correctly yet. The EmailMessage object as modified by set_content is correct, but BytesGenerator does not serialize it correctly.
BytesGenerator
若 disposition is set, use it as the value of the Content-Disposition header. If not specified, and filename is specified, add the header with the value attachment 。若 disposition is not specified and filename is also not specified, do not add the header. The only valid values for disposition are attachment and inline .
attachment
inline
若 filename is specified, use it as the value of the filename 参数为 Content-Disposition 头。
filename
若 cid is specified, add a Content-ID header with cid as its value.
若 params is specified, iterate its items method and use the resulting (key, value) pairs to set additional parameters on the Content-Type 头。
items
(key, value)
若 headers is specified and is a list of strings of the form headername: headervalue 或列表化的 header objects (distinguished from strings by having a name attribute), add the headers to msg .
headername: headervalue
header
name
脚注
email :范例
键入搜索术语或模块、类、函数名称。