diff --git a/src/confirmation.rs b/src/confirmation.rs index 11bf124..0f9a631 100644 --- a/src/confirmation.rs +++ b/src/confirmation.rs @@ -3,12 +3,11 @@ use chrono::Utc; use crate::errors::Error; use crate::management::{delete_key, Action, Pending}; use crate::pending_path; -use crate::settings::{SMTPEncryption, SETTINGS}; +use crate::settings::{MAILER, SETTINGS}; use crate::utils::{get_email_from_cert, parse_pem}; use crate::PENDING_FOLDER; -use lettre::transport::smtp::authentication::Credentials; -use lettre::{Message, SmtpTransport, Transport}; +use lettre::{Message, Transport}; use std::fs; use std::path::Path; @@ -59,7 +58,6 @@ pub fn confirm_action(token: &str) -> Result<(), Error> { } pub fn send_confirmation_email(email: &str, action: &Action, token: &str) -> Result<(), Error> { - println!("Sending mail, token: {}", &token); let email = Message::builder() .from(match SETTINGS.mail_settings.mail_from.parse() { Ok(mailbox) => mailbox, @@ -71,28 +69,13 @@ pub fn send_confirmation_email(email: &str, action: &Action, token: &str) -> Res }) .subject(&SETTINGS.mail_settings.mail_subject) .body(format!("{action} - {token}")); + let message = match email { Ok(message) => message, Err(_) => return Err(Error::MailGeneration), }; - 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::Starttls => { - SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host) - } - }; - let mailer = match builder { - Ok(builder) => builder, - Err(_) => return Err(Error::SmtpBuilder), - } - .credentials(creds) - .port(SETTINGS.mail_settings.smtp_port) - .build(); - match mailer.send(&message) { + + match MAILER.send(&message) { Ok(_) => Ok(()), Err(_) => Err(Error::SendMail), } diff --git a/src/errors.rs b/src/errors.rs index ca4732c..b5eac2e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -15,8 +15,6 @@ pub enum Error { MissingMail, #[error("EE1: Error while sending the E-Mail")] SendMail, - #[error("EE2: Error while building the SMTP connection")] - SmtpBuilder, #[error("ES1: rror while serializing data")] SerializeData, #[error("ES2: Error while deserializing data")] diff --git a/src/settings.rs b/src/settings.rs index 8b31c04..0ea80c4 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,3 +1,4 @@ +use lettre::{transport::smtp::authentication::Credentials, SmtpTransport}; use once_cell::sync::Lazy; use sequoia_net::wkd::Variant; use serde::{Deserialize, Serialize}; @@ -40,7 +41,7 @@ pub enum SMTPEncryption { } fn get_settings() -> Settings { - println!("Reaing settings..."); + println!("Reading settings..."); let content = match fs::read_to_string("wkd.toml") { Ok(content) => content, Err(_) => panic!("Unable to access settings file!"), @@ -51,4 +52,25 @@ fn get_settings() -> Settings { } } +fn get_mailer() -> SmtpTransport { + 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::Starttls => { + SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host) + } + }; + match builder { + Ok(builder) => builder, + Err(_) => panic!("Unable to set up smtp"), + } + .credentials(creds) + .port(SETTINGS.mail_settings.smtp_port) + .build() +} + pub static SETTINGS: Lazy = Lazy::new(get_settings); +pub static MAILER: Lazy = Lazy::new(get_mailer);