Skip to main content

A unified email library for VentureCube organization - supports SMTP, Gmail, SendGrid, AWS SES, and Mailgun

Project description

vc-web-email

A unified email library for VentureCube organization. Send emails through multiple providers with a single, consistent API.

Features

  • Multiple Providers: SMTP, Gmail, SendGrid, AWS SES, Mailgun, Outlook
  • Unified API: Same interface for all providers
  • Attachments: Easy file attachment support
  • Validation: Built-in email and message validation
  • Retry Logic: Automatic retry with exponential backoff
  • Config Files: Load configuration from YAML or JSON
  • Type Hints: Full type annotation support
  • Extensible: Easy to add custom providers

Installation

# Basic installation
pip install vc-web-email

# With SendGrid support
pip install vc-web-email[sendgrid]

# With AWS SES support
pip install vc-web-email[aws]

# With all providers
pip install vc-web-email[all]

# Development installation
pip install vc-web-email[dev]

Quick Start

Basic Usage

from vc_web_email import EmailClient

# Create client with configuration
client = EmailClient({
    'provider': 'smtp',
    'smtp': {
        'host': 'smtp.example.com',
        'port': 587,
        'username': 'your-username',
        'password': 'your-password',
    },
    'default_from': 'sender@example.com'
})

# Send an email
response = client.send(
    to='recipient@example.com',
    subject='Hello from vc-web-email',
    text='This is a plain text message.',
    html='<h1>This is an HTML message.</h1>'
)

if response.success:
    print(f"Email sent! Message ID: {response.message_id}")
else:
    print(f"Failed to send: {response.error}")

Load Configuration from File

Create email-config.yml:

provider: gmail
gmail:
  username: your-email@gmail.com
  password: your-16-char-app-password

default_from: your-email@gmail.com
retry_attempts: 3
timeout: 30
from vc_web_email import EmailClient

client = EmailClient.from_config('email-config.yml')
response = client.send(
    to='recipient@example.com',
    subject='Hello',
    text='Hello World!'
)

Environment Variables

from vc_web_email import EmailClient

# Load from environment variables
client = EmailClient.from_env()

Required environment variables:

  • VC_EMAIL_PROVIDER: Provider name (smtp, gmail, sendgrid, aws_ses, mailgun, outlook)
  • VC_EMAIL_FROM: Default sender email
  • Provider-specific variables (see documentation)

Providers

SMTP

client = EmailClient({
    'provider': 'smtp',
    'smtp': {
        'host': 'smtp.example.com',
        'port': 587,
        'username': 'user@example.com',
        'password': 'password',
        'use_tls': True,
    },
    'default_from': 'user@example.com'
})

Gmail

client = EmailClient({
    'provider': 'gmail',
    'gmail': {
        'username': 'your-email@gmail.com',
        'password': 'your-16-char-app-password',  # App Password, not regular password
    },
    'default_from': 'your-email@gmail.com'
})

Note: For Gmail, you need to:

  1. Enable 2-Factor Authentication
  2. Generate an App Password at https://myaccount.google.com/apppasswords

SendGrid

client = EmailClient({
    'provider': 'sendgrid',
    'sendgrid': {
        'api_key': 'SG.your-api-key',
    },
    'default_from': 'sender@example.com'
})

AWS SES

client = EmailClient({
    'provider': 'aws_ses',
    'aws_ses': {
        'region': 'us-east-1',
        'access_key_id': 'AKIAIOSFODNN7EXAMPLE',
        'secret_access_key': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
    },
    'default_from': 'sender@example.com'
})

Mailgun

client = EmailClient({
    'provider': 'mailgun',
    'mailgun': {
        'api_key': 'key-xxx',
        'domain': 'mg.example.com',
    },
    'default_from': 'sender@mg.example.com'
})

Outlook/Microsoft 365

client = EmailClient({
    'provider': 'outlook',
    'outlook': {
        'username': 'your-email@outlook.com',
        'password': 'your-password',
    },
    'default_from': 'your-email@outlook.com'
})

Advanced Usage

Attachments

from vc_web_email import EmailClient, Attachment

client = EmailClient.from_config('config.yml')

# Attach from file path
response = client.send(
    to='recipient@example.com',
    subject='Document attached',
    text='Please find the document attached.',
    attachments=['path/to/document.pdf']
)

# Or use Attachment class
attachment = Attachment.from_file('path/to/image.png')
response = client.send(
    to='recipient@example.com',
    subject='Image attached',
    text='See the image.',
    attachments=[attachment]
)

