From 0058c3053d9b6ccb94648857e6c2f4590bf49b72 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 3 Jan 2022 15:50:04 +0900 Subject: [PATCH] Move mail config from plume::mail::mailer to plume_models::CONFIG --- plume-models/src/config.rs | 18 ++++++++++++++++++ src/mail.rs | 16 ++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/plume-models/src/config.rs b/plume-models/src/config.rs index fe5282a0..b7459f73 100644 --- a/plume-models/src/config.rs +++ b/plume-models/src/config.rs @@ -21,6 +21,7 @@ pub struct Config { pub logo: LogoConfig, pub default_theme: String, pub media_directory: String, + pub mail: Option, pub ldap: Option, pub proxy: Option, } @@ -245,6 +246,22 @@ impl SearchTokenizerConfig { } } +pub struct MailConfig { + pub server: String, + pub helo_name: String, + pub username: String, + pub password: String, +} + +fn get_mail_config() -> Option { + Some(MailConfig { + server: env::var("MAIL_SERVER").ok()?, + helo_name: env::var("MAIL_HELO_NAME").unwrap_or_else(|_| "localhost".to_owned()), + username: env::var("MAIL_USER").ok()?, + password: env::var("MAIL_PASSWORD").ok()?, + }) +} + pub struct LdapConfig { pub addr: String, pub base_dn: String, @@ -347,6 +364,7 @@ lazy_static! { default_theme: var("DEFAULT_THEME").unwrap_or_else(|_| "default-light".to_owned()), media_directory: var("MEDIA_UPLOAD_DIRECTORY") .unwrap_or_else(|_| "static/media".to_owned()), + mail: get_mail_config(), ldap: get_ldap_config(), proxy: get_proxy_config(), }; diff --git a/src/mail.rs b/src/mail.rs index 1d7c81ee..e161effd 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -54,19 +54,19 @@ mod mailer { }, SmtpClient, SmtpTransport, }; - use std::env; + use plume_models::CONFIG; pub type Mailer = Option; pub fn init() -> Mailer { - let server = env::var("MAIL_SERVER").ok()?; - let helo_name = env::var("MAIL_HELO_NAME").unwrap_or_else(|_| "localhost".to_owned()); - let username = env::var("MAIL_USER").ok()?; - let password = env::var("MAIL_PASSWORD").ok()?; - let mail = SmtpClient::new_simple(&server) + let config = CONFIG.mail.as_ref()?; + let mail = SmtpClient::new_simple(&config.server) .unwrap() - .hello_name(ClientId::Domain(helo_name)) - .credentials(Credentials::new(username, password)) + .hello_name(ClientId::Domain(config.helo_name.clone())) + .credentials(Credentials::new( + config.username.clone(), + config.password.clone(), + )) .smtp_utf8(true) .authentication_mechanism(Mechanism::Plain) .connection_reuse(ConnectionReuseParameters::NoReuse)