Send emails with Django
Send email from a Django view by calling the MailBlastr REST API with requests or httpx.
MailBlastr does not yet publish a Python/Django package, but sending from Django is straightforward: call POST /emails from a view (or a background task) with requests or httpx. Read the API key from Django settings, which in turn reads it from the environment — so the secret never lands in source control.
This guide adds a small view that sends an email. Node.js projects can use npm install mailblastr instead — see Send emails with Node.js.
Prerequisites
- A verified domain for your
fromaddress. (guide) - An API key (
mb_...). (Authentication) requestsinstalled (pip install requests) andMAILBLASTR_API_KEYavailable in your environment.
Read the key from settings
Expose the key through settings.py so views import it from one place.
import os
MAILBLASTR_API_KEY = os.environ["MAILBLASTR_API_KEY"]Send from a view
This view sends a welcome email and returns the MailBlastr email id as JSON.
import requests
from django.conf import settings
from django.http import JsonResponse
from django.views.decorators.http import require_POST
MAILBLASTR_URL = "https://www.mailblastr.com/api/emails"
@require_POST
def send_welcome(request):
res = requests.post(
MAILBLASTR_URL,
headers={"Authorization": f"Bearer {settings.MAILBLASTR_API_KEY}"},
json={
"from": "Acme <hello@yourdomain.com>",
"to": ["delivered@example.com"],
"subject": "Welcome to Acme",
"html": "<p>Thanks for signing up.</p>",
},
timeout=10,
)
if not res.ok:
err = res.json()
return JsonResponse({"error": err["message"]}, status=res.status_code)
return JsonResponse({"id": res.json()["id"]})requests.post(...) into a background task (Celery, Django-Q, or django-tasks) so the view returns immediately.Handling the response
On success MailBlastr returns HTTP 200 with { "id": "..." }; on failure a non-2xx status with { statusCode, name, message }. The view above checks res.ok and surfaces a clean error. Persist the returned id if you want to retrieve the email or reconcile webhook events.
MAILBLASTR_API_KEY server-side — in your environment or secrets manager, read via settings. Never expose it to templates, JavaScript, or the client. The key sends mail as you.Next steps
- See the full Send Email API reference.
- Read Authentication — a
sending_accesskey is the safest choice for a sending-only service. - Start from the Quickstart.