server: add option to use insecure SMTP connection

This commit is contained in:
Igor Rzegocki 2023-01-19 11:30:25 +01:00 committed by GitHub
parent 9018e6fa34
commit d722be8896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 6 deletions

View File

@ -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"

View File

@ -117,6 +117,7 @@ pub struct LdapsOpts {
clap::arg_enum! {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum SmtpEncryption {
NONE,
TLS,
STARTTLS,
}

View File

@ -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);
}

View File

@ -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::<Tokio1Executor>::relay,
SmtpEncryption::STARTTLS => AsyncSmtpTransport::<Tokio1Executor>::starttls_relay,
let mailer = match options.smtp_encryption {
SmtpEncryption::NONE => {
AsyncSmtpTransport::<Tokio1Executor>::builder_dangerous(&options.server)
}
SmtpEncryption::TLS => AsyncSmtpTransport::<Tokio1Executor>::relay(&options.server)?,
SmtpEncryption::STARTTLS => {
AsyncSmtpTransport::<Tokio1Executor>::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(())
}