CONSOLE
Loading
Send transactional emails in Python with zero external libraries. Complete code snippets for HTML emails, attachments, and automatic retry queues.
In Python, send email by initializing smtplib.SMTP(host, 587), creating an SSL/TLS security context with server.starttls(), authenticating with server.login(username, password), and dispatching an EmailMessage or MIMEMultipart object via server.send_message(msg). For high-volume production, manage secrets via python-dotenv and implement retry backoff loops for connection exceptions.
Python developers typically choose between standard SMTP and HTTP REST APIs:
import smtplib
import ssl
from email.message import EmailMessage
# 1. Define Message Parameters
msg = EmailMessage()
msg['Subject'] = "Your One-Time Verification Code"
msg['From'] = "Security Team <auth@yourdomain.com>"
msg['To'] = "recipient@example.com"
# Plaintext fallback
msg.set_content("Your login OTP is: 849201. It expires in 5 minutes.")
# HTML Rich Body
msg.add_alternative("""
<div style="font-family: sans-serif; padding: 20px;">
<h2>Your Login Verification Code</h2>
<p style="font-size: 24px; font-weight: bold; color: #4F46E5;">849201</p>
<p style="color: #64748B;">Expires in 5 minutes.</p>
</div>
""", subtype='html')
# 2. Connect & Dispatch via PhotonRelay
context = ssl.create_default_context()
with smtplib.SMTP("mail.photonrelay.com", 587) as server:
server.starttls(context=context)
server.login("your_photon_api_key", "your_photon_api_secret")
server.send_message(msg)
print("Email dispatched successfully!")
To attach PDF invoices or receipts, read the file in binary mode:
with open('invoice_1092.pdf', 'rb') as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype='application', subtype='pdf', filename=file_name)
import time
def send_with_retry(msg, max_retries=3):
for attempt in range(max_retries):
try:
with smtplib.SMTP("mail.photonrelay.com", 587, timeout=10) as s:
s.starttls()
s.login("key", "secret")
s.send_message(msg)
return True
except (smtplib.SMTPServerDisconnected, TimeoutError) as e:
wait = 2 ** attempt
print(f"Error: {e}. Retrying in {wait}s...")
time.sleep(wait)
return False
Deploy in minutes. Get 5,000 free monthly transactional emails and transparent $0.50/1k pay-as-you-go scaling.
Trademark Notice & Nominative Fair Use: All third-party trademarks, logos, and service marks (SendGrid®, Mailgun®, Amazon SES®, Postmark®, Resend®, Brevo®, SMTP2Go®, SparkPost®) are property of their respective owners and used under 15 U.S.C. § 1115(b)(4).
Photon Console is independent and not affiliated with or endorsed by these providers. Pricing and plan details compiled from public sources as of November 2026. Read full legal disclaimer →