server: add an option to use STARTTLS for smtp

This commit is contained in:
Valentin Tolmer 2022-07-30 15:45:31 +02:00 committed by nitnelave
parent cd0ab378ef
commit 8c1ea11b95

View File

@ -1,4 +1,4 @@
use crate::infra::configuration::MailOptions; use crate::infra::{cli::SmtpEncryption, configuration::MailOptions};
use anyhow::Result; use anyhow::Result;
use lettre::{ use lettre::{
message::Mailbox, transport::smtp::authentication::Credentials, Message, SmtpTransport, 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.user.clone(),
options.password.unsecure().to_string(), options.password.unsecure().to_string(),
); );
let mailer = SmtpTransport::relay(&options.server)? let relay_factory = match options.smtp_encryption {
.credentials(creds) SmtpEncryption::TLS => SmtpTransport::relay,
.build(); SmtpEncryption::STARTTLS => SmtpTransport::starttls_relay,
};
let mailer = relay_factory(&options.server)?.credentials(creds).build();
mailer.send(&email)?; mailer.send(&email)?;
Ok(()) Ok(())
} }