Bulk Sending

from vc_web_email import EmailClient, EmailMessage

client = EmailClient.from_config('config.yml')

messages = [
    EmailMessage(to='user1@example.com', subject='Hello User 1', text='Hi!'),
    EmailMessage(to='user2@example.com', subject='Hello User 2', text='Hi!'),
    EmailMessage(to='user3@example.com', subject='Hello User 3', text='Hi!'),
]

response = client.send_bulk(messages)

print(f"Sent: {response.successful}/{response.total}")
print(f"Success rate: {response.success_rate:.0%}")

CC and BCC

response = client.send(
    to='recipient@example.com',
    subject='Team Update',
    text='Here is the update...',
    cc=['manager@example.com', 'lead@example.com'],
    bcc='archive@example.com'
)

Custom Headers

response = client.send(
    to='recipient@example.com',
    subject='High Priority',
    text='Urgent message!',
    headers={
        'X-Priority': '1',
        'X-Custom-Header': 'custom-value'
    }
)

Using Context Manager

with EmailClient.from_config('config.yml') as client:
    response = client.send(
        to='recipient@example.com',
        subject='Test',
        text='Hello!'
    )

API Reference

EmailClient

# Initialization
client = EmailClient(config)
client = EmailClient.from_config('path/to/config.yml')
client = EmailClient.from_env()

# Sending
response = client.send(to, subject, text=None, html=None, ...)
response = client.send_message(message)
response = client.send_bulk(messages)

# Utilities
client.test_connection()  # Returns True if connection works
client.get_provider_name()  # Returns provider name
client.close()  # Close connections

EmailMessage

from vc_web_email import EmailMessage

message = EmailMessage(
    to='recipient@example.com',  # or list
    subject='Subject line',
    text='Plain text body',
    html='<p>HTML body</p>',
    from_email='sender@example.com',
    cc=['cc@example.com'],
    bcc=['bcc@example.com'],
    reply_to='reply@example.com',
    attachments=[attachment],
    headers={'X-Custom': 'value'},
    tags=['newsletter', 'weekly'],
    metadata={'campaign_id': '123'}
)

# Add attachment
message.add_attachment('path/to/file.pdf')
message.add_attachment(Attachment(...))

EmailResponse

response.success  # bool
response.message_id  # str or None
response.status  # EmailStatus enum
response.provider  # str
response.error  # str or None
response.timestamp  # datetime
response.recipients  # list[str]
response.to_dict()  # dict

Exceptions

from vc_web_email.exceptions import (
    VCEmailError,  # Base exception
    ConfigurationError,  # Invalid configuration
    ValidationError,  # Message validation failed
    ProviderError,  # Provider-specific error
    ConnectionError,  # Connection failed
    AuthenticationError,  # Auth failed
    RateLimitError,  # Rate limit exceeded
    AttachmentError,  # Attachment issue
)

Testing

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run with coverage
pytest --cov=vc_web_email --cov-report=html

# Run specific test file
pytest tests/test_client.py

# Run with verbose output
pytest -v

Development

# Clone repository
git clone https://github.com/venturecube/vc-web-email.git
cd vc-web-email

# Create virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows

# Install in development mode
pip install -e ".[dev]"

# Run tests
pytest

# Format code
black vc_web_email/
isort vc_web_email/

# Type checking
mypy vc_web_email/

# Linting
flake8 vc_web_email/

License

MIT License - see LICENSE file for details.

Support

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

vc_web_email-1.0.0.tar.gz (36.0 kB view details)

Uploaded Source

Built Distribution

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

vc_web_email-1.0.0-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file vc_web_email-1.0.0.tar.gz.

File metadata

  • Download URL: vc_web_email-1.0.0.tar.gz
  • Upload date:
  • Size: 36.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for vc_web_email-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0a6598169ee520091f98751ea4955c3147639d92d0ee601b7f258809701d60c2
MD5 d555a3079c0e07e56a8f757b2f9f2d85
BLAKE2b-256 06d564a460666b123d161cfc325e5a72de34f11703bec9695f395bd9f098573a

See more details on using hashes here.

File details

Details for the file vc_web_email-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: vc_web_email-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 31.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for vc_web_email-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 360b62f77dfbcc8794dc824ca04b2651122d44d63d314d200b952e2a14ccb998
MD5 916c842ea5a8a218db1c116659f7396d
BLAKE2b-256 8a6a5aac3bb5acf6770afb25707a4cebea6bc54248ca77caa7e1674b36afd77a

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