diff --git a/crates/email/src/lib.rs b/crates/email/src/lib.rs index 07cef0b..dca963a 100644 --- a/crates/email/src/lib.rs +++ b/crates/email/src/lib.rs @@ -37,13 +37,12 @@ pub enum Error { const TEMPLATE_EXT: &str = ".mjml.tera"; -pub fn render_template(name: &str, content: C) -> Result { - let base_dir = if cfg!(debug_assertions) { - format!("{}/templates", env!("CARGO_MANIFEST_DIR")) - } else { - "./email-templates".into() - }; - let templates = format!("{base_dir}/**/*{TEMPLATE_EXT}",); +pub fn render_template( + name: &str, + template_dir: &str, + content: C, +) -> Result { + let templates = format!("{template_dir}/**/*{TEMPLATE_EXT}"); // Initialize tera templates let tera = Tera::new(&templates)?; @@ -75,6 +74,7 @@ pub enum Transport { #[derive(Debug, Deserialize)] pub struct Config { + pub template_dir: String, pub from: String, pub transport: Transport, pub host: String, diff --git a/crates/ezidam/ezidam.toml b/crates/ezidam/ezidam.toml index e0d79cc..4b3664e 100644 --- a/crates/ezidam/ezidam.toml +++ b/crates/ezidam/ezidam.toml @@ -8,6 +8,7 @@ url = "../../database/ezidam.sqlite" # remove this section to disable sending email [default.email] +template_dir = "../email/templates" from = "ezidam " transport = "unencrypted" host = "localhost" diff --git a/crates/ezidam/src/email.rs b/crates/ezidam/src/email.rs index 0fd9bab..cd47150 100644 --- a/crates/ezidam/src/email.rs +++ b/crates/ezidam/src/email.rs @@ -11,8 +11,8 @@ pub fn init(config: &Figment) -> Result, String> { } Err(e) => Err(format!("Invalid email configuration: {e}")), }, - Err(_) => { - println!("Sending emails is disabled"); + Err(e) => { + println!("Sending emails has been disabled: {e}"); Ok(None) } } diff --git a/crates/ezidam/src/routes/admin/users.rs b/crates/ezidam/src/routes/admin/users.rs index f1337c4..5cd77b5 100644 --- a/crates/ezidam/src/routes/admin/users.rs +++ b/crates/ezidam/src/routes/admin/users.rs @@ -327,8 +327,21 @@ pub async fn admin_users_password_reset( 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 - 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, Err(e) => { return Ok(Flash::new( @@ -359,27 +372,23 @@ pub async fn admin_users_password_reset( }; // Send email - let (flash_kind, flash_message) = match email_config.inner() { - Some(email_config) => { - match email::send_email(email_config, &user_for_email, email_title, html).await { - Ok(okay) => { - if okay.is_positive() { - ( - FlashKind::Success, - format!("Email is on it's way to {email}"), - ) - } else { - ( - FlashKind::Warning, - "Email should be on it's way, but it might not arrive".into(), - ) - } + let (flash_kind, flash_message) = + match email::send_email(email_config, &user_for_email, email_title, html).await { + Ok(okay) => { + if okay.is_positive() { + ( + FlashKind::Success, + format!("Email is on it's way to {email}"), + ) + } else { + ( + FlashKind::Warning, + "Email should be on it's way, but it might not arrive".into(), + ) } - Err(e) => (FlashKind::Danger, e.to_string()), } - } - None => (FlashKind::Warning, "Email sending is disabled".into()), - }; + Err(e) => (FlashKind::Danger, e.to_string()), + }; Ok(Flash::new( Redirect::to(uri!(admin_users_view(id))), diff --git a/crates/ezidam/src/routes/root/forgot_password.rs b/crates/ezidam/src/routes/root/forgot_password.rs index 7bcc85f..f4893bf 100644 --- a/crates/ezidam/src/routes/root/forgot_password.rs +++ b/crates/ezidam/src/routes/root/forgot_password.rs @@ -146,8 +146,22 @@ pub async fn forgot_password_email_form( 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 - 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, Err(e) => { return Ok(Flash::new( @@ -178,27 +192,20 @@ pub async fn forgot_password_email_form( }; // Send email - let (flash_kind, flash_message) = match email_config.inner() { - Some(email_config) => { - match email::send_email(email_config, &user_for_email, email_title, html).await { - Ok(okay) => { - if okay.is_positive() { - (FlashKind::Success, SUCCESS_MESSAGE.to_string()) - } else { - ( - FlashKind::Warning, - "Email should be on it's way, but it might not arrive".into(), - ) - } + let (flash_kind, flash_message) = + match email::send_email(email_config, &user_for_email, email_title, html).await { + Ok(okay) => { + if okay.is_positive() { + (FlashKind::Success, SUCCESS_MESSAGE.to_string()) + } else { + ( + FlashKind::Warning, + "Email should be on it's way, but it might not arrive".into(), + ) } - Err(e) => (FlashKind::Danger, e.to_string()), } - } - None => ( - FlashKind::Warning, - "Email sending is disabled. Contact your administrator to reset your password.".into(), - ), - }; + Err(e) => (FlashKind::Danger, e.to_string()), + }; Ok(Flash::new( Redirect::to(uri!(forgot_password_page)),