Transactional email¶
Send email over SMTP with EmailUtils — plain-text body + HTML
alternative, attachments, and Jinja2 template rendering. Requires the
[email] extra (aiosmtplib + jinja2 + email-validator).
Configuration¶
EmailSettings ships the SMTP fields ready to use (SMTP_HOST,
SMTP_PORT, SMTP_USERNAME, SMTP_PASSWORD, SMTP_FROM_ADDR,
SMTP_USE_TLS, SMTP_USE_SSL, SMTP_TIMEOUT_SECONDS). Compose it into
your Settings and use email_kwargs() to build the utility without
mapping field by field:
# src/core/mailer.py
from tempest_fastapi_sdk import EmailUtils
from src.core.settings import settings
mailer = EmailUtils(**settings.email_kwargs())
email_kwargs() bridges the SMTP names to the EmailUtils ones:
SMTP_USE_TLS (STARTTLS, port 587) → use_starttls; SMTP_USE_SSL
(implicit TLS from connect, port 465) → use_tls.
SMTP_USE_TLS or SMTP_USE_SSL: the port decides¶
The two names are easy to mix up, and swapping them breaks every send. The port decides:
SMTP_PORT |
Mode | SMTP_USE_TLS |
SMTP_USE_SSL |
|---|---|---|---|
587 |
STARTTLS: connect in clear text, then upgrade | true |
false |
465 |
Implicit TLS (SMTPS): encrypted from the first byte | false |
true |
SMTP_USE_SSL=true on 587 is the classic mistake: the client opens a
TLS handshake, the server answers its 220 banner in clear text, and the
send fails with [SSL: WRONG_VERSION_NUMBER]. With ENV=production and
e-mail on, the guard refuses to boot on the three combinations that
cannot work: SMTP_USE_SSL and SMTP_USE_TLS both true,
SMTP_USE_SSL=true on 587, and SMTP_USE_TLS=true on 465. Other ports
(your own relay on 2525, say) are not judged.
STARTTLS vs SSL/TLS — opportunistic by default
Port 587 → SMTP_USE_TLS=true (default): connect in clear text and
upgrade via STARTTLS. Port 465 → SMTP_USE_SSL=true: connect
already encrypted. STARTTLS is opportunistic: EmailUtils only
upgrades when the server advertises STARTTLS, so a plain server
(MailHog on :1025, or :25) just works — no more SMTPException:
SMTP STARTTLS extension not supported by server. (since v0.38.1). To
force plain text without even attempting the upgrade, set
SMTP_USE_TLS=false; the .env.example generated by tempest new
with [email] already ships that way for MailHog.
Production: real SMTP and credentials¶
In production SMTP is not optional — you send through a real provider
(Gmail/Workspace, AWS SES, SendGrid, Mailgun, ...) and that needs a
host, port, username, and password. The golden rule: those
credentials always come from the environment (.env / secret manager
/ container variables) and are never in the code or committed to Git.
An SMTP credential is a secret — treat it like a DB password
SMTP_PASSWORDnever lands in the repo. Keep.envin.gitignoreand commit only a.env.examplewith fake values.- For Gmail/Workspace do not use the account password — generate
an App Password (https://myaccount.google.com/apppasswords)
with 2FA enabled. The normal password fails with
535 Authentication failed. SMTP_FROM_ADDRmust be an address on a domain you control and have authenticated (SPF/DKIM/DMARC), otherwise the email is marked spam or rejected.
The production services already follow this pattern — declare the SMTP
fields on your Settings and read everything from the environment:
# src/core/settings.py
from pydantic import Field
from tempest_fastapi_sdk import BaseAppSettings, EmailSettings
class Settings(EmailSettings, BaseAppSettings):
"""Application settings. SMTP inherited from EmailSettings."""
FRONTEND_URL: str = Field(
default="http://localhost:3000",
description="Frontend base URL (used in email links).",
)
settings: Settings = Settings()
# .env.example (committed — fake values)
# the real .env (NOT committed) holds the actual credentials
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=your_email@example.com
SMTP_PASSWORD="your_app_password"
SMTP_FROM_ADDR=your_email@example.com
SMTP_USE_TLS=true # STARTTLS on 587
SMTP_USE_SSL=false # use true (and PORT=465) for implicit TLS
Common providers and their port/TLS combination:
| Provider | SMTP_HOST |
SMTP_PORT |
SMTP_USE_TLS (STARTTLS) |
SMTP_USE_SSL (implicit TLS) |
|---|---|---|---|---|
| Gmail / Workspace | smtp.gmail.com |
587 |
true |
false |
| Gmail (direct TLS) | smtp.gmail.com |
465 |
false |
true |
| AWS SES | email-smtp.<region>.amazonaws.com |
587 |
true |
false |
| SendGrid | smtp.sendgrid.net |
587 |
true |
false |
| MailHog (local dev) | localhost |
1025 |
false |
false |
How alofans-api and transport-backend do it
These are the two production patterns: alofans-api uses Gmail on
port 587 (STARTTLS) with an App Password — SMTP_USE_TLS=true.
transport-backend uses port 465 (implicit TLS) —
SMTP_USE_SSL=true, SMTP_PORT=465. Both read SMTP_* from the
environment and never hardcode the password. Pick STARTTLS (587) or
SSL (465) per your provider; do not enable both at once.
Send an email¶
Each send() opens a fresh SMTP connection. to accepts a string or an
iterable; the body (plain text) is always sent and html becomes the
multipart alternative when present.
import asyncio
from tempest_fastapi_sdk import EmailUtils
from src.core.settings import settings
mailer = EmailUtils(**settings.email_kwargs())
async def main() -> None:
"""Run this example."""
await mailer.send(
to="ana@example.com",
subject="Welcome!",
body="Your account was created.",
html="<p>Your account was <strong>created</strong>.</p>",
)
asyncio.run(main())
Optional per-message parameters: cc, bcc, attachments
(Path or EmailAttachment — see Attachments), reply_to, and from_addr (overrides the default
sender). Every transport failure — a host that does not resolve, a
connection refused or timed out, a broken TLS handshake, a rejected login,
a message the server refuses — raises EmailDeliveryException (503,
code="EMAIL_UNAVAILABLE"). A network or TLS failure chains the original
aiosmtplib error as __cause__; a refusal the server answered (rejected
login, refused recipient) is raised from None, because the reply can
quote the address — and the AppException handler logs every 5xx with
its traceback. Before raising, EmailUtils logs once at ERROR with
smtp_host, smtp_port, smtp_error and smtp_code (the refusal's SMTP
code), never the recipient. An error that is yours — a missing attachment, a bad
argument — keeps its own type.
Code catching aiosmtplib.SMTPException must catch the new one
send no longer lets an aiosmtplib error escape. An
except aiosmtplib.SMTPException around the send catches nothing
now; switch to except EmailDeliveryException (from
tempest_fastapi_sdk). A refusal's SMTP code is in the log
(smtp_code), not on the exception.
Attachments: a file on disk or bytes in memory¶
attachments accepts Path (read from disk) and EmailAttachment (bytes you
already hold in memory), mixed in the same list. Content generated on the fly
— a report, a spreadsheet, the PDF from PdfRenderer — goes straight in, with
no temporary file to write and delete:
import asyncio
import csv
import io
from tempest_fastapi_sdk import EmailAttachment, EmailUtils
mailer = EmailUtils(host="localhost", port=1025, from_addr="no-reply@example.com")
def build_report() -> bytes:
"""Build the sales CSV in memory."""
buffer = io.StringIO()
writer = csv.writer(buffer)
writer.writerow(["product", "quantity"])
writer.writerow(["ticket", 3])
return buffer.getvalue().encode("utf-8")
async def main() -> None:
"""Run this example."""
await mailer.send(
to="ana@example.com",
subject="Daily report",
body="The report is attached.",
attachments=[EmailAttachment(filename="sales.csv", content=build_report())],
)
asyncio.run(main())
Each part's Content-Type comes from the file name, via
mimetypes.guess_type — sales.csv goes out as text/csv, receipt.pdf as
application/pdf, logo.png as image/png. That holds for Path and for an
EmailAttachment without an explicit type. An unknown extension (or none) and
a compression suffix (backup.tar.gz, data.csv.gz) fall back to
application/octet-stream: for those, guess_type describes the
decompressed content, which is not what the bytes are.
To pin the type, pass content_type:
EmailAttachment("receipt", pdf_bytes, content_type="application/pdf").
The PDF from PdfRenderer goes in this way — render_document
returns bytes.
EmailAttachment refuses at construction, with ValueError:
- a
content_typethat is not a baretype/subtype("pdf","text/csv; charset=utf-8", with a space or a line break); - an empty
filename, or one with a line break, NUL,/or\. A line break would be a header injection (the standard library refuses it too, but only when the message is assembled); a slash turns the name into a path. APathwhose name has one of these characters is refused the same way, before anything is sent.
Behaviour change for Path
Until now every attachment went out as application/octet-stream. A
.pdf Path now goes out as application/pdf. Code that relies on the
old type passes
EmailAttachment(path.name, path.read_bytes(), "application/octet-stream")
instead of the Path.
Sending to many: send_many¶
send() is for the one-recipient transactional message. Telling the whole
base something — maintenance, an announcement, a campaign — is where the naive
loop gets expensive:
import asyncio
from tempest_fastapi_sdk import BulkEmailReport, EmailUtils
from src.core.settings import settings
mailer = EmailUtils(**settings.email_kwargs())
recipients = ["ana@example.com", "bruno@example.com", "cadu@example.com"]
async def main() -> None:
"""Tell the base, then deal with what did not land."""
report: BulkEmailReport = await mailer.send_many(
recipients,
subject="Scheduled maintenance",
body="We are down from 02:00 to 03:00.",
batch_size=500,
max_concurrency=32,
)
print(report.delivered, "delivered,", report.failed, "not")
for dead in report.permanent:
print(f"prune {dead.email}: {dead.code} {dead.message}")
for later in report.transient:
print(f"requeue {later.email}: {later.code}")
asyncio.run(main())
What it does that for user in users: await mailer.send(...) does not:
Loop over send() |
send_many() |
|
|---|---|---|
| SMTP connections | one per message | one per batch (batch_size) |
| Parallelism | none, or unbounded gather |
max_concurrency open connections |
| Bad address | raises, ending the run | lands in the report |
5xx vs 4xx |
indistinguishable | permanent vs transient |
Concurrency is per connection, not per message
SMTP is serial on one socket: a batch is sent sequentially over its
connection, and parallelism comes from running several batches at once.
That is why max_concurrency counts open connections — the number a
provider actually throttles.
Only an operation failure raises
Host that will not resolve, connection refused, authentication rejected:
those raise EmailDeliveryException, as in send, because the operation
failed. Batches already delivered are not reported anywhere — the
exception takes the report's place. A refusal of one recipient never
raises — it is the report. 5xx means the mailbox does not exist (prune it
from your base); 4xx means full or greylisted (requeue). A refusal with no
code is filed as transient: the cheap mistake is retrying, not deleting a
good address.
Jinja2 templates¶
Pass template_dir= at construction and render with render_template() —
the Jinja2 environment is built lazily on first call and memoized.
Autoescaping is on for .html / .htm / .xml.
# src/core/mailer.py
import asyncio
from tempest_fastapi_sdk import EmailUtils
from src.core.settings import settings
mailer = EmailUtils(
host=settings.SMTP_HOST,
port=settings.SMTP_PORT,
from_addr=settings.SMTP_FROM_ADDR,
template_dir="src/templates/emails",
)
html: str = mailer.render_template(
"welcome.html",
{"user_name": "Ana", "app_url": "https://app.example.com"},
)
async def main() -> None:
"""Run this example."""
await mailer.send(
to="ana@example.com",
subject="Welcome!",
body="Welcome, Ana!",
html=html,
)
asyncio.run(main())
Bundled auth-flow templates
The bundled auth flow (make_auth_router) already sends activation
and password-reset emails using built-in templates
(activation.html, password_reset.html). Drop same-named files in
your template_dir to override them. See Auth flow.
Example: password reset¶
A service that sends a reset link with a short-lived JWT. Note that
request_reset returns silently for an unregistered email — this avoids
account enumeration. For the same reason it swallows a send failure: a
503 only for accounts that exist would tell which addresses are
registered.
# src/services/password_reset.py
import logging
from datetime import timedelta
from tempest_fastapi_sdk import EmailDeliveryException, EmailUtils, JWTUtils
from src.db.repositories import UserRepository
logger: logging.Logger = logging.getLogger(__name__)
class PasswordResetService:
def __init__(
self,
repo: UserRepository,
tokens: JWTUtils,
mailer: EmailUtils,
) -> None:
self.repo: UserRepository = repo
self.tokens: JWTUtils = tokens
self.mailer: EmailUtils = mailer
async def request_reset(self, email: str) -> None:
"""Send a reset link to ``email`` (silent if it does not exist)."""
user = await self.repo.get_or_none({"email": email})
if user is None:
return
token: str = self.tokens.encode(
{"sub": str(user.id), "typ": "password_reset"},
ttl=timedelta(minutes=15),
)
reset_url: str = f"https://app.example.com/reset-password?token={token}"
try:
await self.mailer.send(
to=user.email,
subject="Reset your password",
body=f"Open to reset: {reset_url}",
html=f'<p>Click <a href="{reset_url}">here</a> to reset.</p>',
)
except EmailDeliveryException:
logger.error("Reset link not sent; answer kept the same")
E-mail switched off (EMAIL_ENABLED=false)¶
With EMAIL_ENABLED=true and ENV=production, the guard refuses what can
only be a development setup: a mailpit/mailhog host, localhost on
port 1025, the default SMTP_HOST (localhost) with no SMTP_USERNAME,
the default SMTP_FROM_ADDR (noreply@example.com), an empty one or one
on a reserved domain, and a TLS mode swapped with the port (see
the port decides). An authenticated local relay passes, and so does
Gmail (smtp.gmail.com:587, STARTTLS, app password, real sender).
No SMTP yet? EMAIL_ENABLED=false skips those rules, and the same
EmailUtils(**settings.email_kwargs()) then refuses send and
send_many with IntegrationDisabledException (503,
details={"integration": "email"}) before building the message.
render_template keeps working, and the [email] extra is not required.
The auth flow refuses the signup before creating the account — see
Email disabled and
Switching integrations on and off.
Recap¶
- Install
[email]and composeEmailSettingsinto yourSettings. - One
EmailUtilsinstance per app;send()is async and opens a connection per call. - The text
bodyis required;htmlis the optional multipart alternative. attachmentsmixesPathandEmailAttachment(in-memory bytes); theContent-Typecomes from the file name, withapplication/octet-streamwhen it cannot be guessed.render_template()(withtemplate_dir) builds the HTML from Jinja2.- The bundled auth flow already sends activation/reset with overridable templates.
- Port
587needsSMTP_USE_TLS=true;465needsSMTP_USE_SSL=true— the production guard refuses the swapped pair. - An SMTP failure raises
EmailDeliveryException(503 EMAIL_UNAVAILABLE); see SMTP server down for what each auth route answers.