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
| Backend | When 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.mimetypeis the MIME type lettre stamps on the SinglePart; whenNonewe default toapplication/octet-stream(RFC-9110 recommendation for opaque blobs). - Console
Mailer - Development mailer — prints emails to stdout instead of sending.
- One outbound email. Use the builder methods to assemble.
- File
Mailer - Development / debug mailer — writes each outgoing email to a
timestamped
.emlfile in a configured directory instead of sending it. - InMemory
Mailer - Test mailer — captures every sent email into a shared
Vecfor assertions. - Null
Mailer - Discards all emails. Useful for disabling email sending in environments (e.g. CI) without changing call sites.
Enums§
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>"whennameis provided, or justaddresswhennameisNoneor empty. - from_
settings - mail_
admins - Django-shape
mail_admins(subject, message)— sends to the addresses configured inMailSettings.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 inMailSettings.managers. Same shape asmail_admins; subject is prefixed with"[manager] ". Issue #416. - parseaddr
- Django-parity inverse of
formataddr— Python’semail.utils.parseaddr(address). Splits a"Display Name <user@example.com>"style header value into(name, address). - send_
mail - Build a
BoxedMailerfrom a loadedcrate::config::MailSettingssection (#87 wiring, v0.29). - send_
many - Django-shape
send_mass_mail(datatuple)— bulk-send a batch of messages.datatuplein Django is[(subject, message, from, [to, ...]), ...]; rustango takes a slice of pre-builtEmails, which is the more idiomatic Rust shape and avoids per-tuple boilerplate.
Type Aliases§
- Boxed
Mailer Arc<dyn Mailer>alias.