Django's mail, and the Notix API beside it.
Django already has an email system, and the fastest integration keeps it: point the SMTP backend at the relay and send_mail, EmailMessage and the password reset view all go through Notix. Add the notix-sdk client for the sends where a retry must never mean a second email.
Send an email from Django.
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.usenotix.dev"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = "notix"
EMAIL_HOST_PASSWORD = os.environ["NOTIX_API_KEY"]
DEFAULT_FROM_EMAIL = "Acme <receipts@acme.com>"
import os
from notix import Notix
notix = Notix(os.environ["NOTIX_API_KEY"])
def send_receipt(to: str, order_id: int) -> str:
resp, _ = notix.emails.send(
payload={
"from": "Acme <receipts@acme.com>",
"to": to,
"subject": "Your receipt",
"html": "<p>Thanks for your order.</p>",
},
options={"idempotency_key": f"order-{order_id}-receipt"},
)
return resp["emailId"]
The from address must be on a domain you have verified in Notix. Step by step: the Python quickstart and the longer guide to sending email from Django.
Verify delivery events over the raw body.
Notix signs every webhook call with the webhook's signing secret: an HMAC-SHA256 of the X-Notix-Timestamp header, a dot and the raw body, sent in X-Notix-Signature. The SDKs check the signature and refuse a call more than 5 minutes old.
Pass the request body as bytes, before any JSON parsing: the signature covers the exact bytes Notix sent. A bad call raises WebhookVerificationError; answer 400.
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from notix import WebhookVerificationError
webhooks = notix.webhooks(os.environ["NOTIX_WEBHOOK_SECRET"])
@csrf_exempt
@require_POST
def notix_webhook(request):
try:
event = webhooks.construct_event(request.body, headers=request.headers)
except WebhookVerificationError:
return HttpResponseBadRequest("invalid signature")
return HttpResponse("ok")
Questions, answered.
Do Django's password reset emails go through Notix?
What if the same request runs twice?
Why is my send refused with a from-address error?
Send your first email in five minutes.
Verify a domain, copy an API key, make one call. The free plan does not ask for a card.