Skip to main content

Simple email gateway with IMAP/SMTP clients, connection pooling, and MCP server

Project description

simple-email-gw

PyPI version Python versions License CI Code style: ruff Type checked: mypy Read the Docs

A simple email gateway with IMAP/SMTP clients, connection pooling, and MCP server for AI assistant integration.

Note: This package provides async-only APIs. All client methods are async and must be used with asyncio.run() or in async contexts.

Features

  • Async IMAP and SMTP clients (aioimaplib, aiosmtplib)
  • Connection pooling with automatic management
  • Token bucket rate limiting
  • Audit logging for security compliance
  • CRLF injection prevention
  • Recipient whitelist enforcement
  • TLS 1.2+ minimum encryption
  • MCP server for AI assistant integration

Installation

Using pip

pip install simple-email-gw

Using uv

uv add simple-email-gw

Quick Start

MCP Server

Run the MCP server for AI assistant integration:

# Set environment variables
export EMAIL_IMAP_HOST=imap.gmail.com
export EMAIL_SMTP_HOST=smtp.gmail.com
export EMAIL_USERNAME=your-email@gmail.com
export EMAIL_PASSWORD=your-app-password

# Run server
uvx --from simple-email-gw mcp-server

Programmatic Usage

import asyncio
from simple_email_gw import IMAPClient, SMTPClient, EmailAccount

# Create account configuration
account = EmailAccount(
  name="work",
  imap_host="imap.gmail.com",
  smtp_host="smtp.gmail.com",
  username="user@gmail.com",
  password="app-password"
)

async def main():
  # Use IMAP client
  async with IMAPClient(account) as client:
    folders = await client.list_folders()
    messages = await client.search(folder="INBOX")
    print(f"Found {len(messages)} messages")

  # Use SMTP client
  smtp = SMTPClient(account)
  result = await smtp.send_email(
    to=["recipient@example.com"],
    subject="Test",
    body="Hello world"
  )
  print(f"Sent: {result}")

asyncio.run(main())

Configuration

Environment Variables

Single account configuration:

EMAIL_IMAP_HOST=imap.gmail.com
EMAIL_SMTP_HOST=smtp.gmail.com
EMAIL_USERNAME=user@gmail.com
EMAIL_PASSWORD=app-password

Multiple accounts (JSON):

EMAIL_ACCOUNTS_JSON='[{"name":"work","imap_host":"imap.gmail.com","smtp_host":"smtp.gmail.com","username":"work@example.com","password":"secret"},{"name":"personal","imap_host":"imap.icloud.com","smtp_host":"smtp.icloud.com","username":"personal@icloud.com","password":"secret"}]'

See docs/configuration.md for detailed configuration options.

Rate Limiting

Default limits:

  • IMAP: 60 requests per minute per account
  • SMTP: 100 sends per hour per account

Recipient Whitelist

Restrict outgoing emails to specific domains or addresses:

EMAIL_RECIPIENT_DOMAINS=gmail.com,icloud.com
EMAIL_RECIPIENT_ADDRESSES=admin@company.com

Security Features

TLS 1.2+ Minimum

All connections require TLS 1.2 or higher. Connections with older TLS versions are rejected.

CRLF Injection Prevention

All email headers are sanitized to prevent CRLF injection attacks:

  • Subject lines
  • Message-IDs
  • References headers
  • Email addresses

Recipient Whitelist

Optional whitelist restricts outgoing emails to approved recipients.

Rate Limiting

Token bucket algorithm prevents abuse with separate limits per account.

Audit Logging

All operations are logged for security compliance.

See docs/security.md for complete security documentation.

MCP Tools

Tool Purpose
list_accounts List configured email accounts
list_folders List IMAP folders
search_emails Search messages by criteria
get_email Fetch single message
download_attachment Download attachment to workspace
send_email Send new email
reply_email Reply to thread
move_email Move between folders
delete_email Delete message
mark_email_read Mark message as read

See docs/api.md for complete API reference.

Development

See docs/development.md for development setup and contribution guidelines.

Quick commands:

make dev-env    # Install development dependencies
make test       # Run tests
make lint       # Run linter
make typecheck  # Run type checker
make all        # Run all checks
make mcp-server # Run the MCP server

License

MIT License - see LICENSE for details.

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

simple_email_gw-0.1.0.tar.gz (892.0 kB view details)

Uploaded Source

Built Distribution

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

simple_email_gw-0.1.0-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for simple_email_gw-0.1.0.tar.gz
Algorithm Hash digest
SHA256 592e7062e72995070dd68d11ce2fddaeed1debcb8fbd6bd5238f5f656a92280a
MD5 a269f480792d254bfaba118cacf44990
BLAKE2b-256 fb8d7e74f4bad24ead3d3ac0f1768c69f7d65f7ab3a091f4eb00f4fef4af13be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for simple_email_gw-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 431269f571ee046c8e202d7825288719f7ea60f35e27c17118ddcd43276cfdfc
MD5 4ef43fe45f1413446e02dff2a1bde65e
BLAKE2b-256 199d858f3de920786305aed08dc3ac6ee11dd94dedaa8134d550ac03123e1b44

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