swadhinbiswas docs: add 11 undocumented feature pages (+758 lines, 151 total pages) · 7/25/2026
1697b71
87 lines · 2.0 KB
Raw Download

title: Mailer
description: Send emails from JustAPI applications — sync, async, and template-based.
keywords: [JustAPI, mailer, email, SMTP, send email, templates]

Mailer sends emails over SMTP. It supports synchronous and asynchronous sending, plus Jinja2 templates.

Setup

from justapi import JustAPIApp, Mailer

app = JustAPIApp()

mailer = Mailer(
    host="smtp.example.com",
    port=587,
    username="[email protected]",
    password="secret",
    use_tls=True,
    default_from="[email protected]",
    default_from_name="My App",
)

Send Email

@app.post("/send")
def send_email():
    mailer.send(
        to="[email protected]",
        subject="Hello",
        body="This is the plain text body",
        html="<h1>Hello</h1><p>This is the HTML body</p>",
    )
    return {"status": "sent"}

Async Send

@app.post("/send-async")
async def send_email_async(background_tasks: BackgroundTasks):
    background_tasks.add_task(
        mailer.send_async,
        to="[email protected]",
        subject="Hello",
        body="Sent asynchronously",
    )
    return {"status": "queued"}

Send with Template

mailer = Mailer(
    host="smtp.example.com",
    port=587,
    templates="templates/email",
)

@app.post("/send-template")
def send_welcome():
    mailer.send_template(
        template_name="welcome.html",
        to="[email protected]",
        subject="Welcome!",
        context={"name": "Alice"},
    )
    return {"status": "sent"}

Parameters

Parameter Type Default Description
host str — SMTP server hostname
port int 587 SMTP port
username str None Auth username
password str None Auth password
use_tls bool True Enable TLS
default_from str None Default sender email
default_from_name str None Default sender name
templates str None Directory for Jinja2 email templates