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.
By starting you agree to the Terms and Privacy Policy.
Install the Python SDK
pip, uv or Poetry, into the environment Django runs in. It needs Python 3.9 or newer.
pip install 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.
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 <moc.elpmaxe@olleh>",
"to": request.POST["email"],
"subject": "Your Acme code",
"text": "Your code is 123456.",
"deliver_by": "10m",
}
)
return JsonResponse({"id": sent["id"]})
In the SDK’s tests: run with Django’s test client, CSRF checks on.
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.
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.
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
- 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 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.
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.