mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-12-05 03:12:50 +01:00
Improve mail system
This commit is contained in:
parent
9c35f4adef
commit
1a85cb77fa
3 changed files with 28 additions and 25 deletions
|
@ -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),
|
||||
}
|
||||
|
|
|
@ -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")]
|
||||
|
|
|
@ -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<Settings> = Lazy::new(get_settings);
|
||||
pub static MAILER: Lazy<SmtpTransport> = Lazy::new(get_mailer);
|
||||
|
|
Loading…
Reference in a new issue