用法:
as_string(unixfrom=False, maxheaderlen=0, policy=None)
以字符串形式返回整條消息。當可選的
unixfrom
為真時,信封頭包含在返回的字符串中。unixfrom
默認為False
。出於向後兼容的原因,maxheaderlen
默認為0
,因此如果您想要不同的值,則必須明確覆蓋它(此方法將忽略策略中為max_line_length
指定的值)。policy
參數可用於覆蓋從消息實例獲得的默認策略。這可用於控製該方法生成的一些格式,因為指定的policy
將傳遞給Generator
。如果需要填寫默認值以完成到字符串的轉換(例如,可能會生成或修改 MIME 邊界),則展平消息可能會觸發對
Message
的更改。請注意,提供此方法是為了方便,可能並不總是按照您想要的方式格式化消息。例如,默認情況下,它不會對 unix mbox 格式所需的以
From
開頭的行進行修改。為了獲得更大的靈活性,實例化一個Generator
實例並直接使用它的flatten()
方法。例如:from io import StringIO from email.generator import Generator fp = StringIO() g = Generator(fp, mangle_from_=True, maxheaderlen=60) g.flatten(msg) text = fp.getvalue()
如果消息對象包含未根據 RFC 標準編碼的二進製數據,則不符合標準的數據將替換為 unicode “unknown character” 代碼點。 (另見
as_bytes()
和BytesGenerator
。)在 3.4 版中更改:
policy
添加了關鍵字參數。
相關用法
- Python email.message.Message.as_bytes用法及代碼示例
- Python email.message.Message.add_header用法及代碼示例
- Python email.message.Message.walk用法及代碼示例
- Python email.message.EmailMessage.add_header用法及代碼示例
- Python email.message.EmailMessage.walk用法及代碼示例
- Python email.headerregistry.DateHeader用法及代碼示例
- Python email.utils.getaddresses用法及代碼示例
- Python email.header.decode_header用法及代碼示例
- Python email.iterators._structure用法及代碼示例
- Python email.headerregistry.BaseHeader用法及代碼示例
- Python emoji轉text用法及代碼示例
- Python numpy matrix empty()用法及代碼示例
- Python numpy matrix eye()用法及代碼示例
- Python enchant.request_dict()用法及代碼示例
- Python eval()用法及代碼示例
- Python enum.IntEnum用法及代碼示例
- Python math expm1()用法及代碼示例
- Python enchant.get_enchant_version()用法及代碼示例
- Python enchant.request_pwl_dict()用法及代碼示例
- Python eval用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 email.message.Message.as_string。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。