Skip to main content

Module email_templates

Module email_templates 

Source
Expand description

Tera-rendered email helpers — bridge crate::email mailers and the Tera templating engine. Each email = <name>.subject.txt + <name>.txt + optional <name>.html. See email_templates::EmailRenderer. Tera-rendered email helpers — bridge the existing crate::email mailer trait and the Tera templating engine (already a dep on the admin feature).

Each email is a set of three sibling templates in a directory:

email_templates/
  welcome.subject.txt   -- single-line subject (whitespace trimmed)
  welcome.txt           -- plain-text body  (always required)
  welcome.html          -- HTML body        (optional)

Subject + plain text are required; HTML is added when present so mail clients that prefer HTML get it and plain-text fallbacks still work.

§Quick start

ⓘ
use rustango::email_templates::EmailRenderer;
use rustango::email::Mailer;
use tera::Context;

let renderer = EmailRenderer::from_dir("./email_templates")?;

let mut ctx = Context::new();
ctx.insert("name", "Alice");
ctx.insert("url", "https://app.example.com/verify/abc");

let email = renderer.render("welcome", &ctx)?
    .from("noreply@example.com")
    .to("alice@example.com");

mailer.send(email).await?;

§Inline templates (tests + ad-hoc)

ⓘ
let renderer = EmailRenderer::from_pairs(vec![
    ("welcome.subject.txt", "Welcome, {{ name }}"),
    ("welcome.txt",          "Hi {{ name }}, click {{ url }} to verify."),
    ("welcome.html",         "<p>Hi {{ name }}, <a href=\"{{ url }}\">verify</a>.</p>"),
])?;

Structs§

EmailRenderer
Holds a Tera engine plus per-instance rendering helpers.

Enums§

EmailRenderError