pub struct Mailer { /* private fields */ }Expand description
The primary entry point for sending transactional email.
Mailer loads Markdown templates, performs variable substitution, renders
HTML and plain-text bodies, applies a layout, and delivers the resulting
message over SMTP.
Cloning is cheap (Arc-based) and shares the SMTP connection, template
source, and preloaded layouts.
§Construction
Mailer::new— uses aFileSource(optionally cached) derived fromEmailConfig::templates_path.Mailer::with_source— accepts any customTemplateSource.Mailer::with_stub_transport— in-memory stub for tests (requires feature"test-helpers"or#[cfg(test)]).
Implementations§
Source§impl Mailer
impl Mailer
Sourcepub fn new(config: &EmailConfig) -> Result<Self>
pub fn new(config: &EmailConfig) -> Result<Self>
Create a new Mailer with the default FileSource.
If config.cache_templates is true, the file source is wrapped in a
CachedSource with config.template_cache_size capacity.
§Errors
Returns an error if the SMTP transport cannot be built (e.g., invalid host, mismatched credentials) or if the layouts directory cannot be read.
Sourcepub fn with_source(
config: &EmailConfig,
source: Arc<dyn TemplateSource>,
) -> Result<Self>
pub fn with_source( config: &EmailConfig, source: Arc<dyn TemplateSource>, ) -> Result<Self>
Create a new Mailer with a custom TemplateSource.
Use this to supply an in-memory source, a database-backed source, or any other custom implementation.
§Errors
Returns an error if the SMTP transport cannot be built or if the layouts directory cannot be read.
Sourcepub fn render(&self, email: &SendEmail) -> Result<RenderedEmail>
pub fn render(&self, email: &SendEmail) -> Result<RenderedEmail>
Render a template without sending.
Performs variable substitution, parses the YAML frontmatter, converts the Markdown body to HTML (with button syntax support), applies the layout, and generates the plain-text fallback.
Returns a RenderedEmail containing the subject, HTML, and text.
§Errors
Returns an error if the template cannot be loaded, the frontmatter is missing or malformed, or the requested layout is not found.
Sourcepub async fn send(&self, email: SendEmail) -> Result<()>
pub async fn send(&self, email: SendEmail) -> Result<()>
Render and send an email via SMTP.
Calls Self::render internally, then builds a multipart/alternative
MIME message (text/plain + text/html) and delivers it over the
configured transport.
§Errors
Returns an error if the recipient list is empty, if any address is malformed, if the template cannot be rendered, or if the SMTP delivery fails.