Sign in to fill these examples with your own keys. Sign in Start free trial

Send emails with Django

Use the PostShiba EmailBackend and django.core.mail.

What you'll build

A Django email backend that sends through the PostShiba API. send_mail, EmailMessage, and EmailMultiAlternatives all use it once it is set.

Before you start

  • An API key with Send only access. Full access also works. See Create an API key. Support keys cannot send.
  • A verified sending domain. See Add and verify a domain. from_email must be an address on it.
  • A cluster that is ready to send (sending_ready is true). See Clusters.
  • Python 3.8 or later.

Install

The package is on GitHub, not PyPI. Install it with the django extra.

install.sh
1pip install "postshiba[django] @ git+https://github.com/postshiba/postshiba-python.git"

Send your first email

  1. Set the backend and your API key in settings.py.
settings.py
1EMAIL_BACKEND = "postshiba.django.EmailBackend" 2POSTSHIBA_API_KEY = "YOUR_API_KEY"
  1. Build a message with text and HTML, then call send().
send.py
1from django.core.mail import EmailMultiAlternatives 2 3message = EmailMultiAlternatives( 4 subject="Your receipt", 5 body="Thanks for your order.", 6 from_email="hello@mail.example.com", 7 to=["you@example.com"], 8 reply_to=["support@example.com"], 9 headers={"X-Capsule-Unique-Args": '{"order_id": "ord_123"}'}, 10) 11message.attach_alternative("<p>Thanks for your order.</p>", "text/html") 12message.send()

Try it in python manage.py shell. send() returns 1 when the message is queued. The message then shows under Events in the dashboard.

The backend sends X-Capsule-Unique-Args as unique_args and other custom headers as headers. It always uses your first cluster that is ready to send.

Attachments and templates

Attach a file

Use Django's attach or attach_file. The backend base64-encodes each file for the API. Up to 10 files, 18 MB in total. See Attachments for allowed types.

attachment.py
1from django.core.mail import EmailMessage 2 3message = EmailMessage( 4 subject="Your invoice", 5 body="Your invoice is attached.", 6 from_email="hello@mail.example.com", 7 to=["you@example.com"], 8) 9message.attach_file("invoice.pdf") 10message.send()

Django templates work as usual for the body. Hosted PostShiba templates go through the Python client with template, not through the backend.

Keep the key safe

Use a Send only key in your app. It can send email and call GET /api/v1/users/me. Every other request returns 403 forbidden.

If this key leaks Send only Full access
Send mail from your verified domains Yes Yes
Read events, recipients, templates, and sent messages No Yes
See SMTP passwords and webhook secrets No Yes
Change clusters, domains, credentials, webhooks, and suppressions No Yes
  • Keep Full access keys for setup scripts, the CLI, and MCP. Don't deploy one with your app.
  • Read the key from an environment variable in settings.py, such as POSTSHIBA_API_KEY = os.environ["POSTSHIBA_API_KEY"]. Don't commit it, log it, or put it in a URL.
  • Send from your server. Never put a key, even a Send only key, in a template, browser code, or mobile code.
  • Create one key per app and environment. If a key leaks, delete it on API keys and create a new one. Deleting stops it straight away.

Troubleshooting

A failed send raises postshiba.Error with error, field, and message, unless you pass fail_silently=True.

Status error What to do
401 Invalid token or no user signed in The API key is missing, mistyped, or deleted. Check POSTSHIBA_API_KEY.
403 forbidden Support keys cannot send. Use a Send only or Full access key. A Send only key gets this on any call other than sending.
403 domain_unverified from_email is not on a verified domain.
403 kyc_required Your team is pending production approval. Until then, send only to team members, live inbox addresses, and inbox forward-to addresses.
422 invalid field names the problem, such as a missing to or a rejected attachment.
429 throttled The cluster hit its hourly throttle or 24-hour cap. Retrying right away hits the same cap. See What happens at your cap.
429 rate_limited More than 300 send requests in one minute on one API key. Slow down.

Every other status is on Errors.

Next steps