如何确定邮件头是否是base64编码 [英] how can I determine whether an email header is base64 encoded
问题描述
使用email.header包,我可以执行
Using the email.header package, I can do
the_text,the_charset = decode_header(inputText)
获取电子邮件标题的字符集,其中inputText由诸如
to get the character set of the email header, where the inputText was retrieved by a command like
inputText = msg.get('From')
以使用From:头为例。
to use the From: header as an example.
为了提取该标题的标题编码,我需要这样做吗?:
in order to extract the header encoding for that header, do I have to do something like this?:
the_header_encoding = email.charset.Charset(the_charset).header_encoding
That is, do I have to create an instance of the Charset class based on the name of the charset (and would that even work?), or is there a way to extract the header encoding more directly from the header itself?
推荐答案
Encoded-Message 标题可以由1行或更多行组成,每一行都可以使用不同的编码,也可以不使用任何编码。
Encoded-Message header can consist of 1 or more lines, and each line can use a different encoding, or no encoding at all.
我们必须自己解析编码类型,每行一个。使用正则表达式:
You'll have to parse the type of encoding out yourself, one per line. Using a regular expression:
import re
quopri_entry = re.compile(r'=\?[\w-]+\?(?P<encoding>[QB])\?[^?]+?\?=', flags=re.I)
encodings = {'Q': 'quoted-printable', 'B': 'base64'}
def encoded_message_codecs(header):
used = []
for line in header.splitlines():
entry = quopri_entry.search(line)
if not entry:
used.append(None)
continue
used.append(encodings.get(entry.group('encoding').upper(), 'unknown'))
return used
这将返回从 quoted-printable
, base64
, unknown
或无
如果该行没有使用Encoded-Message。
This returns a list of strings drawn from quoted-printable
, base64
, unknown
or None
if no Encoded-Message was used for that line.
这篇关于如何确定邮件头是否是base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!