diff --git a/conduwuit-example.toml b/conduwuit-example.toml index f2bd19aab..a980efaf3 100644 --- a/conduwuit-example.toml +++ b/conduwuit-example.toml @@ -2173,6 +2173,11 @@ # #connection_uri = +# Same as `connection_uri`, except loaded from a file. If `connection_uri` +# is defined, it is prioritized and `connection_uri_file` is ignored +# +#connection_uri_file = + # The outgoing address which will be used for sending emails. # # For a syntax guide, see https://datatracker.ietf.org/doc/html/rfc2822#section-3.4 diff --git a/src/core/config/check.rs b/src/core/config/check.rs index 3083ef141..ab65b81d1 100644 --- a/src/core/config/check.rs +++ b/src/core/config/check.rs @@ -1,4 +1,4 @@ -use std::env::consts::OS; +use std::{env::consts::OS, path::PathBuf}; use either::Either; use figment::Figment; @@ -263,6 +263,18 @@ pub fn check(config: &Config) -> Result { )); } + if config.smtp.as_ref().is_some_and(|f| { + f.connection_uri.is_none() && f.connection_uri_file.is_none() + || (f.connection_uri == Some(String::new()) + && f.connection_uri_file == Some(PathBuf::new())) + }) { + return Err!(Config( + "connection_uri", + "If smtp is enabled, either connection_uri or connection_uri_file have to be \ + defined." + )); + } + match (&config.default_room_acl_allow, &config.default_room_acl_deny) { | (Some(_), Some(_)) => { return Err!(Config( diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index bf048a526..ea6aeb5e8 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -2589,7 +2589,11 @@ pub struct SmtpConfig { /// /// For a guide on the accepted URI syntax, consult Lettre's documentation: /// https://docs.rs/lettre/latest/lettre/transport/smtp/struct.AsyncSmtpTransport.html#method.from_url - pub connection_uri: String, + pub connection_uri: Option, + + /// Same as `connection_uri`, except loaded from a file. If `connection_uri` + /// is defined, it is prioritized and `connection_uri_file` is ignored + pub connection_uri_file: Option, /// The outgoing address which will be used for sending emails. /// diff --git a/src/service/mailer/mod.rs b/src/service/mailer/mod.rs index c959c397a..9126769c1 100644 --- a/src/service/mailer/mod.rs +++ b/src/service/mailer/mod.rs @@ -20,16 +20,28 @@ pub struct Service { #[async_trait::async_trait] impl crate::Service for Service { fn build(args: Args<'_>) -> Result> { - let transport = args - .server - .config - .smtp - .as_ref() - .map(|config| { - Ok((config.sender.clone(), Transport::from_url(&config.connection_uri)?.build())) - }) - .transpose() - .map_err(|err: TransportError| err!("Failed to set up SMTP transport: {err}"))?; + let transport = + args.server + .config + .smtp + .as_ref() + .map(|config| -> Result<_> { + let connection_uri: String = + if let Some(uri) = &config.connection_uri { + uri.clone() + } else { + std::fs::read_to_string( + config.connection_uri_file.as_ref().unwrap() // Safe to unwrap here assuming the config checks properly run and throw errors + ) + .map_err(|err| err!("Failed to read SMTP connection URI file: {err}"))? + }; + let transport = Transport::from_url(&connection_uri) + .map_err(|err| err!("Failed to set up SMTP transport: {err}"))? + .build(); + + Ok((config.sender.clone(), transport)) + }) + .transpose()?; Ok(Arc::new(Self { transport })) }