Handle RFC 2047 encoded headers

This commit is contained in:
Peter J. Holzer 2019-04-30 21:55:21 +02:00
parent 54290fb668
commit 8d78b2ec26
1 changed files with 26 additions and 4 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/python3
import email.header
import email.parser
import hashlib
import html
@ -32,18 +33,39 @@ def get_message_id(msg):
match = re.search(r'<(.*?)>', msg["Message-ID"])
return match.group(1)
def encode_message_id(msgid):
encmsgid = re.sub('[^!"$(-.0-9:=@-z|~]', lambda x: "{%02x}" % (ord(x.group(0))), msgid)
return encmsgid
def decode_rfc2047(s):
if s is None:
return None
r = ""
for chunk in email.header.decode_header(s):
if chunk[1]:
try:
r += chunk[0].decode(chunk[1])
except LookupError:
r += chunk[0].decode("windows-1252")
except UnicodeDecodeError:
r += chunk[0].decode("windows-1252")
elif type(chunk[0]) == bytes:
r += chunk[0].decode('us-ascii')
else:
r += chunk[0]
return r
def render_message(msg):
msgtmpl = jenv.get_template("message2.html")
bodyhtml = render_body(msg)
context = {
"msg": msg,
"message_id": msg["Message-Id"],
"subject": msg["Subject"],
"from": msg["From"],
"subject": decode_rfc2047(msg["Subject"]),
"from": decode_rfc2047(msg["From"]),
"date": msg["Date"],
"bodyhtml": bodyhtml,
}
@ -338,8 +360,8 @@ def archive(msg):
context = {
"list": "LUGA",
"message_id": mid,
"subject": msg["Subject"],
"from": msg["From"],
"subject": decode_rfc2047(msg["Subject"]),
"from": decode_rfc2047(msg["From"]),
"date": msg["Date"],
"bodyhtml": bodyhtml,
}