mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 05:05:52 +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::errors::Error;
|
||||||
use crate::management::{delete_key, Action, Pending};
|
use crate::management::{delete_key, Action, Pending};
|
||||||
use crate::pending_path;
|
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::utils::{get_email_from_cert, parse_pem};
|
||||||
use crate::PENDING_FOLDER;
|
use crate::PENDING_FOLDER;
|
||||||
|
|
||||||
use lettre::transport::smtp::authentication::Credentials;
|
use lettre::{Message, Transport};
|
||||||
use lettre::{Message, SmtpTransport, Transport};
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
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> {
|
pub fn send_confirmation_email(email: &str, action: &Action, token: &str) -> Result<(), Error> {
|
||||||
println!("Sending mail, token: {}", &token);
|
|
||||||
let email = Message::builder()
|
let email = Message::builder()
|
||||||
.from(match SETTINGS.mail_settings.mail_from.parse() {
|
.from(match SETTINGS.mail_settings.mail_from.parse() {
|
||||||
Ok(mailbox) => mailbox,
|
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)
|
.subject(&SETTINGS.mail_settings.mail_subject)
|
||||||
.body(format!("{action} - {token}"));
|
.body(format!("{action} - {token}"));
|
||||||
|
|
||||||
let message = match email {
|
let message = match email {
|
||||||
Ok(message) => message,
|
Ok(message) => message,
|
||||||
Err(_) => return Err(Error::MailGeneration),
|
Err(_) => return Err(Error::MailGeneration),
|
||||||
};
|
};
|
||||||
let creds = Credentials::new(
|
|
||||||
SETTINGS.mail_settings.smtp_username.to_owned(),
|
match MAILER.send(&message) {
|
||||||
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) {
|
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(Error::SendMail),
|
Err(_) => Err(Error::SendMail),
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,6 @@ pub enum Error {
|
||||||
MissingMail,
|
MissingMail,
|
||||||
#[error("EE1: Error while sending the E-Mail")]
|
#[error("EE1: Error while sending the E-Mail")]
|
||||||
SendMail,
|
SendMail,
|
||||||
#[error("EE2: Error while building the SMTP connection")]
|
|
||||||
SmtpBuilder,
|
|
||||||
#[error("ES1: rror while serializing data")]
|
#[error("ES1: rror while serializing data")]
|
||||||
SerializeData,
|
SerializeData,
|
||||||
#[error("ES2: Error while deserializing data")]
|
#[error("ES2: Error while deserializing data")]
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use lettre::{transport::smtp::authentication::Credentials, SmtpTransport};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use sequoia_net::wkd::Variant;
|
use sequoia_net::wkd::Variant;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -40,7 +41,7 @@ pub enum SMTPEncryption {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_settings() -> Settings {
|
fn get_settings() -> Settings {
|
||||||
println!("Reaing settings...");
|
println!("Reading settings...");
|
||||||
let content = match fs::read_to_string("wkd.toml") {
|
let content = match fs::read_to_string("wkd.toml") {
|
||||||
Ok(content) => content,
|
Ok(content) => content,
|
||||||
Err(_) => panic!("Unable to access settings file!"),
|
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 SETTINGS: Lazy<Settings> = Lazy::new(get_settings);
|
||||||
|
pub static MAILER: Lazy<SmtpTransport> = Lazy::new(get_mailer);
|
||||||
|
|
Loading…
Reference in a new issue