Skip to main content

Crate id_effect_mail

Crate id_effect_mail 

Source
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:

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 (feature auth): id_effect_auth’s AccountNotifier, rendering the verification and password reset mails from Templates and sending them through the Mailer.

Structs§

Address
A validated local@domain mailbox address.
Attachment
A file attached to a message (Content-Disposition: attachment).
DeliverMail
A Message to send in the background: the job deliver_later enqueues and the handler register_mail_jobs registers sends through the MailerService.
InlinePart
A part shown inside the HTML body (Content-Disposition: inline), such as an image the HTML references as <img src="cid:logo">.
MailPreviews
Registered previews over a Templates registry: each a name, the mail it renders and the fixture context it renders with.
Mailbox
A mailbox: an Address with an optional display name (Carol <carol@ledger.example>).
MailerService
The Mailer capability: a shared handle to the installed implementation.
Message
An outgoing message, valid by construction (Message::builder).
MessageBuilder
Builds a Message; build checks it.
RenderedMail
A rendered mail: whichever of the subject, text and HTML templates exist.
SendGridConfig
Settings for SendGridMailer.
SendGridMailer
A Mailer over the SendGrid v3 API. Cloning shares the configuration and HTTP client.
Sent
What a transport reports for an accepted message.
SmtpConfig
Connection settings for SmtpMailer.
SmtpMailer
A Mailer over 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.
TestMailbox
An in-memory Mailer for tests. Cloning shares the mailbox.

Enums§

AddressError
Why a string is not a valid Address.
MailError
Why a message was not sent.
SmtpSecurity
How an SMTP connection is secured.
TemplateError
Why a mail did not render.

Constants§

PREVIEWS_PATH
Where MailPreviews::router serves: 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 Message as an Effect. Implemented by SmtpMailer, SendGridMailer and TestMailbox.

Functions§

deliver
Sends message with the MailerService in R.
deliver_later
Enqueues message as a DeliverMail job on the JobQueueService in R. Fails with JobError::Encode when its content is too large (DeliverMail::new). For a delay or a unique key, enqueue DeliverMail::new(message)? with JobOptions yourself.
deliver_later_in
Enqueues message as a DeliverMail job with the caller’s transaction tx (enqueue_in): it is sent only if tx commits. clock is the queue’s clock.
mailer_env
A MailerEnv wrapping mailer.
provide_mailer
A Layer providing mailer as the MailerService.
register_mail_jobs
Registers the DeliverMail handler in registry: it sends the message with the MailerService in R, so the worker’s environment holds both it and the JobQueueService. A transient failure retries, any other discards (the From<MailError> impl of JobFailure). Fails with JobError::DuplicateJob when the registry already has it.

Type Aliases§

MailerEnv
The minimal typed environment holding only MailerService (the output of the mailer layers).