How to Send Transactional Emails with Python & FastAPI
Fast, asynchronous email delivery for Django, Flask, FastAPI, and Celery background workers using Mailofly's developer-first API.
pip install mailoflyGet Up and Running in 3 Easy Steps
Install via pip or poetry
Run `pip install mailofly` in your virtual environment. Includes type stubs for mypy and Pyright.
Initialize with your environment key
Load your API key from `os.environ` and instantiate `client = Mailofly(api_key=...)`.
Send synchronously or with async workers
Call `client.emails.send()` inside FastAPI endpoints, Django views, or Celery background task queues.
Production-Ready Code Example
Copy and paste into your project. Fully typed and tested.
import os
from mailofly import Mailofly
client = Mailofly(api_key=os.environ.get("MAILOFLY_API_KEY"))
response = client.emails.send(
sender="Security <security@yourdomain.com>",
to=["developer@example.com"],
subject="Your Two-Factor Authentication Code",
html="<p>Your 2FA security code is: <strong>849-201</strong></p><p>Expires in 10 minutes.</p>",
tags=[{"name": "type", "value": "2fa"}],
)
print(f"Email dispatched successfully! ID: {response['id']}")Engineered for Python
FastAPI & Async Friendly
Dispatch emails inside background tasks without blocking your high-throughput ASGI event loop.
Django SMTP Drop-in
Configure Django's standard `EMAIL_BACKEND` with `smtp.mailofly.com`:587 for zero-code migration.
Celery & RQ Task Ready
Serialize email dispatch jobs with built-in retries and idempotency keys to prevent duplicate sends.
Explore Mailofly Infrastructure Capabilities
Build resilient, high-deliverability email systems with our core developer tools.
Frequently Asked Questions about Python
Start Sending from Python in Minutes
3,000 free emails every month. Copy your API key, run the code snippet, and watch your delivery logs in real time.