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.

Need an API key?

SMTP password is a Unitpost API key with emails:send. Username is always unitpost.

  • 5,000 emails/month
  • 3 API keys
  • 5 sending domains
Get free SMTP key
  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.

Search docs and guides

Search the docs and product guides.