Skip to main content
 首页 » 编程设计

Python之自动检测电子邮件内容编码

2025年04月02日28yxwkf

我正在编写一个脚本来处理电子邮件,并且我可以访问电子邮件的原始字符串内容。

我目前正在寻找字符串“Content-Transfer-Encoding:”并扫描紧随其后的字符以确定编码。示例编码:base64 或 7 位或引用打印..

是否有更好的方法来自动确定电子邮件编码(至少是一种更 pythonic 的方式)?

谢谢。

请您参考如下方法:

您可以使用这个标准的 Python 包:email .

例如:

import email 
 
raw = """From: John Doe <example@example.com> 
MIME-Version: 1.0 
Content-Type: text/plain 
Content-Transfer-Encoding: quoted-printable 
 
Hi there! 
""" 
 
my_email = email.message_from_string(raw) 
print my_email["Content-Transfer-Encoding"] 

查看其他示例 here .