Postmark email provider for FastAPI projects.
Project description
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 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 tokenfrom_email(str, required): Default sender email addressfrom_name(str, optional): Default sender namereply_to(str, optional): Default reply-to email address. Defaults tofrom_emaildefault_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 addresstemplate_id(int, required): Postmark template IDtemplate_variables(dict, optional): Template variablesattachments(List[Dict], optional): List of attachmentsreply_to(str, optional): Override reply-to addressfrom_email(str, optional): Override sender emailfrom_name(str, optional): Override sender name
Returns: Dict containing Postmark API response
send_bulk_emails
Send multiple emails in a batch.
Parameters:
emails(List[Dict], required): List of email dictionariesfrom_email(str, optional): Override sender emailfrom_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 errorsHTTPException: Raised for authentication and validation errors- Automatic mapping of Postmark error codes to HTTP status codes
Development
Setup
- Clone the repository
- Install dependencies:
uv sync
License
MIT License - see LICENSE file for details
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_provider_postmark-0.1.2.tar.gz.
File metadata
- Download URL: fastapi_provider_postmark-0.1.2.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d71fb42b1f5362745dad388f078ff36ad992f0963bcd2abab4acaace83294f8c
|
|
| MD5 |
306123b650d9853a4c14633bd412464a
|
|
| BLAKE2b-256 |
8ae55eac8158a4ce9aea58d75cd43f76ac27a2dfa6d3685fb0834df65317050c
|
Provenance
The following attestation bundles were made for fastapi_provider_postmark-0.1.2.tar.gz:
Publisher:
python-publish.yml on Studio-Piot/fastapi-provider-postmark
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_provider_postmark-0.1.2.tar.gz -
Subject digest:
d71fb42b1f5362745dad388f078ff36ad992f0963bcd2abab4acaace83294f8c - Sigstore transparency entry: 822818037
- Sigstore integration time:
-
Permalink:
Studio-Piot/fastapi-provider-postmark@6db6b88fc6a74158cefa29dfa1eb9fcbd4c6e7bf -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/Studio-Piot
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6db6b88fc6a74158cefa29dfa1eb9fcbd4c6e7bf -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_provider_postmark-0.1.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_provider_postmark-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d605f66164f8657f137dc52f51a8d068a37551b1938b6b1c723c24099c92e2b2
|
|
| MD5 |
da939ea96dc754770f2d63973cb05998
|
|
| BLAKE2b-256 |
8662dd4f9f4a92f893b77b2083b5dc1820279ebb1b510c2beb70c4492d1e7722
|
Provenance
The following attestation bundles were made for fastapi_provider_postmark-0.1.2-py3-none-any.whl:
Publisher:
python-publish.yml on Studio-Piot/fastapi-provider-postmark
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_provider_postmark-0.1.2-py3-none-any.whl -
Subject digest:
d605f66164f8657f137dc52f51a8d068a37551b1938b6b1c723c24099c92e2b2 - Sigstore transparency entry: 822818069
- Sigstore integration time:
-
Permalink:
Studio-Piot/fastapi-provider-postmark@6db6b88fc6a74158cefa29dfa1eb9fcbd4c6e7bf -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/Studio-Piot
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6db6b88fc6a74158cefa29dfa1eb9fcbd4c6e7bf -
Trigger Event:
release
-
Statement type: