Send email from Flask
Two routes: one sends a code from a form post, the other takes Mailset’s events. The second verifies request.get_data(), because request.json hands over parsed data instead of the signed bytes.
By starting you agree to the Terms and Privacy Policy.
Install the Python SDK
pip, uv or Poetry, into the environment your Flask app runs in. No Flask extension is involved: the SDK is a plain client.
pip install mailset
Send your first email from Flask
The module makes its Mailset() at import, and it reads MAILSET_KEY. The /codes route sends to request.form["email"] with "deliver_by": "10m", and Flask turns the returned dict, the receipt’s id, into JSON.
from flask import Flask, request
from mailset import Mailset
app = Flask(__name__)
# Made once, at import. Reads MAILSET_KEY.
mailset = Mailset()
@app.post("/codes")
def send_code() -> dict[str, str]:
sent = mailset.send(
{
"from": "Acme <moc.elpmaxe@olleh>",
"to": request.form["email"],
"subject": "Your Acme code",
"text": "Your code is 123456.",
"deliver_by": "10m",
}
)
return {"id": sent["id"]}
In the SDK’s tests: run with Flask’s test client.
Verify webhooks with get_data()
mailset_events reads request.get_data() and hands it, with request.headers and MAILSET_HOOK_SECRET, to webhooks.verify. A MailsetError answers 401 with its code; each event is a dict, and returning "", 204 confirms the batch.
import os
from flask import Flask, request
from mailset import MailsetError, webhooks
app = Flask(__name__)
SECRET = os.environ["MAILSET_HOOK_SECRET"]
@app.post("/mailset/events")
def mailset_events() -> tuple[str, int]:
# The signature covers the body as it came: verify
# request.get_data(). request.json loses the bytes
# it was made over.
body = request.get_data()
headers = request.headers
try:
In the SDK’s tests: run with Flask’s test client.
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 Flask, asked and answered
Do I need Flask-Mail?
No. Flask-Mail sends through an SMTP server you configure; mailset.send() posts to Mailset’s HTTPS API with your key, so the MAIL_SERVER settings go away.
Why get_data() and not request.json?
request.json is the body already parsed. request.get_data() returns the bytes exactly as they came, which is what the signature is over.
Does CSRFProtect block the hook?
Flask has no CSRF check of its own. With Flask-WTF’s CSRFProtect, exempt the hook with @csrf.exempt; the signature guards it instead.
Is the module’s client safe under threads?
Yes. Mailset() is safe to share between threads, so a threaded server can use the one client from every request.
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.