0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-12-04 19:32:53 +01:00

Refactor emails to be async

This commit is contained in:
Delta1925 2023-04-23 12:08:28 +02:00
parent 18f814dec9
commit 695f5b8559
No known key found for this signature in database
GPG key ID: 1C21ACE44193CB25
5 changed files with 17 additions and 14 deletions

3
backend/Cargo.lock generated
View file

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

View file

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

View file

@ -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)?,
}

View file

@ -110,7 +110,7 @@ async fn submit(pem: web::Form<Key>) -> Result<HttpResponse, CompatErr> {
"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<Email>) -> Result<HttpResponse, CompatErr> {
"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

View file

@ -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<Tokio1Executor> {
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::<Tokio1Executor>::relay(&SETTINGS.mail_settings.smtp_host)
}
SMTPEncryption::Starttls => {
SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host)
AsyncSmtpTransport::<Tokio1Executor>::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<Settings> = Lazy::new(get_settings);
pub static MAILER: Lazy<SmtpTransport> = Lazy::new(get_mailer);
pub static MAILER: Lazy<AsyncSmtpTransport<Tokio1Executor>> = Lazy::new(get_mailer);