Send email from FastAPI over SMTP

Call Python's SMTP client from a FastAPI route. Same host, username, and API-key password as every other stack.

What are the SMTP settings?

Hostsmtp.unitpost.com
Usernameunitpost(always this literal value)
PasswordYour Unitpost API key(must carry emails:send)
Port (STARTTLS)587(or 2587 if blocked)
Port (implicit TLS)465(or 2465 if blocked)
From addressAny address on a verified domain

How do I set it up?

  1. Send from a route

    Python
    import os
    import smtplib
    from email.message import EmailMessage
    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.post("/send")
    def send() -> dict[str, bool]:
        msg = EmailMessage()
        msg["From"] = "you@yourdomain.com"
        msg["To"] = "customer@example.com"
        msg["Subject"] = "Hello from FastAPI"
        msg.set_content("Sent via SMTP.")
        with smtplib.SMTP("smtp.unitpost.com", 587) as smtp:
            smtp.starttls()
            smtp.login("unitpost", os.environ["UNITPOST_API_KEY"])
            smtp.send_message(msg)
        return {"ok": True}
  2. Verify delivery

    Confirm in Activity.