Skip to main content

Python SDK for Fmailer email service API (api.fmailer.ru)

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

FmailerSDK

A Python SDK for the Fmailer email service API. Provides both synchronous and asynchronous methods for sending emails via templates or simple HTML content.

Features

  • Synchronous and Asynchronous API - Choose between blocking and non-blocking email sending
  • Template Support - Send emails using pre-configured templates with parameters
  • Simple HTML Emails - Send plain HTML emails directly
  • Idempotency Keys - Prevent duplicate email sends with unique keys
  • Multi-language Support - Send templated emails in different languages
  • Thread Pool Execution - Efficient concurrent email sending with configurable worker threads
  • Callback Support - Handle async results with callbacks for fire-and-forget patterns
  • Fail Silently Mode - Option to suppress exceptions for graceful degradation

Installation

Install the required dependencies:

pip install -r requirements.txt

Dependencies

  • requests - For making HTTP API calls
  • faker - For running tests (development only)

Quick Start

from fmailersdk.sdk import FmailerSdk

# Initialize the SDK
sdk = FmailerSdk(
    username="your-domain@example.com",
    password="your-api-token"
)

# Send a simple HTML email
sdk.send_simple(
    recipient="user@example.com",
    sender="noreply@example.com",
    subject="Welcome!",
    body="<h1>Hello World</h1>"
)

# Send a templated email
sdk.send(
    tpl="welcome-template",
    recipient="user@example.com",
    sender="noreply@example.com",
    lang="en",
    params={"name": "John", "code": "123456"}
)

Usage

Synchronous Methods

Send Simple HTML Email

sdk.send_simple(
    recipient="user@example.com",
    sender="noreply@example.com",
    subject="Important Notice",
    body="<p>This is an important message.</p>",
    idempotency_key="unique-key-123"  # Optional: prevent duplicates
)

Send Templated Email

sdk.send(
    tpl="password-reset",
    recipient="user@example.com",
    sender="noreply@example.com",
    lang="en",  # Optional: language code
    params={"reset_link": "https://example.com/reset/token"},  # Template variables
    idempotency_key="reset-user-123"  # Optional: prevent duplicates
)

Asynchronous Methods

Async methods use a thread pool executor for non-blocking operation. They return Future objects that can be used in various ways:

Fire and Forget

Send emails without waiting for responses:

sdk.send_simple_async(
    recipient="user@example.com",
    sender="noreply@example.com",
    subject="Newsletter",
    body="<h1>Latest Updates</h1>"
)
# Continues immediately without blocking

Using Callbacks

Handle results with callback functions:

def on_complete(success, error):
    if error:
        print(f"Failed to send email: {error}")
    else:
        print("Email sent successfully!")

sdk.send_async(
    tpl="notification",
    recipient="user@example.com",
    sender="noreply@example.com",
    params={"message": "You have a new notification"},
    callback=on_complete
)

Wait for Results

Send async but wait for completion when needed:

future = sdk.send_simple_async(
    recipient="user@example.com",
    sender="noreply@example.com",
    subject="Confirmation",
    body="<p>Please confirm your action</p>"
)

# Do other work...

# Wait for result (with timeout)
try:
    result = future.result(timeout=10)  # Wait up to 10 seconds
    print(f"Email sent: {result}")
except Exception as e:
    print(f"Email failed: {e}")

Batch Sending

Send multiple emails concurrently:

recipients = ["user1@example.com", "user2@example.com", "user3@example.com"]
futures = []

for recipient in recipients:
    future = sdk.send_simple_async(
        recipient=recipient,
        sender="noreply@example.com",
        subject="Batch Email",
        body="<p>Hello!</p>",
        idempotency_key=f"batch-{recipient}"
    )
    futures.append(future)

# Wait for all to complete
for future in futures:
    try:
        future.result(timeout=30)
    except Exception as e:
        print(f"Failed: {e}")

Check Status Without Blocking

future = sdk.send_simple_async(
    recipient="user@example.com",
    sender="noreply@example.com",
    subject="Status Check",
    body="<p>Testing</p>"
)

