Basic Configuration

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os

# SMTP configuration
SMTP_HOST = 'smtp.lettermint.co'
SMTP_PORT = 587
USERNAME = 'lettermint'
API_TOKEN = 'your-api-token'

def send_email(to_email, subject, html_body, text_body=None):
    # Create message
    msg = MIMEMultipart('alternative')
    msg['From'] = 'sender@yourdomain.com'
    msg['To'] = to_email
    msg['Subject'] = subject

    # Add text and HTML parts
    if text_body:
        msg.attach(MIMEText(text_body, 'plain'))
    msg.attach(MIMEText(html_body, 'html'))

    # Send email
    try:
        with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
            server.starttls()
            server.login(USERNAME, API_TOKEN)
            server.send_message(msg)
        print("Email sent successfully")
    except Exception as e:
        print(f"Error sending email: {e}")

# Usage
send_email(
    to_email='recipient@example.com',
    subject='Test Email',
    html_body='<h1>Hello!</h1><p>This is a test email.</p>',
    text_body='Hello! This is a test email.'
)

Advanced Features

Multiple Recipients

def send_bulk_email(recipients, subject, html_body):
    msg = MIMEMultipart('alternative')
    msg['From'] = 'sender@yourdomain.com'
    msg['Subject'] = subject
    msg.attach(MIMEText(html_body, 'html'))

    with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
        server.starttls()
        server.login(USERNAME, API_TOKEN)

        for recipient in recipients:
            msg['To'] = recipient
            server.send_message(msg)
            del msg['To']  # Remove for next iteration

Attachments

def send_email_with_attachment(to_email, subject, body, file_path):
    msg = MIMEMultipart()
    msg['From'] = 'sender@yourdomain.com'
    msg['To'] = to_email
    msg['Subject'] = subject

    msg.attach(MIMEText(body, 'html'))

    # Add attachment
    with open(file_path, 'rb') as attachment:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(attachment.read())

    encoders.encode_base64(part)
    part.add_header(
        'Content-Disposition',
        f'attachment; filename= {os.path.basename(file_path)}'
    )
    msg.attach(part)

    with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
        server.starttls()
        server.login(USERNAME, API_TOKEN)
        server.send_message(msg)