diff --git a/app/.pre-commit-config.yaml b/app/.pre-commit-config.yaml index 60c43ee..2da4462 100644 --- a/app/.pre-commit-config.yaml +++ b/app/.pre-commit-config.yaml @@ -21,3 +21,4 @@ repos: - id: djlint-jinja files: '.*\.html' entry: djlint --reformat + diff --git a/app/app/mail_sender.py b/app/app/mail_sender.py index 2705ddd..dd143d7 100644 --- a/app/app/mail_sender.py +++ b/app/app/mail_sender.py @@ -17,7 +17,7 @@ from attr import dataclass from app import config from app.email import headers from app.log import LOG -from app.message_utils import message_to_bytes +from app.message_utils import message_to_bytes, message_format_base64_parts @dataclass @@ -173,8 +173,12 @@ class MailSender: self._save_request_to_unsent_dir(send_request) return False - def _save_request_to_unsent_dir(self, send_request: SendRequest): - file_name = f"DeliveryFail-{int(time.time())}-{uuid.uuid4()}.{SendRequest.SAVE_EXTENSION}" + def _save_request_to_unsent_dir( + self, send_request: SendRequest, prefix: str = "DeliveryFail" + ): + file_name = ( + f"{prefix}-{int(time.time())}-{uuid.uuid4()}.{SendRequest.SAVE_EXTENSION}" + ) file_path = os.path.join(config.SAVE_UNSENT_DIR, file_name) file_contents = send_request.to_bytes() with open(file_path, "wb") as fd: @@ -256,7 +260,7 @@ def sl_sendmail( send_request = SendRequest( envelope_from, envelope_to, - msg, + message_format_base64_parts(msg), mail_options, rcpt_options, is_forward, diff --git a/app/app/message_utils.py b/app/app/message_utils.py index a5a199b..34f57d4 100644 --- a/app/app/message_utils.py +++ b/app/app/message_utils.py @@ -1,21 +1,42 @@ +import re from email import policy from email.message import Message +from app.email import headers from app.log import LOG +# Spam assassin might flag as spam with a different line length +BASE64_LINELENGTH = 76 + def message_to_bytes(msg: Message) -> bytes: """replace Message.as_bytes() method by trying different policies""" for generator_policy in [None, policy.SMTP, policy.SMTPUTF8]: try: return msg.as_bytes(policy=generator_policy) - except: + except Exception: LOG.w("as_bytes() fails with %s policy", policy, exc_info=True) msg_string = msg.as_string() try: return msg_string.encode() - except: + except Exception: LOG.w("as_string().encode() fails", exc_info=True) return msg_string.encode(errors="replace") + + +def message_format_base64_parts(msg: Message) -> Message: + for part in msg.walk(): + if part.get( + headers.CONTENT_TRANSFER_ENCODING + ) == "base64" and part.get_content_type() in ("text/plain", "text/html"): + # Remove line breaks + body = re.sub("[\r\n]", "", part.get_payload()) + # Split in 80 column lines + chunks = [ + body[i : i + BASE64_LINELENGTH] + for i in range(0, len(body), BASE64_LINELENGTH) + ] + part.set_payload("\r\n".join(chunks)) + return msg diff --git a/app/templates/dashboard/alias_contact_manager.html b/app/templates/dashboard/alias_contact_manager.html index a1fff07..a9b355d 100644 --- a/app/templates/dashboard/alias_contact_manager.html +++ b/app/templates/dashboard/alias_contact_manager.html @@ -50,7 +50,9 @@
This Youtube video can also quickly walk you through the steps: - + How to send emails from an alias
diff --git a/app/templates/dashboard/custom_domain.html b/app/templates/dashboard/custom_domain.html index b967f77..324b291 100644 --- a/app/templates/dashboard/custom_domain.html +++ b/app/templates/dashboard/custom_domain.html @@ -23,7 +23,9 @@ diff --git a/app/templates/dashboard/domain_detail/auto-create.html b/app/templates/dashboard/domain_detail/auto-create.html index 1ad9a08..87f3e66 100644 --- a/app/templates/dashboard/domain_detail/auto-create.html +++ b/app/templates/dashboard/domain_detail/auto-create.html @@ -78,7 +78,7 @@ data-clipboard-text=".*suffix">.*suffixPlease head to our - + documentation to see how to add SIWSL into your app. diff --git a/app/templates/developer/index.html b/app/templates/developer/index.html index 82b1dfd..1381398 100644 --- a/app/templates/developer/index.html +++ b/app/templates/developer/index.html @@ -49,6 +49,7 @@ New website Docs diff --git a/app/templates/discover/index.html b/app/templates/discover/index.html index 55d618b..6dde96b 100644 --- a/app/templates/discover/index.html +++ b/app/templates/discover/index.html @@ -13,7 +13,9 @@