From fc205157a8c365044af09663aa8c5d096978015e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Thu, 29 Jun 2023 16:21:22 +0200 Subject: [PATCH] Preserve also contact name in Original-From (#1787) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adrià Casajús --- email_handler.py | 6 ++++- tests/handler/test_preserved_headers.py | 35 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/email_handler.py b/email_handler.py index f28c3205..bce0bfe6 100644 --- a/email_handler.py +++ b/email_handler.py @@ -899,7 +899,11 @@ def forward_email_to_mailbox( msg[headers.SL_EMAIL_LOG_ID] = str(email_log.id) if user.include_header_email_header: msg[headers.SL_ENVELOPE_FROM] = envelope.mail_from - msg[headers.SL_ORIGINAL_FROM] = contact.website_email + if contact.name: + original_from = f"{contact.name} <{contact.website_email}>" + else: + original_from = contact.website_email + msg[headers.SL_ORIGINAL_FROM] = original_from # when an alias isn't in the To: header, there's no way for users to know what alias has received the email msg[headers.SL_ENVELOPE_TO] = alias.email diff --git a/tests/handler/test_preserved_headers.py b/tests/handler/test_preserved_headers.py index f34af50b..b1a820a7 100644 --- a/tests/handler/test_preserved_headers.py +++ b/tests/handler/test_preserved_headers.py @@ -5,6 +5,7 @@ from app.db import Session from app.email import headers, status from app.mail_sender import mail_sender from app.models import Alias +from app.utils import random_string from tests.utils import create_new_user, load_eml_file, random_email @@ -37,3 +38,37 @@ def test_original_headers_from_preserved(): request.msg[headers.AUTHENTICATION_RESULTS] == msg[headers.AUTHENTICATION_RESULTS] ) + + +@mail_sender.store_emails_test_decorator +def test_original_headers_from_with_name_preserved(): + user = create_new_user() + alias = Alias.create_new_random(user) + Session.flush() + assert user.include_header_email_header + original_sender_address = random_email() + name = random_string(10) + msg = load_eml_file( + "replacement_on_forward_phase.eml", + { + "sender_address": f"{name} <{original_sender_address}>", + "recipient_address": alias.email, + "cc_address": random_email(), + }, + ) + envelope = Envelope() + envelope.mail_from = f"env.{original_sender_address}" + envelope.rcpt_tos = [alias.email] + result = email_handler.MailHandler()._handle(envelope, msg) + assert result == status.E200 + send_requests = mail_sender.get_stored_emails() + assert len(send_requests) == 1 + request = send_requests[0] + assert request.msg[headers.SL_ENVELOPE_FROM] == envelope.mail_from + assert ( + request.msg[headers.SL_ORIGINAL_FROM] == f"{name} <{original_sender_address}>" + ) + assert ( + request.msg[headers.AUTHENTICATION_RESULTS] + == msg[headers.AUTHENTICATION_RESULTS] + )