Skip to main content

Python - Getting Started

This guide will help you get up and running with the Mailblock Python SDK in just a few minutes.

Prerequisites

  • Python 3.8 or higher
  • A Mailblock API key from MailBlock

Installation

Install the SDK using pip:
For async support:
For development with all tools:

Basic Setup

First follow the steps from: Basic Setup Import and initialize the SDK with your API key:

Sending Your First Email

Here’s how to send a simple email using the fluent builder pattern:
Alternatively, you can use the EmailData class directly:

Using Method Chaining

The SDK also supports a fluent, chainable API:
For async operations:

Sending HTML Emails

You can send rich HTML emails instead of plain text:
Or using EmailData:
Or with method chaining:

Sending to Multiple Recipients

Send emails to multiple people with CC and BCC support:
You can also use method chaining with multiple recipients:

Scheduling Emails

Schedule emails to be sent at a future date and time:
Or using EmailData:
You can also use ISO date strings:

Managing Scheduled Emails

Canceling Scheduled Emails

Cancel scheduled emails before they’re sent:
For async operations:

Updating Scheduled Emails

Modify scheduled emails before they’re sent:

Environment Configuration

For production applications, use environment variables for your API key:

Debug Mode

Enable debug mode during development to see detailed logs:
Debug logs include:
  • Request details and timing
  • API responses
  • Error information
  • Performance metrics

Custom Logger

You can also provide your own logger:

Type Safety Support

The SDK includes full type hints and works great with mypy:

Context Manager Support

Use the SDK with context managers for proper resource cleanup:

Error Handling

The SDK uses a consistent response format and exception hierarchy:
You can also check response objects:

Advanced Usage

Bulk Email Sending

Send multiple emails efficiently with async:

Installation Options

Install specific extras based on your needs:

Next Steps

Now that you’ve sent your first email with Python, you might want to:
  • Explore the full API reference
  • See more examples
  • Learn about advanced error handling
  • Check out async/await patterns
  • Set up proper logging for production

Need Help?

Key Differences from Node.js

  • Use from_email instead of from (Python keyword conflict)
  • Use send_sync() for synchronous operations, send() for async
  • Import specific classes: from mailblock import MailBlock, EmailData
  • Exception handling with try/catch using Python exception classes
  • Context managers supported with with statement
  • Full type hints support for IDEs and mypy