Send email from Django
The Python SDK includes a Django email backend, so send_mail, EmailMessage
and Django’s built-in emails (such as password resets) all go through Vaartix. It works with Django
4.2 and later. The source is on GitHub at
vaartix-mail/vaartix-python and the package is on
PyPI.
-
Install the SDK with the Django extra
Terminal window pip install "vaartix[django]" -
Set the backend
settings.py import osVAARTIX_API_KEY = os.environ["VAARTIX_API_KEY"]# Django 6.1 and newerMAILERS = {"default": {"BACKEND": "vaartix.django.EmailBackend"}}# Django 4.2 to 6.0EMAIL_BACKEND = "vaartix.django.EmailBackend"Use the setting that matches your Django version. With
MAILERSyou can pass the key as an option instead:{"BACKEND": "vaartix.django.EmailBackend", "OPTIONS": {"api_key": "vx_live_..."}}. -
Send as usual
orders/emails.py from django.core.mail import EmailMultiAlternativesmessage.attach_alternative("<p>Thanks!</p>", "text/html")message.tags = [{"name": "type", "value": "order"}] # optional, shown in the dashboardmessage.send()print(message.vaartix_id) # em_...HTML alternatives, attachments, inline images (
Content-ID), cc, bcc, reply-to and custom headers are supported. Errors raiseVaartixErrorunless you passfail_silently=True.
Until your domain is verified, send from @test.vaartix.com to [email protected]: see
Test mode.
Receiving webhooks
Section titled “Receiving webhooks”Verify the signature against the raw request body:
import os
from django.http import HttpResponsefrom django.views.decorators.csrf import csrf_exemptfrom django.views.decorators.http import require_POSTfrom vaartix import WebhookVerificationError, verify_webhook
@csrf_exempt@require_POSTdef vaartix_webhook(request): try: event = verify_webhook(request.body, request.headers, os.environ["VAARTIX_WEBHOOK_SECRET"]) except WebhookVerificationError: return HttpResponse(status=400) if event["type"] in ("email.bounced", "email.complained"): ... # stop emailing event["data"]["recipient"] return HttpResponse(status=200)