Skip to main content

Official Lettermint Python SDK

Project description

Lettermint Python SDK

PyPI Version PyPI Downloads Python Version GitHub Tests License

Official Python SDK for the Lettermint email API.

Installation

pip install lettermint

Quick Start

Sending Emails (Synchronous)

from lettermint import Lettermint

client = Lettermint(api_token="your-api-token")

response = (
    client.email
    .from_("sender@example.com")
    .to("recipient@example.com")
    .subject("Hello from Python!")
    .html("<h1>Welcome!</h1>")
    .text("Welcome!")
    .send()
)

print(response["message_id"])

Sending Emails (Asynchronous)

from lettermint import AsyncLettermint

async with AsyncLettermint(api_token="your-api-token") as client:
    response = await (
        client.email
        .from_("sender@example.com")
        .to("recipient@example.com")
        .subject("Hello from Python!")
        .html("<h1>Welcome!</h1>")
        .send()
    )
    print(response["message_id"])

Email Options

Multiple Recipients

client.email.from_("sender@example.com").to(
    "recipient1@example.com",
    "recipient2@example.com"
).subject("Hello").send()

CC and BCC

client.email.from_("sender@example.com").to("recipient@example.com").cc(
    "cc1@example.com",
    "cc2@example.com"
).bcc("bcc@example.com").subject("Hello").send()

Reply-To

client.email.from_("sender@example.com").to("recipient@example.com").reply_to(
    "reply@example.com"
).subject("Hello").send()

RFC 5322 Addresses

client.email.from_("John Doe <john@example.com>").to(
    "Jane Doe <jane@example.com>"
).subject("Hello").send()

Attachments

import base64

# Read and encode your file
with open("document.pdf", "rb") as f:
    content = base64.b64encode(f.read()).decode()

# Regular attachment
client.email.from_("sender@example.com").to("recipient@example.com").subject(
    "Your Document"
).attach("document.pdf", content).send()

# Inline attachment (for embedding in HTML)
client.email.from_("sender@example.com").to("recipient@example.com").subject(
    "Welcome"
).html('<img src="cid:logo@example.com">').attach(
    "logo.png", logo_content, "logo@example.com"
).send()

Custom Headers

client.email.from_("sender@example.com").to("recipient@example.com").subject(
    "Hello"
).headers({"X-Custom-Header": "value"}).send()

Metadata and Tags

client.email.from_("sender@example.com").to("recipient@example.com").subject(
    "Hello"
).metadata({"campaign_id": "123", "user_id": "456"}).tag("welcome-campaign").send()

Routing

client.email.from_("sender@example.com").to("recipient@example.com").subject(
    "Hello"
).route("my-route").send()

Idempotency Key

Prevent duplicate sends when retrying failed requests:

client.email.from_("sender@example.com").to("recipient@example.com").subject(
    "Hello"
).idempotency_key("unique-request-id").send()

Webhook Verification

Verify webhook signatures to ensure authenticity:

from lettermint import Webhook

# Create a webhook verifier
webhook = Webhook(secret="your-webhook-secret")

# Verify using headers (recommended)
payload = webhook.verify_headers(request.headers, request.body)

# Or verify using the signature directly
payload = webhook.verify(
    payload=request.body,
    signature=request.headers["X-Lettermint-Signature"],
)

print(payload["event"])

Static Method

For one-off verification:

from lettermint import Webhook

payload = Webhook.verify_signature(
    payload=request.body,
    signature=request.headers["X-Lettermint-Signature"],
    secret="your-webhook-secret",
)

Custom Tolerance

Adjust the timestamp tolerance (default: 300 seconds):

webhook = Webhook(secret="your-webhook-secret", tolerance=600)

Error Handling

from lettermint import Lettermint
from lettermint.exceptions import (
    ValidationError,
    ClientError,
    HttpRequestError,
    TimeoutError,
)

client = Lettermint(api_token="your-api-token")

try:
    response = client.email.from_("sender@example.com").to("recipient@example.com").subject(
        "Hello"
    ).send()
except ValidationError as e:
    # 422 errors (e.g., daily limit exceeded)
    print(f"Validation error: {e.error_type}")
    print(f"Response: {e.response_body}")
except ClientError as e:
    # 400 errors
    print(f"Client error: {e}")
except TimeoutError as e:
    # Request timeout
    print(f"Timeout: {e}")
except HttpRequestError as e:
    # Other HTTP errors
    print(f"HTTP error {e.status_code}: {e}")

Webhook Errors

from lettermint import Webhook
from lettermint.exceptions import (
    InvalidSignatureError,
    TimestampToleranceError,
    JsonDecodeError,
    WebhookVerificationError,
)

try:
    payload = webhook.verify_headers(headers, body)
except InvalidSignatureError:
    print("Invalid signature - request may be forged")
except TimestampToleranceError:
    print("Timestamp too old - possible replay attack")
except JsonDecodeError:
    print("Invalid JSON in payload")
except WebhookVerificationError as e:
    print(f"Verification failed: {e}")

Configuration

Custom Base URL

client = Lettermint(
    api_token="your-api-token",
    base_url="https://custom.api.com/v1",
)

Custom Timeout

client = Lettermint(
    api_token="your-api-token",
    timeout=60.0,  # 60 seconds
)

Context Manager

Both sync and async clients support context managers for proper resource cleanup:

# Sync
with Lettermint(api_token="your-api-token") as client:
    client.email.from_("sender@example.com").to("recipient@example.com").send()

# Async
async with AsyncLettermint(api_token="your-api-token") as client:
    await client.email.from_("sender@example.com").to("recipient@example.com").send()

Type Hints

This SDK is fully typed with py.typed marker. You'll get full autocomplete and type checking in your IDE.

Requirements

  • Python 3.9+
  • httpx

License

MIT

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

lettermint-1.0.1.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

lettermint-1.0.1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file lettermint-1.0.1.tar.gz.

File metadata

  • Download URL: lettermint-1.0.1.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lettermint-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b79d28d1a35001a59896158421c3599feb38a4d49fdebfca41f862817ec5403e
MD5 615015f25c1dd25da13da35b58139217
BLAKE2b-256 c9a6ff4a7357ea664ce6dc82c7cd680609303a554e77c1e7c68be66cdeb802d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for lettermint-1.0.1.tar.gz:

Publisher: release.yml on lettermint/lettermint-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lettermint-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: lettermint-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lettermint-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 008cf459a24b1c95b2d63138ccfd1bdd2cbc3a1de0fe592c6e93423e5a5e64c3
MD5 9eb948c93d533751f9f6279443afc66b
BLAKE2b-256 4e16ea9a9ca84bef730b51a5a4cb7e35282a8bbf699d82eea4962fb16a79e8ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for lettermint-1.0.1-py3-none-any.whl:

Publisher: release.yml on lettermint/lettermint-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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