Send email from Python over SMTP

Python's smtplib talks SMTP directly. Point it at Unitpost on port 587 with STARTTLS. Django and Flask wrap this; this page is the bare client.

What are the SMTP settings?

Hostsmtp.unitpost.com
Usernameunitpost(always this literal value)
PasswordYour Unitpost API key(must carry emails:send)
Port (STARTTLS)587(or 2587 if blocked)
Port (implicit TLS)465(or 2465 if blocked)
From addressAny address on a verified domain

How do I set it up?

  1. Send with smtplib

    Python
    import os
    import smtplib
    from email.message import EmailMessage
    
    msg = EmailMessage()
    msg["From"] = "you@yourdomain.com"
    msg["To"] = "customer@example.com"
    msg["Subject"] = "Hello from Python"
    msg.set_content("Sent via SMTP.")
    msg.add_alternative("<p>Sent via SMTP.</p>", subtype="html")
    
    with smtplib.SMTP("smtp.unitpost.com", 587) as smtp:
        smtp.starttls()
        smtp.login("unitpost", os.environ["UNITPOST_API_KEY"])
        smtp.send_message(msg)
  2. Framework wrappers

    Using a framework? Django, Flask, FastAPI.