# 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](https://www.mailblastr.com/docs/integrations/nodejs).

## Prerequisites

1. A **verified domain** for your `from` address. ([guide](https://www.mailblastr.com/docs/domains/managing))
2. An **API key** (`mb_...`). ([Authentication](https://www.mailblastr.com/docs/authentication))
3. `requests` installed (`pip install requests`) and `MAILBLASTR_API_KEY` available in your environment.

## Read the key from settings

Expose the key through `settings.py` so views import it from one place.

**settings.py**

```python
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.

**views.py**

```python
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"]})
```

> **Note:** Network calls block the request/response cycle. For high throughput, move the `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](https://www.mailblastr.com/docs/api/emails-get) or reconcile [webhook](https://www.mailblastr.com/docs/webhooks/overview) events.

> **Warning:** Keep `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](https://www.mailblastr.com/docs/api/emails-send) reference.
- Read [Authentication](https://www.mailblastr.com/docs/authentication) — a `sending_access` key is the safest choice for a sending-only service.
- Start from the [Quickstart](https://www.mailblastr.com/docs/quickstart).
