diff options
author | Joris Guyonvarch | 2025-07-18 23:53:49 +0200 |
---|---|---|
committer | Joris Guyonvarch | 2025-07-18 23:53:49 +0200 |
commit | 9a360f17c62bcc524af503efe95e197ce63f6229 (patch) | |
tree | 137211ba28aaad81e097913aea44967bc6f13dea | |
parent | bf26a9a7d145590e498e6057db27cee2ad162445 (diff) |
Specify mail from in environmentmain
-rwxr-xr-x | bin/dev-server | 3 | ||||
-rw-r--r-- | src/db/mod.rs | 6 | ||||
-rw-r--r-- | src/mail.rs | 17 | ||||
-rw-r--r-- | src/model/config.rs | 6 |
4 files changed, 21 insertions, 11 deletions
diff --git a/bin/dev-server b/bin/dev-server index 195791c..197b94c 100755 --- a/bin/dev-server +++ b/bin/dev-server @@ -7,7 +7,8 @@ CMD=${1:-run} export RUST_LOG="budget=info" export AUTH_SECRET="pMkW8PR7bjhc24bbvZLIEHfcooLzrGIh" export DB_PATH="database.db" -export MOCK_MAILS="true" +export MAILS_MOCK="true" +export MAILS_FROM="budget@localhost" export SECURE_COOKIES="false" export SOCKET_ADDRESS="0.0.0.0:3000" 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<String>, value: impl Into<String>) -> Result<()> { +async fn set_pragma( + conn: &Connection, + key: impl Into<String>, + value: impl Into<String>, +) -> 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<Recipient>, subject: &str) -> String { +fn format_headers( + config: &Config, + recipients: Vec<Recipient>, + 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<Recipient>, 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<Config, String> { 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")?, }) |