add option to generate html from plain body part when adding disclaimer

This commit is contained in:
2022-01-04 14:44:16 +01:00
parent c78ab55e98
commit fc6af6eed0
2 changed files with 27 additions and 8 deletions

View File

@@ -170,7 +170,8 @@ class AddDisclaimerConfig(BaseConfig):
"action": {"type": "string"}, "action": {"type": "string"},
"html_template": {"type": "string"}, "html_template": {"type": "string"},
"text_template": {"type": "string"}, "text_template": {"type": "string"},
"error_policy": {"type": "string", "default": "wrap"}}} "error_policy": {"type": "string", "default": "wrap"},
"add_html_body": {"type": "boolean", "default": False}}}
class RewriteLinksConfig(BaseConfig): class RewriteLinksConfig(BaseConfig):

View File

@@ -183,7 +183,7 @@ def _has_content_before_body_tag(soup):
return False return False
def _inject_body(milter): def _inject_body(milter, text_content="", html_content=None):
if not milter.msg.is_multipart(): if not milter.msg.is_multipart():
milter.msg.make_mixed() milter.msg.make_mixed()
@@ -194,8 +194,9 @@ def _inject_body(milter):
attachments.append(attachment) attachments.append(attachment)
milter.msg.clear_content() milter.msg.clear_content()
milter.msg.set_content("") milter.msg.set_content(text_content)
milter.msg.add_alternative("", subtype="html") if html_content is not None:
milter.msg.add_alternative(html_content, subtype="html")
milter.msg.make_mixed() milter.msg.make_mixed()
for attachment in attachments: for attachment in attachments:
@@ -225,7 +226,7 @@ class AddDisclaimer:
_headersonly = False _headersonly = False
def __init__(self, text_template, html_template, action, error_policy, def __init__(self, text_template, html_template, action, error_policy,
pretend=False): add_html_body, pretend=False):
self.text_template_path = text_template self.text_template_path = text_template
self.html_template_path = html_template self.html_template_path = html_template
try: try:
@@ -243,6 +244,7 @@ class AddDisclaimer:
self.error_policy = error_policy.lower() self.error_policy = error_policy.lower()
assert self.error_policy in ["ignore", "reject", "wrap"], \ assert self.error_policy in ["ignore", "reject", "wrap"], \
f"invalid error_policy '{error_policy}'" f"invalid error_policy '{error_policy}'"
self.add_html_body = add_html_body
self.pretend = pretend self.pretend = pretend
def patch_message_body(self, milter, logger): def patch_message_body(self, milter, logger):
@@ -250,9 +252,25 @@ class AddDisclaimer:
html_body, html_content = _get_body_content(milter.msg, "html") html_body, html_content = _get_body_content(milter.msg, "html")
if text_content is None and html_content is None: if text_content is None and html_content is None:
logger.info("message does not contain any body part, " logger.info("message contains only attachment(s), "
"inject empty plain and html body") "inject empty body")
_inject_body(milter) if self.add_html_body:
_inject_body(milter, "", "")
html_body, html_content = _get_body_content(milter.msg, "html")
else:
_inject_body(milter, "")
text_body, text_content = _get_body_content(milter.msg, "plain")
if html_content is None and self.add_html_body:
logger.info("inject html body part generated from plain body")
header = '<meta http-equiv="Content-Type" content="text/html; ' \
'charset=utf-8">'
html_text = re.sub(r"^(.*)$", r"\1<br/>",
escape(text_content, quote=False),
flags=re.MULTILINE)
_inject_body(milter, text_content, f"{header}{html_text}")
text_body, text_content = _get_body_content(milter.msg, "plain")
html_body, html_content = _get_body_content(milter.msg, "html")
variables = defaultdict(str, milter.msginfo["vars"]) variables = defaultdict(str, milter.msginfo["vars"])
variables["ENVELOPE_FROM"] = escape( variables["ENVELOPE_FROM"] = escape(