Quickstarts
Send with Django
Send email from Django with the usend SDK, or through SMTP and send_mail.
You need a verified domain and an API key. Django can use usend two ways: the SDK below, or SMTP so send_mail() keeps working (Django with SMTP).
1. Install and configure
pip install usendimport os
USEND_API_KEY = os.environ["USEND_API_KEY"]2. A helper
from django.conf import settings
from usend import Usend
_client = Usend(settings.USEND_API_KEY)
def send_email(to, subject, html, *, idempotency_key=None):
"""Returns the usend email id. Raises usend.UsendError on failure."""
result = _client.emails.send(
{
"from": "Acme <[email protected]>",
"to": [to] if isinstance(to, str) else list(to),
"subject": subject,
"html": html,
},
idempotency_key=idempotency_key,
)
return result["id"]3. Use it in a view
from django.http import JsonResponse
from django.template.loader import render_to_string
from django.views.decorators.http import require_POST
from usend import UsendError
from .mail import send_email
@require_POST
def invite(request):
email = request.POST["email"]
html = render_to_string("emails/invite.html", {"inviter": request.user})
try:
email_id = send_email(email, "You are invited", html, idempotency_key=f"invite-{email}")
except UsendError as e:
return JsonResponse({"error": str(e)}, status=502)
return JsonResponse({"id": email_id})Run slow or bulk sends from a task queue (Celery, Django-Q, RQ) rather than the request.
Webhooks
from usend import verify_webhook
def usend_webhook(request):
event = verify_webhook(request.body.decode(), request.headers, settings.USEND_WEBHOOK_SECRET)Exempt the view from CSRF (@csrf_exempt); the signature is its protection. See Verify signatures.