From d722be889689631320191c0e14506fcb72788a3d Mon Sep 17 00:00:00 2001 From: Igor Rzegocki Date: Thu, 19 Jan 2023 11:30:25 +0100 Subject: [PATCH] server: add option to use insecure SMTP connection --- lldap_config.docker_template.toml | 2 +- server/src/infra/cli.rs | 1 + server/src/infra/configuration.rs | 3 +++ server/src/infra/mail.rs | 19 ++++++++++++++----- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lldap_config.docker_template.toml b/lldap_config.docker_template.toml index c16fd3f..02e2aac 100644 --- a/lldap_config.docker_template.toml +++ b/lldap_config.docker_template.toml @@ -113,7 +113,7 @@ key_file = "/data/private_key" #server="smtp.gmail.com" ## The SMTP port. #port=587 -## How the connection is encrypted, either "TLS" or "STARTTLS". +## How the connection is encrypted, either "NONE" (no encryption), "TLS" or "STARTTLS". #smtp_encryption = "TLS" ## The SMTP user, usually your email address. #user="sender@gmail.com" diff --git a/server/src/infra/cli.rs b/server/src/infra/cli.rs index a31a968..ab1ba42 100644 --- a/server/src/infra/cli.rs +++ b/server/src/infra/cli.rs @@ -117,6 +117,7 @@ pub struct LdapsOpts { clap::arg_enum! { #[derive(Clone, Debug, Deserialize, Serialize)] pub enum SmtpEncryption { + NONE, TLS, STARTTLS, } diff --git a/server/src/infra/configuration.rs b/server/src/infra/configuration.rs index 209adbe..11e517c 100644 --- a/server/src/infra/configuration.rs +++ b/server/src/infra/configuration.rs @@ -266,6 +266,9 @@ impl ConfigOverrider for SmtpOpts { if let Some(password) = &self.smtp_password { config.smtp_options.password = SecUtf8::from(password.clone()); } + if let Some(smtp_encryption) = &self.smtp_encryption { + config.smtp_options.smtp_encryption = smtp_encryption.clone(); + } if let Some(tls_required) = self.smtp_tls_required { config.smtp_options.tls_required = Some(tls_required); } diff --git a/server/src/infra/mail.rs b/server/src/infra/mail.rs index bbac6c0..c67614b 100644 --- a/server/src/infra/mail.rs +++ b/server/src/infra/mail.rs @@ -26,12 +26,21 @@ async fn send_email(to: Mailbox, subject: &str, body: String, options: &MailOpti options.user.clone(), options.password.unsecure().to_string(), ); - let relay_factory = match options.smtp_encryption { - SmtpEncryption::TLS => AsyncSmtpTransport::::relay, - SmtpEncryption::STARTTLS => AsyncSmtpTransport::::starttls_relay, + let mailer = match options.smtp_encryption { + SmtpEncryption::NONE => { + AsyncSmtpTransport::::builder_dangerous(&options.server) + } + SmtpEncryption::TLS => AsyncSmtpTransport::::relay(&options.server)?, + SmtpEncryption::STARTTLS => { + AsyncSmtpTransport::::starttls_relay(&options.server)? + } }; - let mailer = relay_factory(&options.server)?.credentials(creds).build(); - mailer.send(email).await?; + mailer + .credentials(creds) + .port(options.port) + .build() + .send(email) + .await?; Ok(()) }