# Send email from Django

A view that sends with the `mailset` package, and a webhook view that Django’s CSRF check would refuse. `@csrf_exempt` lets Mailset’s POST in, and the signature on `request.body` does the guarding.

## Install the Python SDK

pip, uv or Poetry, into the environment Django runs in. It needs Python 3.9 or newer.

pip:

```bash
pip install mailset
```

uv:

```bash
uv add mailset
```

poetry:

```bash
poetry add mailset
```

## Send your first email from Django

The client is made once, when Django imports the module, and reads `MAILSET_KEY`. `send_code` is `@require_POST`, sends to `request.POST["email"]` with `"deliver_by": "10m"`, and answers the receipt’s id in a `JsonResponse`.

```python
from django.http import HttpRequest, JsonResponse
from django.views.decorators.http import require_POST

from mailset import Mailset

# Made once, when Django imports the module.
# Reads MAILSET_KEY from the environment.
mailset = Mailset()


@require_POST
def send_code(request: HttpRequest) -> JsonResponse:
    sent = mailset.send(
        {
            "from": "Acme <hello@example.com>",
            "to": request.POST["email"],
            "subject": "Your Acme code",
            "text": "Your code is 123456.",
            "deliver_by": "10m",
        }
    )
    return JsonResponse({"id": sent["id"]})
```

## Verify webhooks in a csrf_exempt view

`mailset_events` is `@csrf_exempt` and `@require_POST`. It verifies `request.body`, since `request.POST` or a JSON parse of it lose the signed bytes, with `request.headers` and `MAILSET_HOOK_SECRET`. A `MailsetError` answers 401, and `HttpResponse(status=204)` tells Mailset the batch arrived.

```python
import os

from django.http import HttpRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST

from mailset import MailsetError, webhooks

SECRET = os.environ["MAILSET_HOOK_SECRET"]


# Mailset signs instead of sending a CSRF token.
@csrf_exempt
@require_POST
def mailset_events(request: HttpRequest) -> HttpResponse:
    # The signature covers the body as it came: verify
    # request.body. request.POST, or a JSON parse of
    # it, loses the bytes it was made over.
    body, headers = request.body, request.headers
    try:
        events = webhooks.verify(body, headers, SECRET)
    except MailsetError as error:
        return HttpResponse(error.code, status=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 HttpResponse(status=204)
```

In the SDK’s tests: run with Django’s test client, CSRF checks on.

## 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 Django, asked and answered

### Is csrf_exempt safe here?

For this view, yes. Django’s CSRF check wants a token Mailset can’t have, and the signature check takes its place: an unsigned or altered request still gets 401. The SDK’s test runs the view with CSRF checks on.

### Does it replace send_mail and EMAIL_BACKEND?

It doesn’t register an email backend. Call `mailset.send()` in the view or task that would have called `send_mail`, with no SMTP settings in `settings.py`.

### Can an async view send?

Use `AsyncMailset` in an `async def` view and `await mailset.send(...)`. The blocking `Mailset` suits ordinary views.

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