use crate::app_config::AppConfig; use lettre::message::header::ContentType; use lettre::transport::smtp::authentication::Credentials; use lettre::{Message, SmtpTransport, Transport}; use std::fmt::Display; pub async fn send_mail(to: &str, subject: &str, body: D) -> anyhow::Result<()> { let conf = AppConfig::get(); let email = Message::builder() .from(format!("GeneIT <{}>", conf.mail_sender).parse()?) .to(to.parse()?) .subject(subject) .header(ContentType::TEXT_PLAIN) .body(body.to_string())?; let mut mailer = match conf.smtp_tls { true => SmtpTransport::relay(&conf.smtp_relay)?, false => SmtpTransport::builder_dangerous(&conf.smtp_relay), } .port(conf.smtp_port); if let (Some(username), Some(password)) = (&conf.smtp_username, &conf.smtp_password) { mailer = mailer.credentials(Credentials::new(username.to_string(), password.to_string())) } let mailer = mailer.build(); mailer.send(&email)?; log::debug!("A mail was sent to {} (subject = {})", to, subject); Ok(()) }