Skip to main content

Module email

Module email 

Source
Expand description

Email backends — email::Mailer trait + console/in-memory/null backends. Email backend layer — pluggable async email sending.

§Quick start

use rustango::email::{Mailer, Email, ConsoleMailer};
use std::sync::Arc;

let mailer: Arc<dyn Mailer> = Arc::new(ConsoleMailer::default());

let email = Email::new()
    .to("user@example.com")
    .from("noreply@my-app.com")
    .subject("Welcome!")
    .body("Thanks for signing up.");
mailer.send(&email).await?;

§Backends

BackendWhen to use
[ConsoleMailer]Development — prints emails to stdout. Default.
[InMemoryMailer]Tests — captures emails into a Vec for assertions.
[FileMailer]Dev / staging — writes one .eml per send under a directory.
[NullMailer]Production guardrail — accepts and discards every send (CI / disabled mail).
[SmtpMailer]Production — async lettre + rustls SMTP relay (email-smtp feature).

§Plug your own

Implement Mailer for any third-party transport (SES, SendGrid, Postmark):

use rustango::email::{Mailer, Email, MailError};
use async_trait::async_trait;

pub struct SesMailer { /* ... */ }

#[async_trait]
impl Mailer for SesMailer {
    async fn send(&self, email: &Email) -> Result<(), MailError> {
        // POST to AWS SES, etc.
        Ok(())
    }
}

Structs§

Attachment
Django-parity EmailMessage.attach(filename, content, mimetype) — one attached blob. mimetype is the MIME type lettre stamps on the SinglePart; when None we default to application/octet-stream (RFC-9110 recommendation for opaque blobs).
ConsoleMailer
Development mailer — prints emails to stdout instead of sending.
Email
One outbound email. Use the builder methods to assemble.
FileMailer
Development / debug mailer — writes each outgoing email to a timestamped .eml file in a configured directory instead of sending it.
InMemoryMailer
Test mailer — captures every sent email into a shared Vec for assertions.
NullMailer
Discards all emails. Useful for disabling email sending in environments (e.g. CI) without changing call sites.

Enums§

MailError

Traits§

Mailer
Pluggable async email backend.

Functions§

formataddr
Django-parity email.utils.formataddr((name, address)) — format an RFC 5322 address with display name. Returns "Display Name <email@example.com>" when name is provided, or just address when name is None or empty.
from_settings
mail_admins
Django-shape mail_admins(subject, message) — sends to the addresses configured in MailSettings.admins. Returns Ok(0) when the list is empty (a no-op without warning, matching Django’s “no ADMINS → silent skip” behavior). Issue #416.
mail_managers
Django-shape mail_managers(subject, message) — sends to the addresses configured in MailSettings.managers. Same shape as mail_admins; subject is prefixed with "[manager] ". Issue #416.
parseaddr
Django-parity inverse of formataddr — Python’s email.utils.parseaddr(address). Splits a "Display Name <user@example.com>" style header value into (name, address).
send_mail
Build a BoxedMailer from a loaded crate::config::MailSettings section (#87 wiring, v0.29).
send_many
Django-shape send_mass_mail(datatuple) — bulk-send a batch of messages. datatuple in Django is [(subject, message, from, [to, ...]), ...]; rustango takes a slice of pre-built Emails, which is the more idiomatic Rust shape and avoids per-tuple boilerplate.

Type Aliases§

BoxedMailer
Arc<dyn Mailer> alias.