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";
|
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,
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,27 +372,23 @@ 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() {
|
(
|
||||||
(
|
FlashKind::Success,
|
||||||
FlashKind::Success,
|
format!("Email is on it's way to <code>{email}</code>"),
|
||||||
format!("Email is on it's way to <code>{email}</code>"),
|
)
|
||||||
)
|
} else {
|
||||||
} else {
|
(
|
||||||
(
|
FlashKind::Warning,
|
||||||
FlashKind::Warning,
|
"Email should be on it's way, but it might not arrive".into(),
|
||||||
"Email should be on it's way, but it might not arrive".into(),
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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(
|
||||||
Redirect::to(uri!(admin_users_view(id))),
|
Redirect::to(uri!(admin_users_view(id))),
|
||||||
|
|
|
||||||
|
|
@ -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,27 +192,20 @@ 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() {
|
(FlashKind::Success, SUCCESS_MESSAGE.to_string())
|
||||||
(FlashKind::Success, SUCCESS_MESSAGE.to_string())
|
} else {
|
||||||
} else {
|
(
|
||||||
(
|
FlashKind::Warning,
|
||||||
FlashKind::Warning,
|
"Email should be on it's way, but it might not arrive".into(),
|
||||||
"Email should be on it's way, but it might not arrive".into(),
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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(
|
||||||
Redirect::to(uri!(forgot_password_page)),
|
Redirect::to(uri!(forgot_password_page)),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue