Skip to content

Documentation

Send email in Python.

Works with: Python 3.9 or newer. No dependencies.

Install

Get a key under API keys in the dashboard and verify a sending domain first (how). Then add the library:

Terminal
pip install mailtruss

Send an email

This sends one message and passes an idempotency key, so repeating the request can never deliver it twice. A send is retried only when you pass an idempotency_key; reads and deletes retry on 429 and 5xx. from_ is sent as from.

send.py
from mailtruss import Mailtruss
​
m = Mailtruss(api_key="ms_live_…") # or set MAILTRUSS_API_KEY
m.emails.send(
from_="Ada <ada@yourdomain.com>", to="you@example.com", subject="Hello",
html="<p>It works.</p>", idempotency_key="order-42",
)

More on what you can put in a message (cc, bcc, attachments, tags, templates, scheduling) is under Sending.

Verify webhook signatures

Mailtruss signs every webhook with the Standard Webhooks scheme. Check the signature before you trust a request. Pass the raw body exactly as received, the request headers and the endpoint's signing secret. It raises ValueError when a header is missing, the timestamp is more than five minutes off, or no signature matches.

webhook.py
import os
from mailtruss import verify_webhook
​
def webhook(request): # Flask-style handler
try:
event = verify_webhook(request.get_data(), request.headers, os.environ["MAILTRUSS_WEBHOOK_SECRET"])
except ValueError:
return "invalid signature", 400
return "ok"

Events, payloads and retries are on the Webhooks page.

Handle errors

Any non-2xx response raises MailtrussError with status, name, message and request_id; a timeout raises MailtrussTimeout. Every error name, with its cause and fix, is in the error catalogue.

Other languages

Last updated

Something wrong or unclear on this page?

Report an issue