Add asynchronous call to database.

See file 'asynchronous.ts'.
This commit is contained in:
Greg Burri 2022-11-29 15:58:06 +01:00
parent 8a3fef096d
commit d28e765e39
20 changed files with 1323 additions and 1049 deletions

View file

@ -1,7 +1,35 @@
use std::time::Duration;
use derive_more::Display;
use lettre::{transport::smtp::authentication::Credentials, Message, SmtpTransport, Transport};
///
pub fn send_validation(site_url: &str, email: &str, token: &str, smtp_login: &str, smtp_password: &str) -> Result<(), Box<dyn std::error::Error>> {
use crate::consts;
#[derive(Debug, Display)]
pub enum Error {
ParseError(lettre::address::AddressError),
SmtpError(lettre::transport::smtp::Error),
Email(lettre::error::Error),
}
impl From<lettre::address::AddressError> for Error {
fn from(error: lettre::address::AddressError) -> Self {
Error::ParseError(error)
}
}
impl From<lettre::transport::smtp::Error> for Error {
fn from(error: lettre::transport::smtp::Error) -> Self {
Error::SmtpError(error)
}
}
impl From<lettre::error::Error> for Error {
fn from(error: lettre::error::Error) -> Self {
Error::Email(error)
}
}
pub fn send_validation(site_url: &str, email: &str, token: &str, smtp_login: &str, smtp_password: &str) -> Result<(), Error> {
let email = Message::builder()
.message_id(None)
.from("recipes@gburri.org".parse()?)
@ -11,7 +39,7 @@ pub fn send_validation(site_url: &str, email: &str, token: &str, smtp_login: &st
let credentials = Credentials::new(smtp_login.to_string(), smtp_password.to_string());
let mailer = SmtpTransport::relay("mail.gandi.net")?.credentials(credentials).build();
let mailer = SmtpTransport::relay("mail.gandi.net")?.credentials(credentials).timeout(Some(consts::SEND_EMAIL_TIMEOUT)).build();
if let Err(error) = mailer.send(&email) {
eprintln!("Error when sending E-mail:\n{:?}", &error);