Send email from Python

Send sign-in codes, password resets, receipts and alerts from Python with TrivSend's SDK, or through SMTP from Django with no package at all. Every email is signed for your own domain and logged with its delivery result.

Start free · Python in the docs

Install and send

bash
pip install trivsend
export TRIVSEND_API_KEY=ts_...
python
from trivsend import TrivSend, TrivSendError

client = TrivSend()  # reads TRIVSEND_API_KEY

try:
    email = client.emails.send(
        {
            "from": "Acme <noreply@notify.example.com>",
            "to": "asha@example.com",
            "subject": "Welcome to Acme",
            "html": "<p>Your account is ready.</p>",
            "text": "Your account is ready.",
        },
        idempotency_key="welcome-42",
    )
    print(email["id"])
except TrivSendError as e:
    print(e.name, e.message, e.status_code)

The SDK has no dependencies and runs on Python 3.9 and later. It uses the API's own field names (reply_to, scheduled_at, expires_at), so the API reference applies as written. It works the same in Flask, FastAPI, Celery tasks and plain scripts.

Django: SMTP in settings.py

Django's built-in SMTP backend works as is. send_mail(), password-reset emails and admin error emails all go through TrivSend:

python
import os

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.trivsend.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = "trivsend"
EMAIL_HOST_PASSWORD = os.environ["TRIVSEND_API_KEY"]
DEFAULT_FROM_EMAIL = "Acme <noreply@notify.example.com>"
SERVER_EMAIL = DEFAULT_FROM_EMAIL

Test it from python manage.py shell:

python
from django.core.mail import send_mail
send_mail("TrivSend test", "It works", None, ["you@example.com"])

Use the SDK alongside SMTP when you need what SMTP can't do: templates, expiry for one-time codes, or checking an email before you send it.

Know what happened to each email

TrivSend calls your app when an email bounces, is marked as spam or fails. Verify the signature with the SDK, using the raw body before any JSON parsing:

python
import os
from trivsend import verify_webhook

# Flask
@app.post("/hooks/trivsend")
def trivsend_hook():
    event = verify_webhook(request.get_data(), request.headers, os.environ["TRIVSEND_WEBHOOK_SECRET"])
    if event["type"] == "email.bounced":
        ...  # mark the address as bad in your database
    return "", 204

verify_webhook raises ValueError when the signature doesn't match or the timestamp is more than 5 minutes old (webhooks).

Built for app email

Also see: SMTP relay · Transactional email · Email API for India