X-Git-Url: http://git.euphorik.ch/?p=recipes.git;a=blobdiff_plain;f=backend%2Fsrc%2Femail.rs;fp=backend%2Fsrc%2Femail.rs;h=b571a292023cd83bcfc64b3264cb978f4a140311;hp=9b095d5f514b4442bb0c5a437279774507bed6f3;hb=d28e765e39e70ad2ab9a42885c786d5d8ba9ba40;hpb=8a3fef096d720666dc8a54789aee02250642d8a1 diff --git a/backend/src/email.rs b/backend/src/email.rs index 9b095d5..b571a29 100644 --- a/backend/src/email.rs +++ b/backend/src/email.rs @@ -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> { +use crate::consts; + +#[derive(Debug, Display)] +pub enum Error { + ParseError(lettre::address::AddressError), + SmtpError(lettre::transport::smtp::Error), + Email(lettre::error::Error), +} + +impl From for Error { + fn from(error: lettre::address::AddressError) -> Self { + Error::ParseError(error) + } +} + +impl From for Error { + fn from(error: lettre::transport::smtp::Error) -> Self { + Error::SmtpError(error) + } +} + +impl From 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);