AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): suppress transactional emails to preview addresses (#15953)
Toran Bruce Richards0234995587f3
diff --git a/autogpt_platform/backend/backend/notifications/email.py b/autogpt_platform/backend/backend/notifications/email.pyindex 9c03d55634..fcfa3e52fc 100644--- a/autogpt_platform/backend/backend/notifications/email.py+++ b/autogpt_platform/backend/backend/notifications/email.py@@ -116,7 +116,10 @@ class EmailSender:"""Send a one-off transactional email (e.g. Better Auth password-resetand verification links) with no notification templating or preferencegating. Raises if the Postmark client is not configured so callers can- surface the failure instead of silently dropping an auth email."""+ surface the failure instead of silently dropping an auth email.+ Fake preview recipients are intentionally skipped."""+ if _suppress_preview_email(user_email):+ returnif not self.postmark:raise RuntimeError("Postmark is not configured; cannot send email")self.postmark.emails.send(@@ -169,6 +172,8 @@ class EmailSender:attachments: list[dict[str, str]] | None = None,tracked: bool = True,) -> None:+ if _suppress_preview_email(user_email):+ returnif not self.postmark:logger.warning("Email tried to send without Postmark configured")return@@ -191,6 +196,14 @@ class EmailSender:)+def _suppress_preview_email(user_email: str) -> bool:+ """Preview accounts have fake addresses that would damage sender reputation."""+ if not user_email.strip().lower().endswith("@previews.agpt.co"):+ return False+ logger.info("Skipping transactional email to the preview recipient domain")+ return True++def _tracking(enabled: bool) -> dict[str, Any]:"""Postmark's per-message tracking fields. Links are rewritten in theHTML part only: a rewritten link in the plain-text part is a long opaquediff --git a/autogpt_platform/backend/backend/notifications/email_suppression_test.py b/autogpt_platform/backend/backend/notifications/email_suppression_test.pynew file mode 100644index 0000000000..0df4c840e8--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/email_suppression_test.py@@ -0,0 +1,100 @@+from typing import Literal+from unittest.mock import MagicMock++import pytest+from prisma.enums import NotificationType++from backend.data.notifications import VerdictData+from backend.notifications.email import EmailSender+from backend.notifications.renderer import RenderedEmail++SendPath = Literal["auth", "notification", "owner"]+SEND_PATHS: tuple[SendPath, ...] = ("auth", "notification", "owner")+++def postmark() -> MagicMock:+ return MagicMock()+++def sender(postmark: MagicMock) -> EmailSender:+ sender = EmailSender.__new__(EmailSender)+ sender.postmark = postmark+ return sender++[email protected]("path", SEND_PATHS)+ "recipient",+ [+ "[email protected]",+ "[email protected]",+ "[email protected]",+ " [email protected] \t",+ ],+)+async def test_preview_recipients_never_reach_postmark(+ sender: EmailSender, postmark: MagicMock, path: SendPath, recipient: str+):+ await _send(sender, path, recipient)+ postmark.emails.send.assert_not_called()++[email protected]("path", SEND_PATHS)+ "recipient",+ [+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ ],+)+async def test_other_recipients_still_reach_postmark(+ sender: EmailSender, postmark: MagicMock, path: SendPath, recipient: str+):+ await _send(sender, path, recipient)+ postmark.emails.send.assert_called_once()+ assert postmark.emails.send.call_args.kwargs["To"] == recipient++[email protected]("path", SEND_PATHS)+async def test_real_recipient_provider_errors_still_propagate(+ sender: EmailSender, postmark: MagicMock, path: SendPath+):+ postmark.emails.send.side_effect = RuntimeError("Provider unavailable")+ with pytest.raises(RuntimeError, match="Provider unavailable"):+ await _send(sender, path, "[email protected]")+++def test_preview_auth_mail_needs_no_postmark_configuration(sender: EmailSender):+ sender.postmark = None+ sender.send_email_or_raise("[email protected]", "Reset", "<p>Reset</p>")+++async def _send(sender: EmailSender, path: SendPath, recipient: str) -> None:+ if path == "auth":+ sender.send_email_or_raise(recipient, "Reset", "<p>Reset</p>", "Reset")+ elif path == "owner":+ await sender.send_owner_email(+ recipient,+ RenderedEmail(+ subject="Result", preheader="", html="<p>Result</p>", text="Result"+ ),+ attachments=[],+ )+ else:+ await sender.send_notification(+ NotificationType.VERDICT,+ recipient,+ VerdictData(+ outcome="approved",+ agent_name="Example workflow",+ reviewer_name="Reviewer",+ reviewed_at_label="Today",+ ),+ "https://example.com/unsubscribe",+ )