From 9a360f17c62bcc524af503efe95e197ce63f6229 Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Fri, 18 Jul 2025 23:53:49 +0200 Subject: Specify mail from in environment --- src/db/mod.rs | 6 +++++- src/mail.rs | 17 ++++++++++------- src/model/config.rs | 6 ++++-- 3 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/db/mod.rs b/src/db/mod.rs index d0c4f7b..b1fdeea 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -38,7 +38,11 @@ async fn apply_migrations(conn: &Connection) -> Result<()> { .await?) } -async fn set_pragma(conn: &Connection, key: impl Into, value: impl Into) -> Result<()> { +async fn set_pragma( + conn: &Connection, + key: impl Into, + value: impl Into, +) -> Result<()> { let key = key.into(); let value = value.into(); Ok(conn diff --git a/src/mail.rs b/src/mail.rs index 7017654..d7df246 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -7,7 +7,6 @@ use tokio::process::Command; use crate::model::config::Config; static FROM_NAME: &str = "Budget"; -static FROM_ADDRESS: &str = "budget@guyonvarch.me"; #[derive(Clone)] pub struct Recipient { @@ -21,15 +20,15 @@ pub async fn send( subject: &str, message: &str, ) -> bool { - let headers = format_headers(recipients.clone(), subject); + let headers = format_headers(config, recipients.clone(), subject); log::info!( "Sending mail{}\n{}", - if config.mock_mails { " (MOCK)" } else { "" }, + if config.mails_mock { " (MOCK)" } else { "" }, headers.clone() ); - if config.mock_mails { + if config.mails_mock { true } else { let recipient_addresses = recipients @@ -41,7 +40,7 @@ pub async fn send( // https://github.com/NixOS/nixpkgs/issues/90248 let mut command = Command::new("/run/wrappers/bin/sendmail"); command.kill_on_drop(true); - command.arg("-f").arg(FROM_ADDRESS); + command.arg("-f").arg(config.mails_from.clone()); command.arg("--").args(recipient_addresses); command .stdin(Stdio::piped()) @@ -72,7 +71,11 @@ pub async fn send( } } -fn format_headers(recipients: Vec, subject: &str) -> String { +fn format_headers( + config: &Config, + recipients: Vec, + subject: &str, +) -> String { let recipients = recipients .into_iter() .map(|r| format_address(&r.name, &r.address)) @@ -82,7 +85,7 @@ fn format_headers(recipients: Vec, subject: &str) -> String { format!( "Date: {}\nFrom: {}\nTo: {}\nSubject: {}", Utc::now().to_rfc2822(), - format_address(FROM_NAME, FROM_ADDRESS), + format_address(FROM_NAME, &config.mails_from), recipients, subject, ) diff --git a/src/model/config.rs b/src/model/config.rs index 1fa5bb4..e0267d5 100644 --- a/src/model/config.rs +++ b/src/model/config.rs @@ -6,7 +6,8 @@ use std::str::FromStr; pub struct Config { pub auth_secret: String, pub db_path: String, - pub mock_mails: bool, + pub mails_mock: bool, + pub mails_from: String, pub secure_cookies: bool, pub socket_address: SocketAddr, } @@ -15,7 +16,8 @@ pub fn from_env() -> Result { Ok(Config { auth_secret: read_string("AUTH_SECRET")?, db_path: read_string("DB_PATH")?, - mock_mails: read_bool("MOCK_MAILS")?, + mails_mock: read_bool("MAILS_MOCK")?, + mails_from: read_string("MAILS_FROM")?, secure_cookies: read_bool("SECURE_COOKIES")?, socket_address: read_socket_address("SOCKET_ADDRESS")?, }) -- cgit v1.2.3