email.mime
: 從頭創(chuàng)建電子郵件和 MIME 對象?
源代碼: Lib/email/mime/
此模塊是舊版 (Compat32
) 電子郵件 API 的組成部分。 它的功能在新版 API 中被 contentmanager
部分替代,但在某些應(yīng)用中這些類仍可能有用,即使是在非舊版代碼中。
通常,你是通過傳遞一個文件或一些文本到解析器來獲得消息對象結(jié)構(gòu)體的,解析器會解析文本并返回根消息對象。 不過你也可以從頭開始構(gòu)建一個完整的消息結(jié)構(gòu)體,甚至是手動構(gòu)建單獨的 Message
對象。 實際上,你也可以接受一個現(xiàn)有的結(jié)構(gòu)體并添加新的 Message
對象并移動它們。 這為切片和分割 MIME 消息提供了非常方便的接口。
你可以通過創(chuàng)建 Message
實例并手動添加附件和所有適當?shù)臉祟^來創(chuàng)建一個新的對象結(jié)構(gòu)體。 不過對于 MIME 消息來說,email
包提供了一些便捷子類來讓事情變得更容易。
這些類列示如下:
- class email.mime.base.MIMEBase(_maintype, _subtype, *, policy=compat32, **_params)?
模塊:
email.mime.base
這是
Message
的所有 MIME 專屬子類。 通常你不會創(chuàng)建專門的MIMEBase
實例,盡管你可以這樣做。MIMEBase
主要被提供用來作為更具體的 MIME 感知子類的便捷基類。_maintype 是 Content-Type 的主類型 (例如 text 或 image),而 _subtype 是 Content-Type 的次類型 (例如 plain 或 gif)。 _params 是一個形參鍵/值字典并會被直接傳遞給
Message.add_header
。如果指定了 policy (默認為
compat32
策略),它將被傳遞給Message
。MIMEBase
類總是會添加一個 Content-Type 標頭 (基于 _maintype, _subtype 和 _params),以及一個 MIME-Version 標頭 (總是設(shè)為1.0
)。在 3.6 版更改: 添加了 policy 僅限關(guān)鍵字形參。
- class email.mime.nonmultipart.MIMENonMultipart?
模塊:
email.mime.nonmultipart
MIMEBase
的子類,這是用于非 multipart MIME 消息的中間基類。 這個類的主要目標是避免使用attach()
方法,該方法僅對 multipart 消息有意義。 如果attach()
被調(diào)用,則會引發(fā)MultipartConversionError
異常。
- class email.mime.multipart.MIMEMultipart(_subtype='mixed', boundary=None, _subparts=None, *, policy=compat32, **_params)?
模塊:
email.mime.multipart
MIMEBase
的子類,這是用于 multipart MIME 消息的中間基類。 可選的 _subtype 默認為 mixed,但可被用來指定消息的子類型。 將會在消息對象中添加一個:mimetype:multipart/_subtype 的 Content-Type 標頭。 并還將添加一個 MIME-Version 標頭。可選的 boundary 是多部分邊界字符串。 當為
None
(默認值) 時,則會在必要時(例如當消息被序列化時)計算邊界。_subparts 是載荷初始子部分的序列。 此序列必須可以被轉(zhuǎn)換為列表。 你總是可以使用
Message.attach
方法將新的子部分附加到消息中。可選的 policy 參數(shù)默認為
compat32
。用于 Content-Type 標頭的附加形參會從關(guān)鍵字參數(shù)中獲取,或者傳入到 _params 參數(shù),該參數(shù)是一個關(guān)鍵字的字典。
在 3.6 版更改: 添加了 policy 僅限關(guān)鍵字形參。
- class email.mime.application.MIMEApplication(_data, _subtype='octet-stream', _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)?
模塊:
email.mime.application
MIMENonMultipart
的子類,MIMEApplication
類被用來表示主類型為 application 的 MIME 消息。 _data 是包含原始字節(jié)數(shù)據(jù)的字符串。 可選的 _subtype 指定 MIME 子類型并默認為 octet-stream。可選的 _encoder 是一個可調(diào)用對象(即函數(shù)),它將執(zhí)行實際的數(shù)據(jù)編碼以便傳輸。 這個可調(diào)用對象接受一個參數(shù),該參數(shù)是
MIMEApplication
的實例。 它應(yīng)當使用get_payload()
和set_payload()
來將載荷改為已編碼形式。 它還應(yīng)根據(jù)需要將任何 Content-Transfer-Encoding 或其他標頭添加到消息對象中。 默認編碼格式為 base64。 請參閱email.encoders
模塊來查看內(nèi)置編碼器列表。可選的 policy 參數(shù)默認為
compat32
。_params 會被直接傳遞給基類的構(gòu)造器。
在 3.6 版更改: 添加了 policy 僅限關(guān)鍵字形參。
- class email.mime.audio.MIMEAudio(_audiodata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)?
模塊:
email.mime.audio
A subclass of
MIMENonMultipart
, theMIMEAudio
class is used to create MIME message objects of major type audio. _audiodata is a string containing the raw audio data. If this data can be decoded as au, wav, aiff, or aifc, then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the audio subtype via the _subtype argument. If the minor type could not be guessed and _subtype was not given, thenTypeError
is raised.可選的 _encoder 是一個可調(diào)用對象(即函數(shù)),它將執(zhí)行實際的音頻數(shù)據(jù)編碼以便傳輸。 這個可調(diào)用對象接受一個參數(shù),該參數(shù)是
MIMEAudio
的實例。 它應(yīng)當使用get_payload()
和set_payload()
來將載荷改為已編碼形式。 它還應(yīng)根據(jù)需要將任何 Content-Transfer-Encoding 或其他標頭添加到消息對象中。 默認編碼格式為 base64。 請參閱email.encoders
模塊來查看內(nèi)置編碼器列表。可選的 policy 參數(shù)默認為
compat32
。_params 會被直接傳遞給基類的構(gòu)造器。
在 3.6 版更改: 添加了 policy 僅限關(guān)鍵字形參。
- class email.mime.image.MIMEImage(_imagedata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)?
模塊:
email.mime.image
A subclass of
MIMENonMultipart
, theMIMEImage
class is used to create MIME message objects of major type image. _imagedata is a string containing the raw image data. If this data type can be detected (jpeg, png, gif, tiff, rgb, pbm, pgm, ppm, rast, xbm, bmp, webp, and exr attempted), then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the image subtype via the _subtype argument. If the minor type could not be guessed and _subtype was not given, thenTypeError
is raised.可選的 _encoder 是一個可調(diào)用對象(即函數(shù)),它將執(zhí)行實際的圖像數(shù)據(jù)編碼以便傳輸。 這個可調(diào)用對象接受一個參數(shù),該參數(shù)是
MIMEImage
的實例。 它應(yīng)當使用get_payload()
和set_payload()
來將載荷改為已編碼形式。 它還應(yīng)根據(jù)需要將任何 Content-Transfer-Encoding 或其他標頭添加到消息對象中。 默認編碼格式為 base64。 請參閱email.encoders
模塊來查看內(nèi)置編碼器列表。可選的 policy 參數(shù)默認為
compat32
。_params 會被直接傳遞給
MIMEBase
構(gòu)造器。在 3.6 版更改: 添加了 policy 僅限關(guān)鍵字形參。
- class email.mime.message.MIMEMessage(_msg, _subtype='rfc822', *, policy=compat32)?
模塊:
email.mime.message
MIMENonMultipart
的子類,MIMEMessage
類被用來創(chuàng)建主類型為 message 的 MIME 對象。 _msg 將被用作載荷,并且必須為Message
類(或其子類)的實例,否則會引發(fā)TypeError
。可選的 _subtype 設(shè)置消息的子類型;它的默認值為 rfc822。
可選的 policy 參數(shù)默認為
compat32
。在 3.6 版更改: 添加了 policy 僅限關(guān)鍵字形參。
- class email.mime.text.MIMEText(_text, _subtype='plain', _charset=None, *, policy=compat32)?
模塊:
email.mime.text
MIMENonMultipart
的子類,MIMEText
類被用來創(chuàng)建主類型為 text 的 MIME 對象。 _text 是用作載荷的字符串。 _subtype 指定子類型并且默認為 plain。 _charset 是文本的字符集并會作為參數(shù)傳遞給MIMENonMultipart
構(gòu)造器;如果該字符串僅包含ascii
碼位則其默認值為us-ascii
,否則為utf-8
。 _charset 形參接受一個字符串或是一個Charset
實例。除非 _charset 參數(shù)被顯式地設(shè)為
None
,否則所創(chuàng)建的 MIMEText 對象將同時具有附帶charset
形參的 Content-Type 標頭,以及 Content-Transfer-Encoding 標頭。 這意味著后續(xù)的set_payload
調(diào)用將不再產(chǎn)生已編碼的載荷,即使它在set_payload
命令中被傳入。 你可以通過刪除Content-Transfer-Encoding
標頭來“重置”此行為,在此之后的set_payload
調(diào)用將自動編碼新的載荷(并添加新的 Content-Transfer-Encoding 標頭)。可選的 policy 參數(shù)默認為
compat32
。在 3.5 版更改: _charset 也可接受
Charset
實例。在 3.6 版更改: 添加了 policy 僅限關(guān)鍵字形參。