if future.done():
    result = future.result()
    print(f"Already completed: {result}")
else:
    print("Still processing...")

Configuration

SDK Options

sdk = FmailerSdk(
    username="your-domain@example.com",
    password="your-api-token",
    fail_silently=False,  # If True, suppresses exceptions
    max_workers=5  # Number of concurrent threads for async operations
)

Cleanup

Properly shutdown the thread pool when done:

# Wait for all pending emails to complete before shutdown
sdk.shutdown(wait=True)

Or use a try-finally pattern:

try:
    sdk.send_simple_async(...)
    # ... more operations
finally:
    sdk.shutdown(wait=True)

API Reference

FmailerSdk

Constructor

FmailerSdk(username: str, password: str, fail_silently=False, max_workers=5)
  • username - Your Fmailer account username (typically your domain)
  • password - Your Fmailer API token
  • fail_silently - If True, suppresses exceptions on errors
  • max_workers - Number of threads for async operations (default: 5)

Methods

send_simple(recipient, sender, subject, body, idempotency_key=None) -> bool

Send a simple HTML email synchronously.

send(tpl, recipient, sender, lang=None, params=None, idempotency_key=None) -> bool

Send a templated email synchronously.

send_simple_async(recipient, sender, subject, body, idempotency_key=None, callback=None) -> Future

Send a simple HTML email asynchronously.

send_async(tpl, recipient, sender, lang=None, params=None, idempotency_key=None, callback=None) -> Future

Send a templated email asynchronously.

shutdown(wait=True)

Shutdown the thread pool executor.

Exceptions

FmailerSdkException

Raised when API requests fail or network errors occur. Can be suppressed with fail_silently=True.

Testing

The SDK includes a comprehensive test suite covering both synchronous and asynchronous operations.

Run All Tests

source .venv/bin/activate
PYTHONPATH=/Users/skyman/Documents/My/Python:$PYTHONPATH python -m unittest tests

Run Specific Test Class

# Synchronous tests
PYTHONPATH=/Users/skyman/Documents/My/Python:$PYTHONPATH python -m unittest tests.FmailersdkTestUtils

# Asynchronous tests
PYTHONPATH=/Users/skyman/Documents/My/Python:$PYTHONPATH python -m unittest tests.FmailersdkAsyncTestUtils

Run Specific Test

PYTHONPATH=/Users/skyman/Documents/My/Python:$PYTHONPATH python -m unittest tests.FmailersdkAsyncTestUtils.test_send_simple_async_success

Examples

See async_example.py for comprehensive examples of all async patterns including:

  • Fire and forget
  • Callback handling
  • Waiting for results
  • Batch sending
  • Status checking
  • Proper cleanup

API Endpoints

The SDK communicates with the following Fmailer API endpoints:

  • Base URL: https://api.fmailer.ru/external/
  • Simple Send: POST /external/send_email_simple/
  • Template Send: POST /external/send_email_tpl/

Contributing

Contributions are welcome! Please ensure all tests pass before submitting a pull request.

License

[Add your license information here]

Support

For issues, questions, or feature requests, please contact Fmailer support or open an issue in this repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fmailersdk-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fmailersdk-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file fmailersdk-0.1.0.tar.gz.

File metadata

  • Download URL: fmailersdk-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for fmailersdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b09347cddb02d24708b13e82331684f92a1681e8c8331184266c99c323a62565
MD5 1389ae66b97b5dbbdbcf3448e2f25c39
BLAKE2b-256 c2a7e23a3b0a23ccd1ee655de39b070f9cc07788129f7ce380936f8ecc5b6cae

See more details on using hashes here.

File details

Details for the file fmailersdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fmailersdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for fmailersdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06e032a7e4a0e90554fa568e04e1916bd0c39e0f98bed0b87c522143e03bf6bc
MD5 2bf229ccca4e43f5dcf5cae56cb0022b
BLAKE2b-256 48971e2cba350e6bc9e77034606e8a82184de919f4fc909e2c3f74ec14414b5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page