Files
forwarder/tests/test_smtp_forward.py
T
1sonhador b070af8503 fix: forward original email via SMTP instead of .eml attachment
Re-send the received RFC822 message with normal Fwd headers (To, From,
Subject). The previous code wrapped the message in message/rfc822 with
filename original-email.eml, which the relay rejected.
2026-07-01 09:18:11 +01:00

60 lines
1.8 KiB
Python

"""Forward re-sends the original message — no .eml wrapper."""
from __future__ import annotations
from email.message import EmailMessage
from forwarder.email.smtp_client import build_forward_message
def test_forward_resends_original_with_updated_headers():
original = EmailMessage()
original["From"] = "[email protected]"
original["To"] = "[email protected]"
original["Subject"] = "Fatura 123"
original.set_content("corpo original")
original.add_attachment(
b"%PDF-1.4",
maintype="application",
subtype="pdf",
filename="fatura.pdf",
)
fwd = build_forward_message(
original,
from_addr="[email protected]",
destination_email="[email protected]",
)
assert fwd["From"] == "[email protected]"
assert fwd["To"] == "[email protected]"
assert fwd["Subject"] == "Fwd: Fatura 123"
attachments = list(fwd.iter_attachments())
assert len(attachments) == 1
assert attachments[0].get_filename() == "fatura.pdf"
assert attachments[0].get_content_type() == "application/pdf"
assert not any((p.get_filename() or "").endswith(".eml") for p in attachments)
# Body preserved from original.
text_parts = [
p.get_payload(decode=True)
for p in fwd.walk()
if not p.is_multipart() and p.get_content_type() == "text/plain"
]
assert any(b"corpo original" in (part or b"") for part in text_parts)
def test_forward_does_not_double_prefix_subject():
original = EmailMessage()
original["Subject"] = "Fwd: Already forwarded"
original.set_content("x")
fwd = build_forward_message(
original,
from_addr="[email protected]",
destination_email="[email protected]",
)
assert fwd["Subject"] == "Fwd: Already forwarded"