Skip to main content

FastAPI Provider Postmark

A Postmark email provider package for FastAPI projects that provides a clean, class-based interface for sending emails via the Postmark API.

Features

  • 📧 Email Sending

    • Send templated emails using Postmark templates
    • Support for attachments
    • Customizable sender information
    • Default template variables
  • 📦 Bulk Email

    • Send multiple emails in a single API call
    • Efficient batch processing
  • 📊 Statistics

    • Retrieve email delivery statistics
    • Monitor email performance
  • 🛡️ Error Handling

    • Comprehensive error handling
    • HTTP exception mapping
    • Detailed logging

Installation

Using UV:

uv add fastapi-provider-postmark

Or using pip:

pip install fastapi-provider-postmark

Quick Start

Basic Usage

from fastapi import FastAPI
from fastapi_provider_postmark import PostmarkProvider

app = FastAPI()

# Initialize the provider
postmark = PostmarkProvider(
    api_key="your-postmark-api-key",
    from_email="support@example.com",
    from_name="My App",
    reply_to="support@example.com",
    default_template_variables={
        "product_url": "https://example.com",
        "product_name": "My App",
        "support_email": "support@example.com",
    }
)

@app.post("/send-email")
async def send_email():
    await postmark.send_email(
        to="user@example.com",
        template_id=123456,
        template_variables={
            "user_name": "John Doe",
            "action_url": "https://example.com/verify",
        }
    )
    return {"message": "Email sent successfully"}

With Environment Variables

import os
from fastapi_provider_postmark import PostmarkProvider

postmark = PostmarkProvider(
    api_key=os.getenv("POSTMARK_API_KEY"),
    from_email=os.getenv("POSTMARK_FROM_EMAIL", "noreply@example.com"),
    from_name=os.getenv("POSTMARK_FROM_NAME", "My App"),
    reply_to=os.getenv("POSTMARK_REPLY_TO", "support@example.com"),
    default_template_variables={
        "product_url": os.getenv("PRODUCT_URL", "https://example.com"),
        "product_name": os.getenv("PRODUCT_NAME", "My App"),
    }
)

Sending Emails with Attachments

attachments = [
    {
        "Name": "document.pdf",
        "Content": "base64_encoded_content_here",
        "ContentType": "application/pdf"
    }
]

await postmark.send_email(
    to="user@example.com",
    template_id=123456,
    template_variables={"user_name": "John Doe"},
    attachments=attachments
)

Sending Emails with Custom Headers

await postmark.send_email(
    to="user@example.com",
    template_id=123456,
    template_variables={"unsubscribe_url": unsubscribe_url},
    headers={
        "List-Unsubscribe": f"<mailto:hello@example.com?subject=unsubscribe>, <{one_click_url}>",
        "List-Unsubscribe-Post": "List-Unsubscribe=One-Click",
    },
)

Sending Bulk Emails

emails = [
    {
        "to": "user1@example.com",
        "template_id": 123456,
        "template_variables": {"user_name": "User 1"}
    },
    {
        "to": "user2@example.com",
        "template_id": 123456,
        "template_variables": {"user_name": "User 2"}
    }
]

responses = await postmark.send_bulk_emails(emails)

Getting Delivery Statistics

stats = await postmark.get_delivery_stats()
print(f"Sent: {stats.get('Sent')}")
print(f"Bounced: {stats.get('Bounced')}")

Using with Dependency Injection

from fastapi import Depends
from fastapi_provider_postmark import PostmarkProvider

def get_postmark_provider() -> PostmarkProvider:
    return PostmarkProvider(
        api_key=os.getenv("POSTMARK_API_KEY"),
        from_email=os.getenv("POSTMARK_FROM_EMAIL"),
        from_name=os.getenv("POSTMARK_FROM_NAME"),
    )

@app.post("/send-email")
async def send_email(
    postmark: PostmarkProvider = Depends(get_postmark_provider)
):
    await postmark.send_email(
        to="user@example.com",
        template_id=123456,
        template_variables={"user_name": "John Doe"}
    )
    return {"message": "Email sent"}

API Reference

PostmarkProvider

__init__

Initialize the Postmark provider.

Parameters:

  • api_key (str, required): Postmark API server token
  • from_email (str, required): Default sender email address
  • from_name (str, optional): Default sender name
  • reply_to (str, optional): Default reply-to email address. Defaults to from_email
  • default_template_variables (dict, optional): Default template variables to include in all emails

send_email

Send an email using a Postmark template.

Parameters:

  • to (str, required): Recipient email address
  • template_id (int, required): Postmark template ID
  • template_variables (dict, optional): Template variables
  • attachments (List[Dict], optional): List of attachments
  • reply_to (str, optional): Override reply-to address
  • from_email (str, optional): Override sender email
  • from_name (str, optional): Override sender name
  • headers (Dict[str, str], optional): Custom email headers, name to value

Returns: Dict containing Postmark API response

send_bulk_emails

Send multiple emails in a batch.

Parameters:

  • emails (List[Dict], required): List of email dictionaries
  • from_email (str, optional): Override sender email
  • from_name (str, optional): Override sender name

Returns: List of response dictionaries

get_delivery_stats

Get email delivery statistics from Postmark.

Returns: Dict containing delivery statistics

Error Handling

The provider includes comprehensive error handling:

  • PostmarkProviderError: Custom exception for provider errors
  • HTTPException: Raised for authentication and validation errors
  • Automatic mapping of Postmark error codes to HTTP status codes

Development

Setup

  1. Clone the repository
  2. Install dependencies:
uv sync

License

MIT License - see LICENSE file for details

Metadata

Release files for fastapi-provider-postmark 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-provider-postmark 0.1.3
File Size Uploaded Provenance Actions
fastapi_provider_postmark-0.1.3.tar.gz 9.3 kB With provenance Details for fastapi_provider_postmark-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-provider-postmark 0.1.3
File Interpreter ABI Platform Actions
fastapi_provider_postmark-0.1.3-py3-none-any.whl Python 3 none any Details for fastapi_provider_postmark-0.1.3-py3-none-any.whl

Total release size: 16.4 kB

Release files / fastapi_provider_postmark-0.1.3.tar.gz

Download URL fastapi_provider_postmark-0.1.3.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ee113607c4983fadc87e9360a3f764a1929881a36927d77aeb55e3f78d43b6c5
BLAKE2b-256 checksum
How to use checksums
e50e2a6efb1c6054a2c11d93b35e17697f95c636d6f1547121e4a932b623dc0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Release files / fastapi_provider_postmark-0.1.3-py3-none-any.whl

Download URL fastapi_provider_postmark-0.1.3-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
259832311e61f0a462d728a9ffeb60b1051283a5c9bd90e5e82048d842c806ef
BLAKE2b-256 checksum
How to use checksums
1a453ee25ef5d9bc2305047eee957154c1d9618330d4c417d6009241b211e914
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Security for fastapi-provider-postmark 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastapi_provider_postmark-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page