Send email with Django
Send from a view or a signal, with the key in settings and out of the repository.
You need a verified sending domain and an API key from Settings › API Keys. If neither exists yet, the quickstart covers both in a couple of minutes.
Install
pip install sending-sdkSend
from django.conf import settings
from sending import Sending
client = Sending(api_key=settings.SENDING_API_KEY)
def send_welcome(user):
return client.emails.send({
"from": "Acme <[email protected]>",
"to": user.email,
"subject": "Welcome",
"html": "<p>You're in.</p>",
# Stable per user: a retried signal must not send twice.
"idempotencyKey": f"welcome-{user.pk}",
})A successful call answers 202 with the message id: accepted and queued, not yet delivered. Delivery, opens, bounces and complaints arrive later, on webhooks or in the dashboard.
What changes in Django
Call it from transaction.on_commit when the email follows a database write: sending inside the transaction means a rollback still leaves the message delivered.
The thing that catches people
The Django email backend is not involved here, so EMAIL_BACKEND and send_mail() settings have no effect on these calls. Pick one path per project rather than half of each.
Next
- All the fields: cc and bcc, reply-to, attachments, templates, scheduling.
- Webhooks: delivery, bounce and complaint events, signed with HMAC.
- Domains: SPF, DKIM and MAIL FROM, and why sending is refused until they are in place.
- MCP: the same operations as tools, when the one writing the code is an agent.