# 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.

## 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:

```bash
pip install mailset
```

uv:

```bash
uv add mailset
```

poetry:

```bash
poetry add 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.

```python
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 <hello@example.com>",
            "to": request.form["email"],
            "subject": "Your Acme code",
            "text": "Your code is 123456.",
            "deliver_by": "10m",
        }
    )
    return {"id": sent["id"]}
```

## 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.

```python
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:
        events = webhooks.verify(body, headers, SECRET)
    except MailsetError as error:
        return error.code, 401
    for event in events:
        if event["type"] != "bounced":
            continue
        if event["bounce"]["kind"] == "hard":
            print("Stop writing to", event["recipient"])
    # 2xx once the events are kept.
    return "", 204
```

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](https://mailset.app/docs/recipes/sending-only)
- **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](https://mailset.app/guide/onboarding)

```bash
mailset domains add example.com --sending
mailset domains apply example.com --sending
mailset domains check example.com
mailset keys create --send --domain example.com --from hello@example.com --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.

- **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 SDK](https://mailset.app/docs/sdk/python): Every task, from the client’s options to errors and retries.
- [Send email from Python](https://mailset.app/send-email/python): The Python guide, with no framework around it.
- [Webhooks](https://mailset.app/docs/webhooks): The events Mailset posts about your product’s email, and how to check a request’s signature.
- [Give sign-in codes a deadline](https://mailset.app/docs/recipes/deadlines): Use deliverBy so a code that would arrive late is never sent.
- [Retry without sending twice](https://mailset.app/docs/recipes/retries): Make a send safe to repeat with an idempotency key or a Message-ID.
- [Handle bounces](https://mailset.app/docs/recipes/bounces): Tell a dead address from a full inbox, and keep your list clean.
- [Send many messages at once](https://mailset.app/docs/recipes/batches): Up to 100 messages a request, each with its own answer.

## Product email on every plan

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 plan and price: https://mailset.app/pricing

## 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.

## Try it on your own domain

7 days free, with everything on. Make a key, run the example above and watch the receipt come back. Start at https://mailset.app/start

---

© 2026 Intenseloop, Inc. Mailset’s site, docs and guides are copyrighted. You may read them and use their code to build with Mailset. Don’t republish them or use them to build a copy of Mailset. Terms: https://mailset.app/terms#content