Official Python SDK•Backend & Data Engineering

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 mailofly
Setup Tutorial

Get Up and Running in 3 Easy Steps

01

Install via pip or poetry

Run `pip install mailofly` in your virtual environment. Includes type stubs for mypy and Pyright.

02

Initialize with your environment key

Load your API key from `os.environ` and instantiate `client = Mailofly(api_key=...)`.

03

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.

main.py
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']}")
Why Developers Love It

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.

Got Questions?

Frequently Asked Questions about Python

Yes. You can use FastAPI's `BackgroundTasks` to trigger `client.emails.send()` so your HTTP response returns immediately without waiting for the network roundtrip.

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.