| Crates.io | torii-mailer |
| lib.rs | torii-mailer |
| version | 0.5.1 |
| created_at | 2025-07-18 04:15:12.102009+00 |
| updated_at | 2025-07-23 00:20:07.280489+00 |
| description | Pluggable email system for the torii authentication ecosystem |
| homepage | |
| repository | https://github.com/cmackenzie1/torii-rs |
| max_upload_size | |
| id | 1758584 |
| size | 103,358 |
A pluggable email system for the Torii authentication ecosystem, built on top of lettre and askama.
use torii_mailer::prelude::*;
// Configure from environment variables
let config = MailerConfig::from_env()?;
let transport = config.build_transport()?;
let engine = AskamaTemplateEngine::new();
// Create template context
let context = TemplateContext {
app_name: "My App".to_string(),
app_url: "https://myapp.com".to_string(),
user_name: Some("John Doe".to_string()),
user_email: Some("john@example.com".to_string()),
};
// Build and send a magic link email
let email = MagicLinkEmail::build(
&engine,
&config.get_from_address(),
"john@example.com",
"https://myapp.com/auth/magic/abc123",
context
).await?;
transport.send_email(email).await?;
Configure the mailer using environment variables:
MAILER_SMTP_HOST=smtp.gmail.com
MAILER_SMTP_PORT=587
MAILER_SMTP_USERNAME=your-email@gmail.com
MAILER_SMTP_PASSWORD=your-app-password
MAILER_SMTP_TLS=starttls # none, starttls, tls
MAILER_FILE_OUTPUT_DIR=./emails
MAILER_SENDMAIL=true
MAILER_SENDMAIL_COMMAND=/usr/sbin/sendmail # optional
MAILER_FROM_ADDRESS=noreply@myapp.com
MAILER_FROM_NAME="My App"
MAILER_APP_NAME="My App"
MAILER_APP_URL=https://myapp.com
The crate provides pre-built email types for common authentication flows:
Templates are built using askama and can be customized by implementing your own template engine or extending the existing ones.
All built-in templates are responsive and follow email best practices:
You can create custom templates by implementing the TemplateEngine trait or by extending the AskamaTemplateEngine.
For local development, use the file transport which saves emails as .eml files:
let config = MailerConfig {
transport: TransportConfig::File {
output_dir: PathBuf::from("./emails"),
},
// ... other config
};
You can then open the .eml files in your email client to preview the emails.
The mailer integrates seamlessly with torii-core services:
use torii_core::services::MagicLinkService;
use torii_mailer::prelude::*;
// In your magic link service
let config = MailerConfig::from_env()?;
let transport = config.build_transport()?;
let engine = AskamaTemplateEngine::new();
// Generate magic link token
let token = magic_link_service.generate_token("user@example.com").await?;
// Send email
let context = TemplateContext {
app_name: config.app_name.clone(),
app_url: config.app_url.clone(),
user_email: Some("user@example.com".to_string()),
user_name: None,
};
let email = MagicLinkEmail::build(
&engine,
&config.get_from_address(),
"user@example.com",
&format!("{}/auth/magic/{}", config.app_url, token.token),
context
).await?;
transport.send_email(email).await?;