From 8a06d7b7e87b3f84ea77254536930c2acd412ac8 Mon Sep 17 00:00:00 2001 From: Zichao Lin Date: Wed, 24 Jul 2024 11:55:17 +0800 Subject: [PATCH] fix: support plain text emails --- main.py | 14 +++-- template.html | 149 ++++++++++++++++++++++++++------------------------ 2 files changed, 88 insertions(+), 75 deletions(-) diff --git a/main.py b/main.py index b2e76ae..a757e6b 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ import datetime import logging import socks import socket +import re from email.header import decode_header @@ -20,14 +21,14 @@ def decode_mime_words(s): for fragment, encoding in decoded_fragments ) -def add_mask(original_msg, content): +def add_mask(original_msg, content, is_html): original_subject = decode_mime_words(original_msg['Subject']) from_name, from_address = parseaddr(original_msg['From']) from_name = decode_mime_words(from_name) to_name, to_address = parseaddr(original_msg['To']) to_name = decode_mime_words(to_name) - header = f"""
Forwarded Email

From: {from_name} <{from_address}>

To: {to_name} <{to_address}>

Subject: {original_subject}

""" - footer = f"""
FORWARDED

Notice:

 This is a automatically forwarded email, which means it may contains something bad.

 You shouldn't reply directly to this email, it will never reach your destination!

""" + header = f"""

Forwarded Email

{'' if is_html else '

This email is plain text, it may have display issues

'}

From: {from_name} <{from_address}>

To: {to_name} <{to_address}>

Subject: {original_subject}

""" + footer = f"""
FORWARDED

Notice:

 This is a automatically forwarded email, which means it may contains something bad.

 You shouldn't reply directly to this email, it will never reach your destination!

""" return header + content + footer def load_config(config_file='config.json'): @@ -133,7 +134,12 @@ def forward_emails(account_config, emails, logger): logger.error(f"Failed to extract body from email {email_id}") continue - html_content = add_mask(original_msg, body) + is_html = bool(re.compile(r'<[^>]+>').search(body)) + + if not is_html: + body = body.replace('\n', '
') + + html_content = add_mask(original_msg, body, is_html) msg.attach(MIMEText(html_content, 'html')) for attachment in attachments: diff --git a/template.html b/template.html index c6b276e..a0c4bcb 100644 --- a/template.html +++ b/template.html @@ -1,71 +1,78 @@ - - - - - - - -
- Forwarded Email - -

From: {from_name} <{from_address}>

-

To: {to_name} <{to_address}>

-

Subject: {original_subject}

-
- - - - - - -
- -
- - - - - - - -
- FORWARDED - -

Notice:

-

 This is a automatically forwarded email, which means it may contains something bad.

-

 You shouldn't reply directly to this email, it will never reach your destination!

-
+ + + + + + + + + + +
+

Forwarded Email

+ +
+

From: {from_name} <{from_address}>

+

To: {to_name} <{to_address}>

+

Subject: {original_subject}

+
+ + + + + + +
+ +
+ + + + + + + +
+ FORWARDED + +

+ Notice: +

+

+  This is a automatically forwarded email, which means it may + contains something bad. +

+

+  You shouldn't reply directly to this email, it will never reach + your destination! +

+
+ +