From 695f5b85591456f2613e4d7037b878b1e070971c Mon Sep 17 00:00:00 2001 From: Delta1925 Date: Sun, 23 Apr 2023 12:08:28 +0200 Subject: [PATCH] Refactor emails to be async --- backend/Cargo.lock | 3 +++ backend/Cargo.toml | 2 +- backend/src/confirmation.rs | 6 +++--- backend/src/main.rs | 4 ++-- backend/src/settings.rs | 16 ++++++++-------- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index fb5abcc..24163a8 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -1192,10 +1192,12 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76bd09637ae3ec7bd605b8e135e757980b3968430ff2b1a4a94fb7769e50166d" dependencies = [ + "async-trait", "base64 0.21.0", "email-encoding", "email_address", "fastrand", + "futures-io", "futures-util", "hostname", "httpdate", @@ -1207,6 +1209,7 @@ dependencies = [ "quoted_printable", "socket2", "tokio", + "tokio-native-tls", ] [[package]] diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 6a5974d..11c4660 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -9,7 +9,7 @@ actix-web = "4.3.1" anyhow = "1.0.70" chrono = "0.4.24" flexi_logger = "0.25.3" -lettre = "0.10.4" +lettre = { version = "0.10.4", features = ["tokio1-native-tls"] } log = "0.4.17" once_cell = "1.17.1" rand = "0.8.5" diff --git a/backend/src/confirmation.rs b/backend/src/confirmation.rs index fc2ae9f..4539c54 100644 --- a/backend/src/confirmation.rs +++ b/backend/src/confirmation.rs @@ -9,7 +9,7 @@ use crate::utils::{extract_domain, get_email_from_cert, parse_pem, read_file}; use crate::{log_err, pending_path}; use anyhow::Result; -use lettre::{Message, Transport}; +use lettre::{AsyncTransport, Message}; use std::fs; use std::path::Path; @@ -43,7 +43,7 @@ pub fn confirm_action(token: &str) -> Result<(Action, String)> { } } -pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<()> { +pub async fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<()> { let template = log_err!( read_file(&Path::new("assets").join("mail-template.html")), error, @@ -82,7 +82,7 @@ pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> R let email = log_err!(email, warn)?; - match log_err!(MAILER.send(&email), warn) { + match log_err!(MAILER.send(email).await, warn) { Ok(_) => Ok(()), Err(_) => Err(SpecialErrors::MailErr)?, } diff --git a/backend/src/main.rs b/backend/src/main.rs index ccd0187..255d1f3 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -110,7 +110,7 @@ async fn submit(pem: web::Form) -> Result { "Sending email to {} to add a key... (Request token: {})", email, token ); - send_confirmation_email(&email, &Action::Add, &token)?; + send_confirmation_email(&email, &Action::Add, &token).await?; info!("User {} requested to add a key successfully!", email); Ok(return_outcome(Ok("You submitted your key successfully!"))?) } @@ -140,7 +140,7 @@ async fn delete(email: web::Query) -> Result { "Sending email to {} to delete a key... (Request token: {})", email.email, token ); - send_confirmation_email(&email.email, &Action::Delete, &token)?; + send_confirmation_email(&email.email, &Action::Delete, &token).await?; info!( "User {} requested to delete his key successfully!", email.email diff --git a/backend/src/settings.rs b/backend/src/settings.rs index 9953799..344bf45 100644 --- a/backend/src/settings.rs +++ b/backend/src/settings.rs @@ -1,5 +1,5 @@ -use lettre::{transport::smtp::authentication::Credentials, SmtpTransport}; -use log::{debug, error, warn}; +use lettre::{transport::smtp::authentication::Credentials, AsyncSmtpTransport, Tokio1Executor}; +use log::{debug, error}; use once_cell::sync::Lazy; use sequoia_net::wkd::Variant; use sequoia_openpgp::policy::StandardPolicy; @@ -65,16 +65,18 @@ fn get_settings() -> Settings { settings } -fn get_mailer() -> SmtpTransport { +fn get_mailer() -> AsyncSmtpTransport { debug!("Setting up smtp..."); let creds = Credentials::new( SETTINGS.mail_settings.smtp_username.to_owned(), SETTINGS.mail_settings.smtp_password.to_owned(), ); let builder = match &SETTINGS.mail_settings.smtp_tls { - SMTPEncryption::Tls => SmtpTransport::relay(&SETTINGS.mail_settings.smtp_host), + SMTPEncryption::Tls => { + AsyncSmtpTransport::::relay(&SETTINGS.mail_settings.smtp_host) + } SMTPEncryption::Starttls => { - SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host) + AsyncSmtpTransport::::starttls_relay(&SETTINGS.mail_settings.smtp_host) } }; let mailer = match builder { @@ -87,8 +89,6 @@ fn get_mailer() -> SmtpTransport { .credentials(creds) .port(SETTINGS.mail_settings.smtp_port) .build(); - debug!("Testing smtp connection..."); - let _ = log_err!(mailer.test_connection(), warn); mailer } @@ -96,4 +96,4 @@ pub const ERROR_TEXT: &str = "An error occoured:"; pub const POLICY: &StandardPolicy = &StandardPolicy::new(); pub const ROOT_FOLDER: &str = "data"; pub static SETTINGS: Lazy = Lazy::new(get_settings); -pub static MAILER: Lazy = Lazy::new(get_mailer); +pub static MAILER: Lazy> = Lazy::new(get_mailer);