LettermintLettermint
  • Knowledge base
  • Community
  • Changelog
  • Support
  • Documentation
  • Sending API
  • Team API
  • MCP server
Get started
Send email
    Send with
    SMTP
      IntroductionNodemailerPHPMailerPython (smtplib)Go SMTPRuby (Mail)
    Email activitySchedulingTest emailsTLSIdempotencySuppressionsTagsInline imagesData retentionSending limits
    Tracking
Receive email
Manage
Resources
SMTP

Python (smtplib)

Use Python's smtplib module to send email through the Lettermint SMTP relay.

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' # Existing singular tag msg['X-LM-Tag'] = 'order-confirmation' # Structured tags. MIMEMultipart permits repeated custom headers. msg['X-LM-Tags'] = 'campaign=orders, environment=production' msg['X-LM-Tags'] = 'variant=B' # 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'))

X-LM-Tag is the singular tag. X-LM-Tags contains structured pairs. Assign the custom field again to add another header.

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.

PHPMailerGo SMTP
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.' )