email: pass template_dir in config
This commit is contained in:
parent
8657c1198d
commit
cd7a894334
5 changed files with 66 additions and 49 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,8 +372,7 @@ pub async fn admin_users_password_reset(
|
|||
};
|
||||
|
||||
// Send email
|
||||
let (flash_kind, flash_message) = match email_config.inner() {
|
||||
Some(email_config) => {
|
||||
let (flash_kind, flash_message) =
|
||||
match email::send_email(email_config, &user_for_email, email_title, html).await {
|
||||
Ok(okay) => {
|
||||
if okay.is_positive() {
|
||||
|
|
@ -376,9 +388,6 @@ pub async fn admin_users_password_reset(
|
|||
}
|
||||
}
|
||||
Err(e) => (FlashKind::Danger, e.to_string()),
|
||||
}
|
||||
}
|
||||
None => (FlashKind::Warning, "Email sending is disabled".into()),
|
||||
};
|
||||
|
||||
Ok(Flash::new(
|
||||
|
|
|
|||
|
|
@ -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,8 +192,7 @@ pub async fn forgot_password_email_form(
|
|||
};
|
||||
|
||||
// Send email
|
||||
let (flash_kind, flash_message) = match email_config.inner() {
|
||||
Some(email_config) => {
|
||||
let (flash_kind, flash_message) =
|
||||
match email::send_email(email_config, &user_for_email, email_title, html).await {
|
||||
Ok(okay) => {
|
||||
if okay.is_positive() {
|
||||
|
|
@ -192,12 +205,6 @@ pub async fn forgot_password_email_form(
|
|||
}
|
||||
}
|
||||
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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue