Developer SDK

Python Transactional Email API & SDK

Send transactional emails from Python applications with sub-50ms REST dispatch, automated DKIM signing, and parcel delivery receipts.

Asynchronous and Synchronous Python Support

Whether you are building with FastAPI, Flask, Django, or asynchronous background workers like Celery and RQ, SendPorter gives you ergonomic client options with zero bloat.

Method 1: Synchronous Sending with Requests

Dispatch transactional emails using Python's popular requests package:

import os
import requests

def send_transactional_email(to_email: str, receipt_id: str, amount: float):
    url = "https://api.sendporter.com/v1/send"
    headers = {
        "Authorization": f"Bearer {os.environ['SENDPORTER_API_KEY']}",
        "Content-Type": "application/json",
    }
    payload = {
        "from": "billing@yourdomain.com",
        "to": to_email,
        "subject": f"Payment Receipt #{receipt_id}",
        "html": f"<p>Thank you for your payment of <strong>{amount} USD</strong>.</p>",
        "tags": ["billing", "receipt"],
        "track_opens": True,
        "track_clicks": True,
    }

    response = requests.post(url, json=payload, timeout=5)
    response.raise_for_status()
    data = response.json()
    print("Parcel tracking ID:", data.get("id"))
    return data

Method 2: Asynchronous Sending with HTTPX & FastAPI

In modern async frameworks like FastAPI or Quart, use non-blocking HTTP requests with httpx:

import os
import httpx
from fastapi import FastAPI, BackgroundTasks

app = FastAPI()

async def async_send_welcome(email: str):
    async with httpx.AsyncClient() as client:
        await client.post(
            "https://api.sendporter.com/v1/send",
            headers={"Authorization": f"Bearer {os.environ['SENDPORTER_API_KEY']}"},
            json={
                "from": "hello@yourdomain.com",
                "to": email,
                "subject": "Welcome to our platform!",
                "html": "<h1>Welcome aboard!</h1><p>We're thrilled to have you.</p>",
            },
            timeout=10.0,
        )

@app.post("/signup")
async def signup(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(async_send_welcome, email)
    return {"status": "user created, email queued"}

Method 3: Verifying Webhooks in Python

Verify webhook authenticity using Python's standard hmac and hashlib modules:

import hmac
import hashlib

def verify_webhook_signature(payload_bytes: bytes, signature_header: str, secret: str) -> bool:
    computed_signature = hmac.new(
        secret.encode("utf-8"),
        msg=payload_bytes,
        digestmod=hashlib.sha256
    ).hexdigest()

    return hmac.compare_digest(computed_signature, signature_header)

Production Patterns for Python Applications

Ensure reliable message dispatch in production web applications with these core guidelines:

  • Persistent HTTP Sessions: Reuse a requests.Session() or httpx.AsyncClient() instance across requests to maintain TCP keep-alive connections and shave 40ms off TLS handshakes.
  • Structured Parcel Tracking: Log the id returned in SendPorter's JSON response to your structured log aggregator (Datadog, CloudWatch, Grafana Loki) for end-to-end tracing.
  • Automatic Retries on 5xx: Configure urllib3.util.Retry on your session to retry idempotently on transient network hiccups.

Frequently Asked Questions

Can I use Python standard library smtplib with SendPorter?

Yes. Python's built-in smtplib.SMTP("smtp.sendporter.com", 587) with starttls() and login() works out of the box with zero external pip dependencies.

How are attachments handled in the Python API?

You can pass base64-encoded file strings with filename and content-type in the attachments array, or send MIME multipart messages directly through our SMTP relay.

Is there an official SendPorter SDK on PyPI?

Yes, you can install the official client via pip install sendporter for typed models and helper utilities.

Start sending emails from Python in minutes

Create your SendPorter account with 2,500 free monthly emails.

Request Access