agentleFS
litestar-orglitestar-org/litestar-skills/skills/litestar-email/SKILL.md

litestar-email

Skill19 starsChanged 11 days agoMITView on GitHub

Use when working with litestar_email, EmailPlugin, EmailConfig, EmailService, EmailMessage, EmailMultiAlternatives, InMemoryBackend, SMTPConfig, ResendConfig, SendGridConfig, MailgunConfig, SESConfig. Not for marketing APIs — use vendor SDKs.

  • Installs packages

The file

Show its rules and text

We split this skill into the rules it gives an agent

16 rules. About 0 are common, 4 are distinctive, and 12 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 12

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use NamedDependency[EmailService] with Provide(email_config.provide_service().__aiter__) (or FromDishka[EmailService] when the project uses …

    line 15
  2. Distinctive:

    Keep API keys and SMTP credentials in the project's settings layer.

    line 23
  3. Very specific:

    If using the plugin's default dependency directly without overriding dependencies, register signature_types=[AsyncServiceProvider], …

    line 131
  4. Very specific:

    If the project uses Dishka, yield EmailService from a Scope.REQUEST provider and inject FromDishka[EmailService]:

    line 136
  5. Very specific:

    Use backend="memory" in tests and clear InMemoryBackend.outbox between tests.

    line 255
  6. Very specific:

    Do not pass html_body to EmailMessage.

    line 267
  7. Very specific:

    Do not import nonexistent symbols such as ConsoleConfig, InMemoryConfig, EmailAttachment, FallbackConfig, RateLimitConfig, or RetryConfig.

    line 269
  8. Very specific:

    Do not pass file paths as attachments.

    line 274
  9. Very specific:

    Do not pass a string to reply_to.

    line 276
  10. Very specific:

    Do not annotate a route handler with mailer: NamedDependency[EmailService] without binding …

    line 277
  11. Very specific:

    Do not read app state as an open service by default.

    line 281
  12. Very specific:

    Do not configure a named API backend separately from its settings.

    line 283
  13. Distinctive:

    Do not send SES attachments.

    line 286
  14. Very specific:

    Do not assume fail_silently=True suppresses every exception.

    line 287
  15. Distinctive:

    Do not hard-code API keys, SMTP passwords, or AWS credentials.

    line 290
  16. Distinctive:

    Do not force a provider migration.

    line 291

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

litestar-email

litestar-email 0.4.0 provides one async sending interface for console, memory, SMTP, Resend, SendGrid, Mailgun, Amazon SES, and custom backends. Match the backend already selected by the project; keep message construction independent from the transport.

Code Style Rules

  • Use NamedDependency[EmailService] with Provide(email_config.provide_service().__aiter__) (or FromDishka[EmailService] when the project uses Dishka) for direct EmailService handler injection.
  • Pass recipient collections as list[str]. to, cc, bcc, and reply_to are list fields.
  • Pass attachment content as bytes. Do file I/O before constructing the message and keep that I/O async.
  • Await send_message() and send_messages(). Both return the count sent.
  • Keep API keys and SMTP credentials in the project's settings layer.

Quick Reference

Install

pip install "litestar-email>=0.4.0"
pip install "litestar-email[smtp]>=0.4.0"   # aiosmtplib
pip install "litestar-email[ses]>=0.4.0"    # botocore for SigV4
pip install "litestar-email[httpx]>=0.4.0"   # default HTTP transport
pip install "litestar-email[aiohttp]>=0.4.0" # alternative HTTP transport

The HTTP API backends select httpx by default, but the transport is optional in litestar-email itself. Install the httpx or aiohttp extra (unless the project already provides that dependency), and select aiohttp only when the project standardizes on it.

Configure the Plugin

from os import environ

from litestar import Litestar
from litestar_email import EmailConfig, EmailPlugin, SMTPConfig

email_config = EmailConfig(
    backend=SMTPConfig(
        host="smtp.example.com",
        port=587,
        username=environ["SMTP_USERNAME"],
        password=environ["SMTP_PASSWORD"],
        use_tls=True,
    ),
    from_email="noreply@example.com",
    from_name="Example App",
)

app = Litestar(plugins=[EmailPlugin(config=email_config)])

EmailConfig fields:

FieldDefaultContract
backend"console"Registered name, import path, or built-in backend config object
from_email"noreply@localhost"Default sender address
from_name""Default display name
fail_silentlyFalseBackend-specific best-effort delivery behavior
email_service_dependency_key"mailer"Litestar DI key
email_service_state_key"mailer"Key holding the config in app state

The dependency and state keys occupy separate namespaces. Change them independently when the application already uses either key:

email_config = EmailConfig(
    backend="memory",
    email_service_dependency_key="email_service",
    email_service_state_key="email_config",
)

Inject EmailService

EmailPlugin.on_app_init() registers:

  • Provide(config.provide_service, sync_to_thread=False) under email_service_dependency_key;
  • config.signature_namespace (BaseEmailBackend, EmailConfig, EmailMessage, EmailMultiAlternatives, EmailService, MailgunConfig, ResendConfig, SESConfig, SMTPConfig, SendGridConfig) in Litestar's signature namespace;
  • the EmailConfig instance under email_service_state_key in app state.

Because EmailConfig.provide_service() is a synchronous method returning an AsyncServiceProvider instance rather than an async def generator function, Litestar's default Provide(config.provide_service, sync_to_thread=False) injects AsyncServiceProvider rather than EmailService.

To inject an open EmailService with request-scoped connection cleanup, bind Provide(email_config.provide_service().__aiter__) on the router, controller, or handler (or use Dishka's FromDishka[EmailService]):

from litestar import post
from litestar.di import NamedDependency, Provide
from litestar_email import EmailConfig, EmailMessage, EmailService

email_config = EmailConfig(backend="memory", from_email="noreply@example.com")


@post(
    "/notifications",
    dependencies={"mailer": Provide(email_config.provide_service().__aiter__)},
)
async def send_notification(
    mailer: NamedDependency[EmailService],
) -> dict[str, int]:
    sent = await mailer.send_message(
        EmailMessage(
            subject="Notification",
            body="You have a new notification.",
            to=["recipient@example.com"],
        ),
    )
    return {"sent": sent}

If using the plugin's default dependency directly without overriding dependencies, register signature_types=[AsyncServiceProvider], annotate the parameter as mailer: NamedDependency[AsyncServiceProvider], and enter async with mailer as service: inside the handler.

If the project uses Dishka, yield EmailService from a Scope.REQUEST provider and inject FromDishka[EmailService]:

from collections.abc import AsyncIterator

from dishka import Provider, Scope
from dishka.integrations.litestar import FromDishka, inject
from litestar import post
from litestar_email import EmailConfig, EmailMessage, EmailService


def build_email_provider(config: EmailConfig) -> Provider:
    async def provide_mailer() -> AsyncIterator[EmailService]:
        async with config.provide_service() as mailer:
            yield mailer

    provider = Provider(scope=Scope.REQUEST)
    provider.provide(provide_mailer)
    return provider


@post("/welcome")
@inject
async def send_welcome(
    email_service: FromDishka[EmailService],
) -> dict[str, int]:
    sent = await email_service.send_message(
        EmailMessage(subject="Welcome", body="Hello!", to=["user@example.com"]),
    )
    return {"sent": sent}

App state does not contain a permanently open EmailService. Use plugin.get_service(app.state) or config.get_service(app.state) when code outside handler DI needs a service derived from app state.

Construct Messages

subject and body are required constructor arguments. Recipient lists have empty-list defaults, so provide at least one delivery recipient before sending.

from litestar_email import EmailMessage

message = EmailMessage(
    subject="Monthly report",
    body="The report is attached.",
    from_email="Reports <reports@example.com>",
    to=["owner@example.com"],
    cc=["audit@example.com"],
    bcc=["archive@example.com"],
    reply_to=["support@example.com"],
    headers={"X-Campaign-ID": "monthly-report"},
)
message.attach(
    filename="report.pdf",
    content=b"report content",
    mimetype="application/pdf",
)
message.attach_alternative(
    content="<p>The report is attached.</p>",
    mimetype="text/html",
)

EmailMessage does not accept html_body or from_name. Put a per-message display name in from_email, as shown above. Use EmailMultiAlternatives.html_body for the HTML convenience constructor:

from litestar_email import EmailMultiAlternatives

message = EmailMultiAlternatives(
    subject="Welcome",
    body="Welcome to Example App.",
    to=["user@example.com"],
    html_body="<p>Welcome to <strong>Example App</strong>.</p>",
)

The message collections have these exact shapes:

FieldType
to, cc, bcc, reply_tolist[str]
headersdict[str, str]
attachmentslist[tuple[str, bytes, str]]
alternativeslist[tuple[str, str]]

recipients() returns to + cc + bcc; it does not include reply_to.

Backends, Lifecycle, Exceptions, and In-Memory Testing

Backend / UseConfigExtra
Console / Testsbackend="console" or backend="memory" (InMemoryBackend.outbox)None
SMTPbackend=SMTPConfig(host=..., port=..., use_tls=...)smtp
Resend / SendGrid / MailgunResendConfig / SendGridConfig / MailgunConfighttpx or aiohttp
Amazon SES (SendEmail Simple)SESConfig(...) (rejects MIME attachments with EmailDeliveryError)ses + httpx or aiohttp
CustomSubclass BaseEmailBackend or register with @email_backend(...)Project-specific

Inside async with config.provide_service() as mailer:, calls reuse one open backend; Provide(config.provide_service().__aiter__) scopes that lifecycle per request. Catch EmailRateLimitError, EmailAuthenticationError, and EmailConnectionError before EmailDeliveryError. Clear InMemoryBackend.outbox around tests. See Backends, SES, Exceptions, and In-Memory Testing for full config tables, SigV4 SES rules, HTTPTransport, exception hierarchy, and pytest fixtures.

<workflow>

Workflow

  1. Inspect the project's existing provider, network policy, and dependency extras. Keep its backend unless the user asks to migrate.
  2. Build one EmailConfig with the selected backend config and default sender.
  3. Register EmailPlugin(config=...) and wire handler DI with Provide(email_config.provide_service().__aiter__) + NamedDependency[EmailService] (or Dishka FromDishka[EmailService]).
  4. Construct EmailMessage with plain text. Add HTML through attach_alternative() or EmailMultiAlternatives.
  5. Load attachment bytes asynchronously, then call attach().
  6. Reuse a service context for batches. Let request-scoped DI manage service cleanup in handlers.
  7. Use backend="memory" in tests and clear InMemoryBackend.outbox between tests.
  8. For slow or retryable delivery, use the queue system already present in the project. Choose litestar-queues or litestar-saq only when it matches the existing stack.
</workflow><guardrails>

Guardrails

  • Do not pass html_body to EmailMessage; only EmailMultiAlternatives defines that field.
  • Do not import nonexistent symbols such as ConsoleConfig, InMemoryConfig, EmailAttachment, FallbackConfig, RateLimitConfig, or RetryConfig. Console and memory backends use backend="console" and backend="memory" (no config dataclass); attachments are (filename, content_bytes, mimetype) tuples added via attach() or attachments=[...].
  • Do not pass file paths as attachments. Pass (filename, content_bytes, mimetype) or call attach().
  • Do not pass a string to reply_to; pass list[str].
  • Do not annotate a route handler with mailer: NamedDependency[EmailService] without binding Provide(email_config.provide_service().__aiter__) (or a Dishka provider); EmailPlugin's default Provide(config.provide_service, sync_to_thread=False) returns AsyncServiceProvider.
  • Do not read app state as an open service by default. The plugin stores its EmailConfig there and derives services from it.
  • Do not configure a named API backend separately from its settings. Use backend=ResendConfig(...), backend=SendGridConfig(...), backend=MailgunConfig(...), or backend=SESConfig(...).
  • Do not send SES attachments. Select an attachment-capable backend.
  • Do not assume fail_silently=True suppresses every exception. Rate-limit failures (EmailRateLimitError) on HTTP API backends and SES authentication failures (EmailAuthenticationError) always propagate.
  • Do not hard-code API keys, SMTP passwords, or AWS credentials.
  • Do not force a provider migration. Match the project's deployed backend and operational constraints.
</guardrails><validation>

Validation

  • litestar-email>=0.4.0 and the selected backend extra are installed.
  • EmailPlugin(config=...) is registered.
  • The handler name matches email_service_dependency_key.
  • Handler injection binds Provide(email_config.provide_service().__aiter__) with NamedDependency[EmailService] (or uses Dishka FromDishka[EmailService]).
  • EmailMessage supplies subject, body, and a delivery recipient.
  • Attachments are byte triples and the selected backend supports them.
  • HTML content is stored in alternatives, not passed to EmailMessage.
  • SMTP TLS mode matches the server.
  • SES messages contain no attachments and contain text or HTML.
  • Batch sends reuse a managed service context.
  • Tests clear and assert InMemoryBackend.outbox.
  • Delivery exceptions are caught from most specific to least specific.
  • Secrets come from the project's settings layer.
</validation><example>

Example

from dataclasses import dataclass
from html import escape

from litestar import Litestar, post
from litestar.di import NamedDependency, Provide
from litestar.params import JSONBody
from litestar_email import (
    EmailConfig,
    EmailMessage,
    EmailPlugin,
    EmailService,
)

email_config = EmailConfig(
    backend="memory",
    from_email="notifications@example.com",
    from_name="Example App",
)


@dataclass
class Notification:
    recipient: str
    subject: str
    text: str


@post(
    "/notifications",
    dependencies={"mailer": Provide(email_config.provide_service().__aiter__)},
)
async def create_notification(
    data: JSONBody[Notification],
    mailer: NamedDependency[EmailService],
) -> dict[str, int]:
    message = EmailMessage(
        subject=data.subject,
        body=data.text,
        to=[data.recipient],
    )
    message.attach_alternative(
        content=f"<p>{escape(data.text)}</p>",
        mimetype="text/html",
    )
    return {"sent": await mailer.send_message(message)}


app = Litestar(
    route_handlers=[create_notification],
    plugins=[EmailPlugin(config=email_config)],
)
</example>

References Index

  • Backends, SES, Lifecycle, Exceptions, and In-Memory Testing — SMTP / Resend / SendGrid / Mailgun / SES configs, custom backends, SigV4 rules, service context reuse, exception hierarchy, and InMemoryBackend.outbox fixtures.
  • Litestar dependency injection
  • Litestar settings
  • Litestar Queues
  • Litestar SAQ
  • Litestar testing

Official References

Shared Styleguide Baseline

  • General Principles
  • Python
  • Litestar

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.