---
title: "Send email from Python over SMTP"
description: "stdlib smtplib — no framework required."
url: https://www.unitpost.com/guides/smtp/python
section: SMTP
updated: 2026-08-28
---
# Send email from Python over SMTP

## Connection settings

- **Host**: smtp.unitpost.com
- **Username**: unitpost (always this literal value)
- **Password**: Your Unitpost API key (must carry emails:send)
- **Port (STARTTLS)**: 587 (or 2587 if blocked)
- **Port (implicit TLS)**: 465 (or 2465 if blocked)
- **From address**: Any address on a verified domain

## Send email from Python over SMTP

> stdlib smtplib — no framework required.

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.

### 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 (/guides/smtp/django), Flask (/guides/smtp/flask), FastAPI (/guides/smtp/fastapi).

## Related

- [Send email from Flask over SMTP](https://www.unitpost.com/guides/smtp/flask): Flask-Mail pointed at Unitpost.
- [Send email from FastAPI over SMTP](https://www.unitpost.com/guides/smtp/fastapi): smtplib from a FastAPI dependency.
- [Send email from Go over SMTP](https://www.unitpost.com/guides/smtp/go): net/smtp with STARTTLS.
