Email¶
A tiny SMTP email client built on the standard library — no extra dependency.
Register it¶
# extensions.py
from endocore.extensions import EmailExtension
extensions = [
EmailExtension(
host="smtp.example.com", port=587, use_tls=True,
username="apikey", password="…", default_from="no-reply@example.com",
),
]
This registers an EmailClient as a DI provider under the name email (and by
type).
Send from a handler¶
async def handler(request, email): # injected EmailClient
email.send(
to="user@example.com",
subject="Welcome",
body="Thanks for signing up!",
)
return Response.json({"sent": True})
HTML and multiple recipients¶
email.send(
to=["a@example.com", "b@example.com"],
subject="Report",
body="<h1>Weekly report</h1>…",
html=True,
from_addr="reports@example.com",
)
Without the extension¶
from endocore.extensions import EmailClient
client = EmailClient(host="localhost", port=25)
client.send(to="a@b.com", subject="Hi", body="Hello")
Sending in the background
Sending email can be slow — return it as a background task so the client isn't kept waiting, or enqueue it via Celery for retries.