Send email with Django
A server-side call to the SendSend API from your Django app.
- Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
- Set SENDSEND_API_KEY in the environment and read it in settings.py with os.environ.
- Add this to
mail/views.py:
import json, uuid, requests
from django.conf import settings
from django.http import JsonResponse
def send(request):
to = json.loads(request.body)["to"]
r = requests.post(
"https://api.sendsend.email/v1/emails",
headers={
"Authorization": f"Bearer {settings.SENDSEND_API_KEY}",
"Idempotency-Key": str(uuid.uuid4()),
},
json={"from": "Acme <hello@mail.acme.com>", "to": [to], "subject": "Hello from Django", "html": "<p>It works!</p>"},
timeout=15,
)
return JsonResponse(r.json(), status=r.status_code)Good to know
- A
202response means the message was queued, not delivered. See message states and webhooks. - Always send an Idempotency-Key, so a retry cannot send twice.
- Check the response status. Errors have one shape: see Errors.
- More options: attachments, scheduling, headers, templates and React Email.