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().
By starting you agree to the Terms and Privacy Policy.
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 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.
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.
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.
# 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.
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"What the Python SDK’s build checks
Each line comes from the SDK’s own build, which fails when one stops being true.
| Fact | Detail | Known from |
|---|---|---|
| Python 3.9+ | The oldest its manifest allows | Its manifest |
| Typed throughout | TypedDicts throughout, checked by mypy | A test |
| Sync and async | Mailset and AsyncMailset | A test |
Where to go from here
- The Python SDKEvery task, from the client’s options to errors and retries.
- Send email from PythonThe Python guide, with no framework around it.
- WebhooksThe events Mailset posts about your product’s email, and how to check a request’s signature.
- Give sign-in codes a deadlineUse deliverBy so a code that would arrive late is never sent.
- Retry without sending twiceMake a send safe to repeat with an idempotency key or a Message-ID.
- Handle bouncesTell a dead address from a full inbox, and keep your list clean.
- Send many messages at onceUp to 100 messages a request, each with its own answer.
Product email on every plan
- Base$29a month10 members included
- Team$79a month25 members included
- Base
Regular price $29$14.50a month10 members included - Team
Regular 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.
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.
By starting you agree to the Terms and Privacy Policy.