Add decoding of mail headers
This commit is contained in:
12
README.md
12
README.md
@@ -77,15 +77,17 @@ The following configuration options are optional in each quarantine section:
|
|||||||
|
|
||||||
The following template variables are available:
|
The following template variables are available:
|
||||||
* **{EMAIL_ENVELOPE_FROM}**
|
* **{EMAIL_ENVELOPE_FROM}**
|
||||||
E-mail from address received by the milter.
|
E-mail sender address received by the milter.
|
||||||
* **{EMAIL_ENVELOPE_FROM_URL}**
|
* **{EMAIL_ENVELOPE_FROM_URL}**
|
||||||
Like EMAIL_ENVELOPE_FROM, but URL encoded
|
Like EMAIL_ENVELOPE_FROM, but URL encoded
|
||||||
* **{EMAIL_FROM}**
|
* **{EMAIL_FROM}**
|
||||||
Value of the from header of the original e-mail.
|
Value of the FROM header of the original e-mail.
|
||||||
* **{EMAIL_TO}**
|
* **{EMAIL_ENVELOPE_TO}**
|
||||||
E-mail recipient address of this notification.
|
E-mail recipient address of this notification.
|
||||||
* **{EMAIL_TO_URL}**
|
* **{EMAIL_ENVELOPE_TO_URL}**
|
||||||
Like EMAIL_TO, but URL encoded
|
Like EMAIL_ENVELOPE_TO, but URL encoded
|
||||||
|
* **{EMAIL_TO}**
|
||||||
|
Value of the TO header of the original e-mail.
|
||||||
* **{EMAIL_SUBJECT}**
|
* **{EMAIL_SUBJECT}**
|
||||||
Configured e-mail subject.
|
Configured e-mail subject.
|
||||||
* **{EMAIL_QUARANTINE_ID}**
|
* **{EMAIL_QUARANTINE_ID}**
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ import logging.handlers
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from email.header import decode_header, make_header
|
||||||
|
|
||||||
import pyquarantine
|
import pyquarantine
|
||||||
|
|
||||||
from pyquarantine.version import __version__ as version
|
from pyquarantine.version import __version__ as version
|
||||||
@@ -56,7 +58,7 @@ def print_table(columns, rows):
|
|||||||
# get the length of the longest value
|
# get the length of the longest value
|
||||||
lengths.append(
|
lengths.append(
|
||||||
len(str(max(rows, key=lambda x: len(str(x[key])))[key])))
|
len(str(max(rows, key=lambda x: len(str(x[key])))[key])))
|
||||||
# use the the longer one
|
# use the longer one
|
||||||
length = max(lengths)
|
length = max(lengths)
|
||||||
column_lengths.append(length)
|
column_lengths.append(length)
|
||||||
column_formats.append("{{:<{}}}".format(length))
|
column_formats.append("{{:<{}}}".format(length))
|
||||||
@@ -120,7 +122,9 @@ def list_quarantine_emails(config, args):
|
|||||||
row["recipient"] = metadata["recipients"].pop(0)
|
row["recipient"] = metadata["recipients"].pop(0)
|
||||||
if "subject" not in emails[quarantine_id]["headers"].keys():
|
if "subject" not in emails[quarantine_id]["headers"].keys():
|
||||||
emails[quarantine_id]["headers"]["subject"] = ""
|
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)
|
rows.append(row)
|
||||||
|
|
||||||
if metadata["recipients"]:
|
if metadata["recipients"]:
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import re
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from cgi import escape
|
from cgi import escape
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from email.header import decode_header, make_header
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.mime.image import MIMEImage
|
from email.mime.image import MIMEImage
|
||||||
@@ -351,21 +352,26 @@ class EMailNotification(BaseNotification):
|
|||||||
"{}: generating notification email for '{}'".format(
|
"{}: generating notification email for '{}'".format(
|
||||||
queueid, recipient))
|
queueid, recipient))
|
||||||
self.logger.debug("{}: parsing email template".format(queueid))
|
self.logger.debug("{}: parsing email template".format(queueid))
|
||||||
if "from" not in headers.keys():
|
# decode some headers
|
||||||
headers["from"] = ""
|
decoded_headers = {}
|
||||||
if "to" not in headers.keys():
|
for var in ["from", "to", "subject"]:
|
||||||
headers["to"] = ""
|
if header in headers:
|
||||||
if "subject" not in headers.keys():
|
decoded_headers[header] = str(
|
||||||
headers["subject"] = ""
|
make_header(decode_header(headers[header])))
|
||||||
|
else:
|
||||||
|
headers[var] = ""
|
||||||
|
decoded_headers[var] = ""
|
||||||
|
|
||||||
# generate dict containing all template variables
|
# generate dict containing all template variables
|
||||||
variables = defaultdict(str,
|
variables = defaultdict(str,
|
||||||
EMAIL_HTML_TEXT=sanitized_text,
|
EMAIL_HTML_TEXT=sanitized_text,
|
||||||
EMAIL_FROM=escape(headers["from"]),
|
EMAIL_FROM=escape(decoded_headers["from"]),
|
||||||
EMAIL_ENVELOPE_FROM=escape(mailfrom),
|
EMAIL_ENVELOPE_FROM=escape(mailfrom),
|
||||||
EMAIL_ENVELOPE_FROM_URL=escape(quote(mailfrom)),
|
EMAIL_ENVELOPE_FROM_URL=escape(quote(mailfrom)),
|
||||||
EMAIL_TO=escape(recipient),
|
EMAIL_TO=escape(decoded_headers["to"]),
|
||||||
EMAIL_TO_URL=escape(quote(recipient)),
|
EMAIL_ENVELOPE_TO=escape(recipient),
|
||||||
EMAIL_SUBJECT=escape(headers["subject"]),
|
EMAIL_ENVELOPE_TO_URL=escape(quote(recipient)),
|
||||||
|
EMAIL_SUBJECT=escape(decoded_headers["subject"]),
|
||||||
EMAIL_QUARANTINE_ID=quarantine_id)
|
EMAIL_QUARANTINE_ID=quarantine_id)
|
||||||
|
|
||||||
if subgroups:
|
if subgroups:
|
||||||
|
|||||||
Reference in New Issue
Block a user