From dd7e39262672d4d9ed4b0c6c913c9ee15c9e1af0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Mrozek?= Date: Thu, 24 Nov 2022 14:47:56 +0100 Subject: [PATCH] server: use async api for email sending Fixes #378 --- server/src/infra/auth_service.rs | 4 +++- server/src/infra/mail.rs | 21 +++++++++++---------- server/src/main.rs | 11 ++++++++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/server/src/infra/auth_service.rs b/server/src/infra/auth_service.rs index fdb18a4..cdbafb5 100644 --- a/server/src/infra/auth_service.rs +++ b/server/src/infra/auth_service.rs @@ -179,7 +179,9 @@ where &token, &data.server_url, &data.mail_options, - ) { + ) + .await + { warn!("Error sending email: {:#?}", e); return Err(TcpError::InternalServerError(format!( "Could not send email: {}", diff --git a/server/src/infra/mail.rs b/server/src/infra/mail.rs index 60c27d0..bbac6c0 100644 --- a/server/src/infra/mail.rs +++ b/server/src/infra/mail.rs @@ -1,12 +1,12 @@ use crate::infra::{cli::SmtpEncryption, configuration::MailOptions}; -use anyhow::Result; +use anyhow::{Ok, Result}; use lettre::{ - message::Mailbox, transport::smtp::authentication::Credentials, Message, SmtpTransport, - Transport, + message::Mailbox, transport::smtp::authentication::Credentials, AsyncSmtpTransport, + AsyncTransport, Message, Tokio1Executor, }; use tracing::debug; -fn send_email(to: Mailbox, subject: &str, body: String, options: &MailOptions) -> Result<()> { +async fn send_email(to: Mailbox, subject: &str, body: String, options: &MailOptions) -> Result<()> { let from = options .from .clone() @@ -27,15 +27,15 @@ fn send_email(to: Mailbox, subject: &str, body: String, options: &MailOptions) - options.password.unsecure().to_string(), ); let relay_factory = match options.smtp_encryption { - SmtpEncryption::TLS => SmtpTransport::relay, - SmtpEncryption::STARTTLS => SmtpTransport::starttls_relay, + SmtpEncryption::TLS => AsyncSmtpTransport::::relay, + SmtpEncryption::STARTTLS => AsyncSmtpTransport::::starttls_relay, }; let mailer = relay_factory(&options.server)?.credentials(creds).build(); - mailer.send(&email)?; + mailer.send(email).await?; Ok(()) } -pub fn send_password_reset_email( +pub async fn send_password_reset_email( username: &str, to: &str, token: &str, @@ -54,14 +54,15 @@ To reset your password please visit the following URL: {}/reset-password/step2/{ Please contact an administrator if you did not initiate the process.", username, domain, token ); - send_email(to, "[LLDAP] Password reset requested", body, options) + send_email(to, "[LLDAP] Password reset requested", body, options).await } -pub fn send_test_email(to: Mailbox, options: &MailOptions) -> Result<()> { +pub async fn send_test_email(to: Mailbox, options: &MailOptions) -> Result<()> { send_email( to, "LLDAP test email", "The test is successful! You can send emails from LLDAP".to_string(), options, ) + .await } diff --git a/server/src/main.rs b/server/src/main.rs index 87735dc..63bcf78 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -131,7 +131,16 @@ fn send_test_email_command(opts: TestEmailOpts) -> Result<()> { let to = opts.to.parse()?; let config = infra::configuration::init(opts)?; infra::logging::init(&config)?; - mail::send_test_email(to, &config.smtp_options) + + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build()?; + + runtime.block_on( + mail::send_test_email(to, &config.smtp_options) + .unwrap_or_else(|e| error!("Could not send email: {:#}", e)), + ); + Ok(()) } fn run_healthcheck(opts: RunOpts) -> Result<()> {