Skip to content

Commit

Permalink
fix(email): Get email payload when email is multipart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Mar 14, 2023
1 parent 880891c commit 02d8044
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
14 changes: 7 additions & 7 deletions email_utils/handle_outside_to_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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":
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions email_utils/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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 = "[email protected]"
Expand All @@ -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] = "[email protected]"
message[headers.TO] = alias.address

response = await handle(
envelope=envelope,
Expand Down

0 comments on commit 02d8044

Please sign in to comment.