XOAUTH2 support will make the transport building logic a lot more complex, having a separate function for it will make it more manageable.
Signed-off-by: Arthur Bied-Charreton <[email protected]> --- proxmox-notify/src/endpoints/smtp.rs | 44 ++++++++++++++++------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/proxmox-notify/src/endpoints/smtp.rs b/proxmox-notify/src/endpoints/smtp.rs index 699ed1c6..1340d8ea 100644 --- a/proxmox-notify/src/endpoints/smtp.rs +++ b/proxmox-notify/src/endpoints/smtp.rs @@ -162,6 +162,30 @@ pub struct SmtpEndpoint { pub private_config: SmtpPrivateConfig, } +impl SmtpEndpoint { + fn build_transport(&self, tls: Tls, port: u16) -> Result<SmtpTransport, Error> { + let mut transport_builder = SmtpTransport::builder_dangerous(&self.config.server) + .tls(tls) + .port(port) + .timeout(Some(Duration::from_secs(SMTP_TIMEOUT.into()))); + + if let Some(username) = self.config.username.as_deref() { + if let Some(password) = self.private_config.password.as_deref() { + transport_builder = transport_builder.credentials((username, password).into()); + } else { + return Err(Error::NotifyFailed( + self.name().into(), + Box::new(Error::Generic( + "username is set but no password was provided".to_owned(), + )), + )); + } + }; + + Ok(transport_builder.build()) + } +} + impl Endpoint for SmtpEndpoint { fn send(&self, notification: &Notification) -> Result<(), Error> { let tls_parameters = TlsParameters::new(self.config.server.clone()) @@ -182,25 +206,7 @@ impl Endpoint for SmtpEndpoint { } }; - let mut transport_builder = SmtpTransport::builder_dangerous(&self.config.server) - .tls(tls) - .port(port) - .timeout(Some(Duration::from_secs(SMTP_TIMEOUT.into()))); - - if let Some(username) = self.config.username.as_deref() { - if let Some(password) = self.private_config.password.as_deref() { - transport_builder = transport_builder.credentials((username, password).into()); - } else { - return Err(Error::NotifyFailed( - self.name().into(), - Box::new(Error::Generic( - "username is set but no password was provided".to_owned(), - )), - )); - } - } - - let transport = transport_builder.build(); + let transport = self.build_transport(tls, port)?; let recipients = mail::get_recipients( self.config.mailto.as_slice(), -- 2.47.3
