Add decoding of mail headers

This commit is contained in:
2020-01-29 21:35:37 +01:00
parent f4399312b4
commit 983362a69a
3 changed files with 29 additions and 17 deletions

View File

@@ -20,6 +20,8 @@ import logging.handlers
import sys
import time
from email.header import decode_header, make_header
import pyquarantine
from pyquarantine.version import __version__ as version
@@ -56,7 +58,7 @@ def print_table(columns, rows):
# get the length of the longest value
lengths.append(
len(str(max(rows, key=lambda x: len(str(x[key])))[key])))
# use the the longer one
# use the longer one
length = max(lengths)
column_lengths.append(length)
column_formats.append("{{:<{}}}".format(length))
@@ -120,7 +122,9 @@ def list_quarantine_emails(config, args):
row["recipient"] = metadata["recipients"].pop(0)
if "subject" not in emails[quarantine_id]["headers"].keys():
emails[quarantine_id]["headers"]["subject"] = ""
row["subject"] = emails[quarantine_id]["headers"]["subject"][:60]
row["subject"] = str(make_header(decode_header(
emails[quarantine_id]["headers"]["subject"])))[:60].replace(
"\r", "").replace("\n", "").strip()
rows.append(row)
if metadata["recipients"]:

View File

@@ -19,6 +19,7 @@ import re
from bs4 import BeautifulSoup
from cgi import escape
from collections import defaultdict
from email.header import decode_header, make_header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
@@ -351,21 +352,26 @@ class EMailNotification(BaseNotification):
"{}: generating notification email for '{}'".format(
queueid, recipient))
self.logger.debug("{}: parsing email template".format(queueid))
if "from" not in headers.keys():
headers["from"] = ""
if "to" not in headers.keys():
headers["to"] = ""
if "subject" not in headers.keys():
headers["subject"] = ""
# decode some headers
decoded_headers = {}
for var in ["from", "to", "subject"]:
if header in headers:
decoded_headers[header] = str(
make_header(decode_header(headers[header])))
else:
headers[var] = ""
decoded_headers[var] = ""
# generate dict containing all template variables
variables = defaultdict(str,
EMAIL_HTML_TEXT=sanitized_text,
EMAIL_FROM=escape(headers["from"]),
EMAIL_FROM=escape(decoded_headers["from"]),
EMAIL_ENVELOPE_FROM=escape(mailfrom),
EMAIL_ENVELOPE_FROM_URL=escape(quote(mailfrom)),
EMAIL_TO=escape(recipient),
EMAIL_TO_URL=escape(quote(recipient)),
EMAIL_SUBJECT=escape(headers["subject"]),
EMAIL_TO=escape(decoded_headers["to"]),
EMAIL_ENVELOPE_TO=escape(recipient),
EMAIL_ENVELOPE_TO_URL=escape(quote(recipient)),
EMAIL_SUBJECT=escape(decoded_headers["subject"]),
EMAIL_QUARANTINE_ID=quarantine_id)
if subgroups: