From 02d80445ea82f3647b95bd99ef621c9918c65e6b Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Tue, 14 Mar 2023 15:18:31 +0100 Subject: [PATCH] fix(email): Get email payload when email is multipart --- email_utils/handle_outside_to_local.py | 14 +++++++------- email_utils/handler.py | 1 + tests/test_statistics.py | 6 +++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/email_utils/handle_outside_to_local.py b/email_utils/handle_outside_to_local.py index b4e8658..bbc1be5 100644 --- a/email_utils/handle_outside_to_local.py +++ b/email_utils/handle_outside_to_local.py @@ -50,9 +50,11 @@ def handle_outside_to_local( message_id=message[headers.MESSAGE_ID], ) - content = message.get_payload(decode=True) + content = message.get_payload(decode=True) or message.get_payload() + logger.info("Parsing content.") if type(content) is str: + logger.info("Found 1 content.") content_type = message.get_content_type() if content_type == "text/html": @@ -72,6 +74,8 @@ def handle_outside_to_local( message.set_payload(content, "utf-8") elif type(content) is list: + logger.info(f"Found {len(content)} contents.") + for part in message.walk(): match part.get_content_type(): case "text/html": @@ -91,19 +95,15 @@ def handle_outside_to_local( part.set_payload(content, "utf-8") + logger.info("Parsing content done.") if alias.create_mail_report and alias.user.public_key is not None: + logger.info("Creating mail report.") create_email_report( db, report_data=report, user=alias.user, ) - logger.info( - f"Email {envelope.mail_from} is from outside and wants to send to alias " - f"{alias.address}. " - f"Relaying email to locally saved user {alias.user.email.address}." - ) - set_header( message, headers.KLECK_FORWARD_STATUS, diff --git a/email_utils/handler.py b/email_utils/handler.py index e152dc9..f25f399 100644 --- a/email_utils/handler.py +++ b/email_utils/handler.py @@ -182,6 +182,7 @@ async def handle(envelope: Envelope, message: Message) -> str: except NoResultFound: pass else: + # OUTSIDE user wants to send a mail TO an alias handle_outside_to_local( db, alias=alias, diff --git a/tests/test_statistics.py b/tests/test_statistics.py index 0cd297a..5793b1c 100644 --- a/tests/test_statistics.py +++ b/tests/test_statistics.py @@ -6,6 +6,7 @@ from app import constants from app.constants import ROOT_DIR +from email_utils import headers from email_utils.handler import handle @@ -18,7 +19,7 @@ async def test_can_create_statistics( ): previous_statistics = client.get("/v1/server/statistics").json() user = create_user(is_verified=True) - alias = create_random_alias(user=user) + alias = create_random_alias(user=user, pref_remove_trackers=True) envelope = Envelope() envelope.mail_from = "outside@example.com" @@ -27,6 +28,9 @@ async def test_can_create_statistics( html = (ROOT_DIR / "explorative_tests" / "image_tracker_url.html").read_text() message = MIMEMultipart("alternative") message.attach(MIMEText(html, "html")) + message[headers.SUBJECT] = "Test" + message[headers.FROM] = "outside@example.com" + message[headers.TO] = alias.address response = await handle( envelope=envelope,