LettermintLettermint
  • Knowledge base
  • Community
  • Changelog
  • Support
  • Documentation
  • Sending API
  • Team API
Getting started
Guides
    Node.jsPHPPythonGoLaravelMagento 2NuxtJava
    SMTP
      IntroductionPHPMailerNodemailerPython (smtplib)Ruby (Mail)Golang
Platform
Resources
SMTP

Python (smtplib)

This guide covers SMTP integration. For full configuration options and available headers, see the SMTP Introduction.

Basic Configuration

Use environment variables for credentials:

Code
import os SMTP_HOST = 'smtp.lettermint.co' SMTP_USERNAME = 'lettermint' API_TOKEN = os.environ.get('LETTERMINT_PROJECT_TOKEN')

Advanced Features

Multiple Recipients

Code
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

Code
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)

Lettermint Headers

Add Lettermint-specific headers for tags, metadata, and routing:

Code
from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart msg = MIMEMultipart('alternative') msg['From'] = 'sender@yourdomain.com' msg['To'] = 'recipient@example.com' msg['Subject'] = 'Order Confirmation' # Tag for categorization msg['X-LM-Tag'] = 'order-confirmation' # Metadata for tracking (included in webhooks) msg['X-LM-Metadata-order_id'] = '12345' msg['X-LM-Metadata-customer_id'] = 'cust_789' # Route selection msg['X-Lettermint-Route'] = 'transactional' msg.attach(MIMEText('<p>Your order has been confirmed.</p>', 'html'))

Metadata headers are extracted by Lettermint and included in webhook payloads, but not added to the actual email sent to recipients. See the SMTP Introduction for full details on available headers.

Last modified on May 11, 2026
NodemailerRuby (Mail)
On this page
  • Basic Configuration
  • Advanced Features
    • Multiple Recipients
    • Attachments
    • Lettermint Headers
import smtplib import os from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # SMTP configuration SMTP_HOST = 'smtp.lettermint.co' SMTP_PORT = 587 USERNAME = 'lettermint' API_TOKEN = os.environ.get('LETTERMINT_PROJECT_TOKEN') def send_email(to_email, subject, html_body, text_body=None): msg = MIMEMultipart('alternative') msg['From'] = 'sender@yourdomain.com' msg['To'] = to_email msg['Subject'] = subject if text_body: msg.attach(MIMEText(text_body, 'plain')) msg.attach(MIMEText(html_body, 'html')) 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}") 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.' )