Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mail

A message names a template and its variables, or carries its own subject and body; send queues it and a worker delivers it through the configured driver.

use laterite_core::mail::Message;

ctx.mail()
    .send(
        Message::template("acme.blog::welcome", serde_json::json!({ "name": "Ada", "link": url }))
            .to("Ada <ada@acme.example>"),
    )
    .await?;
MethodDoes
Message::template(code, vars)A message rendered from the template code.
Message::raw(subject, text, html)A message with its own body; html optional.
.to(addr), .cc(addr), .bcc(addr), .reply_to(addr), .from(addr)Addresses, as Name <address> or bare.
.attach(filename, content_type, bytes)A file.
mail.send(message)Queues it; Pushed::Ran in inline mode.
mail.send_now(message)Renders and delivers here, returning the driver’s error.
mail.envelope(message)What the driver would deliver, rendered and addressed; nothing is sent.
mail.send_test(addr)The built-in test template, delivered here.

Register a template

use laterite_core::mail::MailRegistry;

fn register(&self, registry: &mut Registry) {
    registry.add_mail_template("acme.blog::welcome", include_str!("../mail/welcome.htm"));
    registry.add_mail_layout("acme", include_str!("../mail/layouts/acme.htm"));
    registry.add_mail_partial("signature", include_str!("../mail/partials/signature.htm"));
}
---
subject: Welcome, {{ name }}
layout: default
description: Sent when an account is made.
---
# Hello {{ name }}

Your account on **{{ app_name }}** is ready.

{% set body %}Sign in{% endset %}
{{ partial("button", url=link, body=body) }}
PartHolds
Front mattersubject, layout (default unless set), description; a layout or partial has name.
BodyA template’s body, written as Markdown; a layout’s or partial’s, as HTML.

The plain-text part is made from the body, with each partial’s text form. A message that needs its own text part registers a .txt beside the .htm:

registry.add_mail_template_with_text(
    "acme.blog::welcome",
    include_str!("../mail/welcome.htm"),
    include_str!("../mail/welcome.txt"),
);
Welcome {{ name }}.

{{ partial("button", url=link, body="Sign in") }}

A layout’s text wrapper and a partial’s text form register the same way (add_mail_layout_with_text, add_mail_partial_with_text). A layout’s CSS goes in a <style> element in its HTML, reading the brand as {{ brand.button_bg }}.

The older form, a key = "value" header and sections split by a line of == (text, HTML, CSS), still reads and is logged as deprecated.

A layout receives content, subject, app_name, css and brand_css. An undefined variable renders empty. A template edited in the admin keeps its edit until it is reset to the file.

TemplateSent by
laterite::testThe Mail settings’ Send test and lat mail test.
laterite.auth::inviteCreating an operator; name, username, link, expires.
laterite.auth::resetThe Forgot your password page; name, username, link, expires.
PartialArguments
buttonurl, body
panel, table, subcopybody, Markdown
header, footerbody, url (header only)

Configure the transport

# config/default.toml
[mail]
driver = "smtp"            # smtp | log | stub | a registered driver
from = "Acme <no-reply@acme.example>"
# to = "dev@acme.example"  # every message goes here while set

[mail.smtp]
host = "smtp.acme.example"
port = 587
encryption = "starttls"    # starttls | tls | none
username = "apikey"
password = ""              # or LAT__MAIL__SMTP__PASSWORD
timeout_seconds = 30
KeyDescription
mail.driversmtp (the smtp feature of laterite-core, on in lateritecmf), log (kept in the mail log, nothing leaves), stub (kept in memory) or a driver a module registered. Default log.
mail.fromThe sender. Default Laterite <no-reply@localhost>.
mail.toA development override: every message goes to this address instead of its recipients.
mail.smtp.*The server, port, encryption, credentials and timeout.

The Mail settings under Settings → System → Mail lay over these: a blank field keeps the configured value, the password is sealed under the application key, and a save switches the transport at once. Gated by backend.manage_mail.

lat mail test ada@acme.example

Send a test message to me on the settings screen mails the signed-in operator the built-in laterite::test template and shows the driver’s error when it fails.

Edit templates in the admin

# a role's permissions
- backend.manage_mail_templates
- backend.manage_mail
PermissionGrants
backend.manage_mail_templatesSettings → System → Mail Templates: the templates, layouts and partials with a preview, a test send to the signed-in operator, Reset to the file, Delete for one no file registers, and New; Mail Branding: the colours every layout reads, with a preview on the test template.
backend.manage_mailMail (the sender and transport, Send a test message) and Mail Log: the messages the log driver kept, each as text and as HTML, with Clear.
Brand colourLayout property
Page background, content background, panel backgroundbrand.body_bg, brand.content_bg, brand.panel_bg
Header text, headings, text, muted text, footer textbrand.header_color, brand.heading_color, brand.text_color, brand.muted_color, brand.footer_color
Links, button, button text, bordersbrand.link_color, brand.button_bg, brand.button_text_color, brand.border_color

Register a driver

use laterite_core::mail::{MailDriverReg, MailRegistry, Mailer};

fn register(&self, registry: &mut Registry) {
    registry.add_mail_driver(MailDriverReg::new("acme-http", |config, _db| {
        Ok(Arc::new(AcmeHttp::new(&config.smtp.username)))
    }));
}

A driver implements Mailer: a name() and deliver(&Envelope); mail.driver = "acme-http" or the settings screen selects it.