From 8c1ea11b9520b8b54881d957198ab935b689fd00 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sat, 30 Jul 2022 15:45:31 +0200 Subject: [PATCH] server: add an option to use STARTTLS for smtp --- server/src/infra/mail.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/src/infra/mail.rs b/server/src/infra/mail.rs index 2f5609f..60c27d0 100644 --- a/server/src/infra/mail.rs +++ b/server/src/infra/mail.rs @@ -1,4 +1,4 @@ -use crate::infra::configuration::MailOptions; +use crate::infra::{cli::SmtpEncryption, configuration::MailOptions}; use anyhow::Result; use lettre::{ message::Mailbox, transport::smtp::authentication::Credentials, Message, SmtpTransport, @@ -26,9 +26,11 @@ fn send_email(to: Mailbox, subject: &str, body: String, options: &MailOptions) - options.user.clone(), options.password.unsecure().to_string(), ); - let mailer = SmtpTransport::relay(&options.server)? - .credentials(creds) - .build(); + let relay_factory = match options.smtp_encryption { + SmtpEncryption::TLS => SmtpTransport::relay, + SmtpEncryption::STARTTLS => SmtpTransport::starttls_relay, + }; + let mailer = relay_factory(&options.server)?.credentials(creds).build(); mailer.send(&email)?; Ok(()) }