2016-08-30 25 views
1

我正在編寫腳本來處理電子郵件,並且我可以訪問電子郵件的原始字符串內容。Python - 自動檢測電子郵件內容編碼

我目前正在尋找字符串「Content-Transfer-Encoding:」並掃描緊隨其後的字符,以確定編碼。示例編碼:base64或7bit或quoted-printable ..

有沒有更好的方法來自動確定電子郵件編碼(至少更pythonic的方式)?

謝謝。

回答

1

你可以使用這個標準的Python包:email

例如:

import email 

raw = """From: John Doe <[email protected]> 
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