response = ( client.email .from_("John Doe <john@yourdomain.com>") .to("recipient@example.com") .subject("Hello from Lettermint") .text("This is a test email sent using the Lettermint Python SDK.") .send())print(f"Email sent with ID: {response['message_id']}")
3. Email Features
Basic Email
Send a simple text or HTML email:
Code
response = ( client.email .from_("John Doe <john@yourdomain.com>") .to("recipient@example.com") .subject("Your account is ready!") .html("<h1>Welcome!</h1><p>Thanks for signing up.</p>") .text("Welcome! Thanks for signing up.") .send())
One tag per message. Tags can contain letters, numbers, hyphens, underscores, and spaces (max 255 characters).
See Tags documentation for more details.
Route Selection
Direct emails to specific routes within your project:
response = ( client.email .from_("notifications@yourdomain.com") .to("user@example.com") .subject("Order Confirmation") .html("<p>Your order has been confirmed.</p>") .idempotency_key("order-12345-confirmation") .send())
Use a unique key per logical email (e.g., combining order ID + email type). Retrying with the same key won't send duplicate emails.
4. Async Support
The SDK provides an async client for use with asyncio:
Code
import asyncioimport osfrom lettermint import AsyncLettermintasync def send_email(): async with AsyncLettermint(api_token=os.environ.get("LETTERMINT_API_TOKEN")) as client: response = await ( client.email .from_("John Doe <john@yourdomain.com>") .to("recipient@example.com") .subject("Hello from Lettermint") .text("This is a test email.") .send() ) print(f"Email sent with ID: {response['message_id']}")asyncio.run(send_email())
Use the async client in FastAPI, Starlette, or other async frameworks for better performance.
5. Client Configuration
Customize the client with optional parameters:
Code
client = Lettermint( api_token=os.environ.get("LETTERMINT_API_TOKEN"), base_url="https://api.lettermint.co/v1", # Custom API URL timeout=60.0, # Request timeout in seconds)
Use the client as a context manager for automatic resource cleanup:
Code
with Lettermint(api_token=os.environ.get("LETTERMINT_API_TOKEN")) as client: response = ( client.email .from_("sender@yourdomain.com") .to("recipient@example.com") .subject("Test") .text("Hello!") .send() )
6. Response
Code
response = ( client.email .from_("John Doe <john@yourdomain.com>") .to("recipient@example.com") .subject("Test") .text("Hello!") .send())print(response["message_id"]) # Unique email ID
7. Error Handling
Handle errors with specific exception types:
Code
from lettermint import Lettermintfrom lettermint.exceptions import ( ValidationError, ClientError, TimeoutError, HttpRequestError,)client = Lettermint(api_token=os.environ.get("LETTERMINT_API_TOKEN"))try: response = ( client.email .from_("sender@yourdomain.com") .to("recipient@example.com") .subject("Test") .text("Hello!") .send() )except ValidationError as e: # 422 errors (invalid parameters, daily limit exceeded, etc.) print(f"Validation error: {e.error_type}")except ClientError as e: # 400 errors (bad request) print(f"Client error: {e}")except TimeoutError as e: # Request timed out print(f"Timeout: {e}")except HttpRequestError as e: # Other HTTP errors print(f"HTTP error {e.status_code}: {e}")