email: pass template_dir in config

This commit is contained in:
Philippe Loctaux 2023-07-14 16:38:08 +02:00
parent 8657c1198d
commit cd7a894334
5 changed files with 66 additions and 49 deletions

View file

@ -37,13 +37,12 @@ pub enum Error {
const TEMPLATE_EXT: &str = ".mjml.tera"; const TEMPLATE_EXT: &str = ".mjml.tera";
pub fn render_template<C: Serialize>(name: &str, content: C) -> Result<String, Error> { pub fn render_template<C: Serialize>(
let base_dir = if cfg!(debug_assertions) { name: &str,
format!("{}/templates", env!("CARGO_MANIFEST_DIR")) template_dir: &str,
} else { content: C,
"./email-templates".into() ) -> Result<String, Error> {
}; let templates = format!("{template_dir}/**/*{TEMPLATE_EXT}");
let templates = format!("{base_dir}/**/*{TEMPLATE_EXT}",);
// Initialize tera templates // Initialize tera templates
let tera = Tera::new(&templates)?; let tera = Tera::new(&templates)?;
@ -75,6 +74,7 @@ pub enum Transport {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Config { pub struct Config {
pub template_dir: String,
pub from: String, pub from: String,
pub transport: Transport, pub transport: Transport,
pub host: String, pub host: String,

View file

@ -8,6 +8,7 @@ url = "../../database/ezidam.sqlite"
# remove this section to disable sending email # remove this section to disable sending email
[default.email] [default.email]
template_dir = "../email/templates"
from = "ezidam <ezidam@mail.local>" from = "ezidam <ezidam@mail.local>"
transport = "unencrypted" transport = "unencrypted"
host = "localhost" host = "localhost"

View file

@ -11,8 +11,8 @@ pub fn init(config: &Figment) -> Result<Option<Config>, String> {
} }
Err(e) => Err(format!("Invalid email configuration: {e}")), Err(e) => Err(format!("Invalid email configuration: {e}")),
}, },
Err(_) => { Err(e) => {
println!("Sending emails is disabled"); println!("Sending emails has been disabled: {e}");
Ok(None) Ok(None)
} }
} }

View file

@ -327,8 +327,21 @@ pub async fn admin_users_password_reset(
user_timezone: user.timezone().into(), user_timezone: user.timezone().into(),
}; };
// Get email config
let email_config = match email_config.inner() {
Some(email_config) => email_config,
None => {
return Ok(Flash::new(
Redirect::to(uri!(admin_users_view(id))),
FlashKind::Warning,
"Email sending is disabled.".to_string(),
));
}
};
// Render email template // Render email template
let mjml = match email::render_template("password-reset", &content) { let mjml = match email::render_template("password-reset", &email_config.template_dir, &content)
{
Ok(mjml) => mjml, Ok(mjml) => mjml,
Err(e) => { Err(e) => {
return Ok(Flash::new( return Ok(Flash::new(
@ -359,8 +372,7 @@ pub async fn admin_users_password_reset(
}; };
// Send email // Send email
let (flash_kind, flash_message) = match email_config.inner() { let (flash_kind, flash_message) =
Some(email_config) => {
match email::send_email(email_config, &user_for_email, email_title, html).await { match email::send_email(email_config, &user_for_email, email_title, html).await {
Ok(okay) => { Ok(okay) => {
if okay.is_positive() { if okay.is_positive() {
@ -376,9 +388,6 @@ pub async fn admin_users_password_reset(
} }
} }
Err(e) => (FlashKind::Danger, e.to_string()), Err(e) => (FlashKind::Danger, e.to_string()),
}
}
None => (FlashKind::Warning, "Email sending is disabled".into()),
}; };
Ok(Flash::new( Ok(Flash::new(

View file

@ -146,8 +146,22 @@ pub async fn forgot_password_email_form(
user_timezone: user.timezone().into(), user_timezone: user.timezone().into(),
}; };
// Get email config
let email_config = match email_config.inner() {
Some(email_config) => email_config,
None => {
return Ok(Flash::new(
Redirect::to(uri!(forgot_password_page)),
FlashKind::Warning,
"Email sending is disabled. Contact your administrator to reset your password."
.to_string(),
));
}
};
// Render email template // Render email template
let mjml = match email::render_template("password-reset", &content) { let mjml = match email::render_template("password-reset", &email_config.template_dir, &content)
{
Ok(mjml) => mjml, Ok(mjml) => mjml,
Err(e) => { Err(e) => {
return Ok(Flash::new( return Ok(Flash::new(
@ -178,8 +192,7 @@ pub async fn forgot_password_email_form(
}; };
// Send email // Send email
let (flash_kind, flash_message) = match email_config.inner() { let (flash_kind, flash_message) =
Some(email_config) => {
match email::send_email(email_config, &user_for_email, email_title, html).await { match email::send_email(email_config, &user_for_email, email_title, html).await {
Ok(okay) => { Ok(okay) => {
if okay.is_positive() { if okay.is_positive() {
@ -192,12 +205,6 @@ pub async fn forgot_password_email_form(
} }
} }
Err(e) => (FlashKind::Danger, e.to_string()), Err(e) => (FlashKind::Danger, e.to_string()),
}
}
None => (
FlashKind::Warning,
"Email sending is disabled. Contact your administrator to reset your password.".into(),
),
}; };
Ok(Flash::new( Ok(Flash::new(