---
title: "Send email from FastAPI over SMTP"
description: "smtplib from a FastAPI dependency."
url: https://www.unitpost.com/guides/smtp/fastapi
section: SMTP
updated: 2026-08-28
---
# Send email from FastAPI 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 FastAPI over SMTP

> smtplib from a FastAPI dependency.

Call Python's SMTP client from a FastAPI route. Same host, username, and API-key password as every other stack.

### 1. Send from a route

```python
import os
import smtplib
from email.message import EmailMessage
from fastapi import FastAPI

app = FastAPI()

@app.post("/send")
def send() -> dict[str, bool]:
    msg = EmailMessage()
    msg["From"] = "you@yourdomain.com"
    msg["To"] = "customer@example.com"
    msg["Subject"] = "Hello from FastAPI"
    msg.set_content("Sent via SMTP.")
    with smtplib.SMTP("smtp.unitpost.com", 587) as smtp:
        smtp.starttls()
        smtp.login("unitpost", os.environ["UNITPOST_API_KEY"])
        smtp.send_message(msg)
    return {"ok": True}
```

### 2. Verify delivery

Confirm in Activity.

## Related

- [Send email from Go over SMTP](https://www.unitpost.com/guides/smtp/go): net/smtp with STARTTLS.
- [Send email from Spring Boot over SMTP](https://www.unitpost.com/guides/smtp/spring): JavaMailSender properties pointed at Unitpost.
- [Send email from .NET over SMTP](https://www.unitpost.com/guides/smtp/dotnet): MailKit pointed at Unitpost.
