Skip to main content

How to Configure and Run the Email Worker

Framework M handles email delivery asynchronously to ensure web request handlers are not blocked by SMTP connection latency. Outbound emails are stored in a database queue and sent by a scheduled background worker.

This guide explains how to configure SMTP settings and run the background email worker.


Configuration​

Add your SMTP connection parameters to your application's configuration file (e.g. .env or config.yaml):

[email.smtp]
host = "smtp.mailtrap.io"
port = 2525
username = "your-username"
password = "your-password"
use_tls = true
use_ssl = false
default_sender = "noreply@example.com"

Queueing an Email​

To queue an email, create an EmailRequest and dispatch it through the container's email queue adapter.

from framework_m_core.container import Container
from framework_m_core.interfaces.email_queue import EmailRequest

container = Container()
request = EmailRequest(
from_address="noreply@example.com",
to=["user@example.com"],
subject="Welcome!",
body="Thank you for signing up.",
)
await container.email_queue().queue_email(request)

Running the Email Worker​

The email sender runs as a background task consuming events from the send_direct_email queue. It requires Taskiq (or a similar job queue) and the framework standard worker to be running.

Start the Worker Process​

To start listening to scheduled jobs and background tasks:

uv run m worker

Manual Trigger​

You can also import and enqueue direct jobs manually for testing:

from framework_m_standard.adapters.factory import get_job_queue

# Manually enqueue an email job payload
await get_job_queue().enqueue("send_direct_email", queue_id="123", request_data={...})