FastAPI

Send email from FastAPI

Send from your endpoints with the mailset package, and take Mailset’s delivery events on a route of your own. A Pydantic body would break the signature check, so the hook reads await request.body().

Open Mailset
Continue with Google

By starting you agree to the Terms and Privacy Policy.

Install

Install the Python SDK

The Python SDK, from pip, uv or Poetry. It brings httpx, the same HTTP client FastAPI’s TestClient is built on.

pip install mailset
Send

Send your first email from FastAPI

This is Mailset, the blocking client: in a plain def endpoint FastAPI runs it in its thread pool. In an async def endpoint, use AsyncMailset and await mailset.send(...). "deliver_by": "10m" and sent["id"] work the same way in both.

send.py
from mailset import Mailset

# Reads MAILSET_KEY from the environment.
mailset = Mailset()

sent = mailset.send(
    {
        "from": "Acme <moc.elpmaxe@olleh>",
        "to": "gro.elpmaxe@ana",
        "subject": "Your Acme code",
        "text": "Your code is 123456.",
        "html": "<p>Your code is <b>123456</b>.</p>",
        "deliver_by": "10m",
    }
)

print(sent["id"], sent["status"])

In the SDK’s tests: run with FastAPI’s TestClient.

Webhooks

Verify webhooks in FastAPI

mailset_events takes the raw Request and reads await request.body(), then hands the bytes and request.headers to webhooks.verify with MAILSET_HOOK_SECRET. A failed check raises MailsetError, answered 401. Each event is a dict, so a hard bounce is event["bounce"]["kind"] == "hard", and 204 marks the batch kept.

main.py
# A FastAPI endpoint for your hook.
import os

from fastapi import FastAPI, Request, Response

from mailset import MailsetError, webhooks

app = FastAPI()
SECRET = os.environ["MAILSET_HOOK_SECRET"]


@app.post("/mailset/events")
async def mailset_events(request: Request) -> Response:
    # The signature covers the body as it came: read
    # its bytes. A pydantic body or request.json()
    # loses what it was made over.
    body = await request.body()
    headers = request.headers

In the SDK’s tests: run with FastAPI’s TestClient.

Domain

Send from your own domain

Your product sends as your own domain either way. What changes is where your team’s mail lives.

  • Sending only. Your team’s mail stays at Google, Microsoft or wherever it is now. Mailset asks for DKIM, a bounce host and DMARC, and leaves your MX alone. Send from a domain whose mail stays elsewhere
  • Mailboxes too. Your team’s mail moves to Mailset, with a real mailbox for every address on the domain and your product’s email on the same bill. Set up a domain
mailset domains add example.com --sendingmailset domains apply example.com --sendingmailset domains check example.commailset keys create --send --domain example.com --from moc.elpmaxe@olleh --name "Acme API"
Facts

What the Python SDK’s build checks

Each line comes from the SDK’s own build, which fails when one stops being true.

FactDetailKnown from
Python 3.9+The oldest its manifest allowsIts manifest
Typed throughoutTypedDicts throughout, checked by mypyA test
Sync and asyncMailset and AsyncMailsetA test
Next

Where to go from here

Pricing

Product email on every plan

  • Base$29a month10 members included
  • Team$79a month25 members included
  • BaseRegular price $29$14.50a month10 members included
  • TeamRegular price $79$39.50a month25 members included

Black Friday prices, for trials started by Thursday, Dec 31. Offer terms

Both include 10,000 emails a month, your product’s and your team’s together. Past that it’s $0.40 per 1,000 with pay as you go.

Every price, and what teams pay elsewhere

Questions

Sending from FastAPI, asked and answered

Why not a Pydantic model for the webhook?

FastAPI would parse the JSON before your code runs, and the signature covers the bytes as sent. Take Request, read the body, and let webhooks.verify return the events.

Do I need fastapi-mail?

No. fastapi-mail sends through an SMTP server; this package sends through Mailset’s HTTPS API with your key, so ConnectionConfig and its SMTP settings go away.

Can the hook answer first and work later?

Yes, and it should when the work is slow: Mailset gives each post 15 seconds. Keep the events, answer 204, then process them, in a BackgroundTasks task for instance.

Make a key and sendTry it on your own domain

Make a key in Settings › Developers, keep it as MAILSET_KEY, and run the example above.7 days free, with everything on. Make a key, run the example above and watch the receipt come back.

Open Mailset
Continue with Google

By starting you agree to the Terms and Privacy Policy.