Expand description
Outgoing mail for id_effect services: SMTP, the SendGrid v3 API, templates, and delivery through
id_effect_jobs.
Address is a validated local@domain mailbox address: no whitespace or control characters,
a local part of at most Address::MAX_LOCAL_LEN bytes, a domain of non-empty dot-separated
labels, and at most Address::MAX_LEN bytes in all (RFC 5321).
use id_effect_mail::{Address, AddressError};
let from = Address::parse("billing@ledger.example").unwrap();
assert_eq!(from.to_string(), "billing@ledger.example");
assert_eq!(Address::parse("billing"), Err(AddressError::MissingAt));A Message is built and checked by Message::builder: sender, To / Cc / Bcc,
Reply-To, subject, text and HTML bodies, Attachments, InlineParts the HTML
references as cid:<content-id>, and extra headers. It is sent through the Mailer
capability (MailerService, deliver), provided by one of three layers:
SmtpMailer::layer: pooled SMTP over lettre (implicit TLS,STARTTLSor plaintext).SendGridMailer::layer: the SendGrid v3 API over theHttpClientcapability.TestMailbox::layer: records messages in memory, withassert_sent_to,assert_subject,assert_contains.
use id_effect::Exit;
use id_effect_mail::{Address, MailerEnv, Message, TestMailbox, deliver};
let message = Message::builder()
.from(Address::parse("billing@ledger.example").unwrap())
.to(Address::parse("carol@ledger.example").unwrap())
.subject("Your invoice")
.text("Your invoice is attached.")
.build()
.unwrap();
let mailbox = TestMailbox::new();
let program = deliver::<MailerEnv, _>(message).provide_layer(mailbox.layer());
assert!(matches!(program.block_on(), Exit::Success(_)));
mailbox.assert_sent_to("carol@ledger.example");
mailbox.assert_subject("Your invoice");Templates renders a mail’s subject, text and HTML bodies from minijinja templates with
shared layouts and partials: HTML autoescaped, a missing variable an error
(TemplateError), the context any Serialize value. With feature preview,
MailPreviews serves registered previews at /_mail/previews (HTML, text and raw MIME),
re-reading the templates from disk on every request in a debug build.
use id_effect_mail::{Address, Templates};
let templates = Templates::new()
.with_template("layout.txt", "{% block body %}{% endblock %}\n-- Ledger").unwrap()
.with_template("receipt.subject.txt", "Receipt {{ number }}").unwrap()
.with_template(
"receipt.txt",
r#"{% extends "layout.txt" %}{% block body %}Paid: {{ amount }}{% endblock %}"#,
)
.unwrap();
let message = templates
.message("receipt", &serde_json::json!({ "number": 7, "amount": "12.00 EUR" }))
.unwrap()
.from(Address::parse("billing@ledger.example").unwrap())
.to(Address::parse("carol@ledger.example").unwrap())
.build()
.unwrap();
assert_eq!(message.subject(), "Receipt 7");
assert_eq!(message.text(), Some("Paid: 12.00 EUR\n-- Ledger"));Neither transport retries a send by itself; a refused send says whether a later attempt may
succeed (MailError::is_transient). With feature jobs, deliver_later enqueues a message
as a DeliverMail job of id_effect_jobs (deliver_later_in with the caller’s SQL
transaction, so it is sent only if that commits), and register_mail_jobs adds the handler
that sends it through the Mailer: a transient failure is retried with backoff, any other
makes the job dead with the error recorded.
With feature auth, AccountMails implements id_effect_auth’s AccountNotifier: the
verification and password reset mails, rendered from templates and sent directly through the
Mailer with a short retry. They never go through deliver_later, whose job payload would
keep a usable token in the jobs table (see the auth module).
Tier platform (ADR 0010 Amendment 6): the Ledger reference app under
examples/reference/ depends on it.
Re-exports§
pub use auth::AccountMails;
Modules§
- auth
AccountMails(featureauth):id_effect_auth’sAccountNotifier, rendering the verification and password reset mails fromTemplatesand sending them through theMailer.
Structs§
- Address
- A validated
local@domainmailbox address. - Attachment
- A file attached to a message (
Content-Disposition: attachment). - Deliver
Mail - A
Messageto send in the background: the jobdeliver_laterenqueues and the handlerregister_mail_jobsregisters sends through theMailerService. - Inline
Part - A part shown inside the HTML body (
Content-Disposition: inline), such as an image the HTML references as<img src="cid:logo">. - Mail
Previews - Registered previews over a
Templatesregistry: each a name, the mail it renders and the fixture context it renders with. - Mailbox
- A mailbox: an
Addresswith an optional display name (Carol <carol@ledger.example>). - Mailer
Service - The
Mailercapability: a shared handle to the installed implementation. - Message
- An outgoing message, valid by construction (
Message::builder). - Message
Builder - Builds a
Message;buildchecks it. - Rendered
Mail - A rendered mail: whichever of the subject, text and HTML templates exist.
- Send
Grid Config - Settings for
SendGridMailer. - Send
Grid Mailer - A
Mailerover the SendGrid v3 API. Cloning shares the configuration and HTTP client. - Sent
- What a transport reports for an accepted message.
- Smtp
Config - Connection settings for
SmtpMailer. - Smtp
Mailer - A
Mailerover a pooled SMTP connection. Cloning shares the pool. - Templates
- A registry of mail templates: added from strings (
Templates::with_template) or read from a directory (Templates::from_dir). Cloning shares the registry. - Test
Mailbox - An in-memory
Mailerfor tests. Cloning shares the mailbox.
Enums§
- Address
Error - Why a string is not a valid
Address. - Mail
Error - Why a message was not sent.
- Smtp
Security - How an SMTP connection is secured.
- Template
Error - Why a mail did not render.
Constants§
- PREVIEWS_
PATH - Where
MailPreviews::routerserves: the index at/_mail/previews, a preview at/_mail/previews/{name}, its views at/_mail/previews/{name}/{html,text,raw}.
Traits§
- Mailer
- Capability: sends one
Messageas anEffect. Implemented bySmtpMailer,SendGridMailerandTestMailbox.
Functions§
- deliver
- Sends
messagewith theMailerServiceinR. - deliver_
later - Enqueues
messageas aDeliverMailjob on theJobQueueServiceinR. Fails withJobError::Encodewhen its content is too large (DeliverMail::new). For a delay or a unique key, enqueueDeliverMail::new(message)?withJobOptionsyourself. - deliver_
later_ in - Enqueues
messageas aDeliverMailjob with the caller’s transactiontx(enqueue_in): it is sent only iftxcommits.clockis the queue’s clock. - mailer_
env - A
MailerEnvwrappingmailer. - provide_
mailer - A
Layerprovidingmaileras theMailerService. - register_
mail_ jobs - Registers the
DeliverMailhandler inregistry: it sends the message with theMailerServiceinR, so the worker’s environment holds both it and theJobQueueService. A transient failure retries, any other discards (theFrom<MailError>impl ofJobFailure). Fails withJobError::DuplicateJobwhen the registry already has it.
Type Aliases§
- Mailer
Env - The minimal typed environment holding only
MailerService(the output of the mailer layers).