Official Python SDK for the Freesend email API
Project description
Freesend Python SDK
Official Python SDK for the Freesend email API.
Installation
pip install freesend
Quick Start
from freesend import Freesend, SendEmailRequest, FreesendConfig
# Initialize the client
config = FreesendConfig(api_key="your-api-key-here")
freesend = Freesend(config)
# Create email request
email_data = SendEmailRequest(
fromName="Your Company",
fromEmail="hello@yourdomain.com",
to="recipient@example.com",
subject="Hello from Freesend!",
html="<h1>Welcome!</h1><p>This email was sent using Freesend.</p>",
text="Welcome! This email was sent using Freesend."
)
# Send the email
try:
response = freesend.send_email(email_data)
print(response.message) # "Email sent successfully"
except Exception as e:
print(f"Failed to send email: {e}")
API Reference
FreesendConfig
Configuration object for the Freesend client.
from freesend import FreesendConfig
config = FreesendConfig(
api_key="your-api-key-here",
base_url="https://freesend.metafog.io" # Optional, defaults to this value
)
SendEmailRequest
Request data for sending an email.
from freesend import SendEmailRequest, Attachment
email_data = SendEmailRequest(
fromEmail="hello@yourdomain.com", # Required
to="recipient@example.com", # Required
subject="Email Subject", # Required
fromName="Your Company", # Optional
text="Plain text content", # Optional (either text or html required)
html="<h1>HTML content</h1>", # Optional (either text or html required)
attachments=[ # Optional
Attachment(
filename="document.pdf",
content="base64_encoded_content",
contentType="application/pdf"
)
]
)
Attachment
Represents an email attachment.
from freesend import Attachment
attachment = Attachment(
filename="document.pdf", # Required
content="base64_encoded_content", # Required (either content or url)
url="https://example.com/file.pdf", # Optional (either content or url, not both)
contentType="application/pdf" # Optional
)
Examples
Basic Email
from freesend import Freesend, SendEmailRequest, FreesendConfig
# Initialize client
freesend = Freesend(FreesendConfig(api_key="your-api-key-here"))
# Send simple email
try:
response = freesend.send_email(SendEmailRequest(
fromName="Your Company",
fromEmail="hello@yourdomain.com",
to="user@example.com",
subject="Welcome to our platform!",
html="<h1>Welcome!</h1><p>Thank you for joining us.</p>",
text="Welcome! Thank you for joining us."
))
print("Email sent:", response.message)
except Exception as e:
print("Failed to send email:", e)
Email with Attachment
import base64
from freesend import Freesend, SendEmailRequest, Attachment, FreesendConfig
# Initialize client
freesend = Freesend(FreesendConfig(api_key="your-api-key-here"))
# Read and encode file
with open("invoice.pdf", "rb") as file:
file_content = base64.b64encode(file.read()).decode('utf-8')
# Create attachment
attachment = Attachment(
filename="invoice.pdf",
content=file_content,
contentType="application/pdf"
)
# Send email with attachment
try:
response = freesend.send_email(SendEmailRequest(
fromName="Your Company",
fromEmail="billing@yourdomain.com",
to="customer@example.com",
subject="Your invoice is ready",
html="<h1>Invoice Attached</h1><p>Please find your invoice attached.</p>",
text="Invoice attached. Please find your invoice attached.",
attachments=[attachment]
))
print("Email sent:", response.message)
except Exception as e:
print("Failed to send email:", e)
Using Custom Base URL
from freesend import Freesend, FreesendConfig
# Initialize with custom base URL
config = FreesendConfig(
api_key="your-api-key-here",
base_url="https://your-custom-freesend-instance.com"
)
freesend = Freesend(config)
# Use as normal...
Error Handling
from freesend import Freesend, SendEmailRequest, FreesendConfig
from freesend.exceptions import FreesendError, FreesendAPIError, FreesendValidationError
freesend = Freesend(FreesendConfig(api_key="your-api-key-here"))
try:
response = freesend.send_email(SendEmailRequest(
fromEmail="hello@yourdomain.com",
to="user@example.com",
subject="Test email"
# Missing html/text content
))
except FreesendValidationError as e:
print("Validation error:", e.message)
except FreesendAPIError as e:
print("API error:", e.message)
print("Status code:", e.status_code)
except FreesendError as e:
print("Freesend error:", e.message)
except Exception as e:
print("Unexpected error:", e)
Common Error Codes
| Status Code | Description |
|---|---|
| 400 | Bad Request - Missing required fields or invalid data |
| 401 | Unauthorized - Invalid or missing API key |
| 403 | Forbidden - API key is inactive or invalid |
| 500 | Internal Server Error - Email sending failed |
Development
# Clone the repository
git clone https://github.com/mokshablr/Freesend.git
cd Freesend/sdk/python
# Install in development mode
pip install -e .
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Format code
black freesend/
# Lint code
flake8 freesend/
# Type checking
mypy freesend/
License
MIT License - see LICENSE 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
freesend-1.0.0.tar.gz
(7.6 kB
view details)
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 freesend-1.0.0.tar.gz.
File metadata
- Download URL: freesend-1.0.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f5fe893007f489df29a682f33a183cb17dd6b7c9afd98fa7ef3910511a30a9d
|
|
| MD5 |
424dd5646eb377a4af9b8ae450910762
|
|
| BLAKE2b-256 |
a56fc158c6e348c89f818a4c38041659820d89e6254c13bf659089c62bca6404
|
File details
Details for the file freesend-1.0.0-py3-none-any.whl.
File metadata
- Download URL: freesend-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b07ade2b7036f2bc698ae9e54588f70655177cc7a687e9ecad57ec742337d809
|
|
| MD5 |
e3764e117fc6088d879ca71a76b0714b
|
|
| BLAKE2b-256 |
3c9687649c685c0686ef4f9b4aa3e7b9dfd260302b3f0235be0543f41a6d8ae8
|