"""A tiny SMTP server that hands Sentry's email to exe.dev's email gateway. Sentry speaks SMTP; exe.dev VMs send mail with an HTTP call (https://exe.dev/docs/send-email). This listens on port 2525 inside Sentry's Compose network (it is not published) and sends each message, as plain text, to each recipient through the gateway. The gateway delivers only to the VM's owner, their exe.dev team, and people the VM is shared with who have signed in to it, which is everyone who can reach the dashboard. Mail to anyone else is dropped, with the gateway's reason in the log: docker compose logs exe-mailer """ import asyncio import email import email.policy import json import threading import urllib.error import urllib.request from aiosmtpd.controller import Controller GATEWAY = "http://169.254.169.254/gateway/email/send" def send(to, subject, body): req = urllib.request.Request( GATEWAY, data=json.dumps({"to": to, "subject": subject, "body": body}).encode(), headers={"Content-Type": "application/json"}, ) with urllib.request.urlopen(req, timeout=30) as r: r.read() class Relay: async def handle_DATA(self, server, session, envelope): msg = email.message_from_bytes(envelope.original_content, policy=email.policy.default) subject = str(msg.get("Subject", "")) or "(no subject)" part = msg.get_body(preferencelist=("plain", "html")) body = part.get_content() if part else "" for rcpt in envelope.rcpt_tos: try: await asyncio.to_thread(send, rcpt, subject, body) print(f"sent to {rcpt}: {subject}", flush=True) except urllib.error.HTTPError as e: print(f"gateway refused {rcpt}: {e.code} {e.read().decode(errors='replace')[:300]}", flush=True) except OSError as e: print(f"gateway unreachable for {rcpt}: {e}", flush=True) # Accept the message either way: a refused address must not turn a # sign-up or an invitation into a server error. The log says why. return "250 OK" Controller(Relay(), hostname="0.0.0.0", port=2525).start() print("mail relay listening on :2525", flush=True) threading.Event().wait()