diff --git a/crates/modules/smtp-notifier/README.md b/crates/modules/smtp-notifier/README.md index 57f1906b..06ffdcfe 100644 --- a/crates/modules/smtp-notifier/README.md +++ b/crates/modules/smtp-notifier/README.md @@ -6,13 +6,13 @@ This module will send an email notification when Pulsar identifies a threat. |Config|Type|Description| |------|----|-----------| -|`user` (required)|string|user credential for smtp server| +|`username` (required)|string|user credential for smtp server. Usually it's your email address, otherwise `sender` field must be set| |`password` (required)|string|password credential for smtp server| |`server` (required)|string|smtp server url to use| |`receivers` (required)|string|comma separated emails to send notifications to| |`port`|int|port for smtp server| |`encryption`|string|encryption type to use for smtp: tls, starttls, none| -|`sender`|string|set a different email sender (should be allowed by email provider)| +|`sender`|string|set this if `username` is not your email address or if you want a custom sender (custom sender address must be allowed by your email provider)| Default configuration: diff --git a/crates/modules/smtp-notifier/src/lib.rs b/crates/modules/smtp-notifier/src/lib.rs index 3a8b1ddb..a54174b4 100644 --- a/crates/modules/smtp-notifier/src/lib.rs +++ b/crates/modules/smtp-notifier/src/lib.rs @@ -53,16 +53,13 @@ async fn smtp_notifier_task( let body = format!("{description}\n Source event: {payload}"); let mut message_builder = Message::builder() - .subject(subject); + .subject(subject) + .from(config.sender.clone()); for receiver in config.receivers.iter() { message_builder = message_builder.to(receiver.clone()) } - if let Some(sender) = &config.sender { - message_builder = message_builder.from(sender.clone()); - } - let message = message_builder.body(body)?; let smtp_transport = match config.encryption { @@ -76,7 +73,7 @@ async fn smtp_notifier_task( }; smtp_transport - .credentials(Credentials::new(config.user.clone(), config.password.clone())) + .credentials(Credentials::new(config.username.clone(), config.password.clone())) .port(config.port) .build() .send(message) @@ -128,31 +125,38 @@ impl FromStr for Encryption { #[derive(Clone, Debug)] struct SmtpNotifierConfig { server: String, - user: String, + username: String, password: String, receivers: Vec, port: u16, encryption: Encryption, - sender: Option, + sender: Mailbox, } impl TryFrom<&ModuleConfig> for SmtpNotifierConfig { type Error = ConfigError; fn try_from(config: &ModuleConfig) -> Result { + let username = config.required::("username")?; + + // Get sender from `sender` field or try to parse `username` as an email let sender = match config.get_raw("sender") { - Some(s) => { - let mailbox = s - .parse::() - .map_err(|err| ConfigError::InvalidValue { - field: "sender".to_string(), - value: s.to_string(), - err: err.to_string(), - })?; - - Some(mailbox) - } - None => None, + Some(s) => s + .parse::() + .map_err(|err| ConfigError::InvalidValue { + field: "sender".to_string(), + value: s.to_string(), + err: err.to_string(), + })?, + None => username + .parse::() + .map_err(|err| ConfigError::InvalidValue { + field: "username".to_string(), + value: username.to_string(), + err: format!( + "if `username` is not the email address, a `sender` must be set: {err}" + ), + })?, }; let receivers = config.get_list::("receivers")?; @@ -165,7 +169,7 @@ impl TryFrom<&ModuleConfig> for SmtpNotifierConfig { Ok(SmtpNotifierConfig { server: config.required::("server")?, - user: config.required::("user")?, + username, password: config.required::("password")?, receivers, port: config.with_default::("port", 465)?,