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";
pub fn render_template<C: Serialize>(name: &str, content: C) -> Result<String, Error> {
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<C: Serialize>(
name: &str,
template_dir: &str,
content: C,
) -> Result<String, Error> {
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,

View file

@ -8,6 +8,7 @@ url = "../../database/ezidam.sqlite"
# remove this section to disable sending email
[default.email]
template_dir = "../email/templates"
from = "ezidam <ezidam@mail.local>"
transport = "unencrypted"
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(_) => {
println!("Sending emails is disabled");
Err(e) => {
println!("Sending emails has been disabled: {e}");
Ok(None)
}
}

View file

@ -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 <code>{email}</code>"),
)
} 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 <code>{email}</code>"),
)
} 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))),

View file

@ -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)),