use lettre::sendmail::SendmailTransport; use lettre::{SendableEmail, Transport}; use lettre_email::Email; use crate::model::config::Config; static FROM: &str = "contact@guyonvarch.me"; pub fn send( config: &Config, to: Vec<(String, String)>, subject: String, message: String, ) -> bool { match prepare_email(to.clone(), subject.clone(), message.clone()) { Ok(email) => { if config.mock_mails { let formatted_to = to .into_iter() .map(|t| t.0) .collect::>() .join(", "); info!( "MOCK MAIL\nto: {}\nsubject: {}\n\n{}", formatted_to, subject, message ); true } else { let mut sender = SendmailTransport::new_with_command(&config.sendmail_path); match sender.send(email) { Ok(_) => true, Err(err) => { error!("Error sending email: {:?}", err); false } } } } Err(err) => { error!("Error preparing email: {:?}", err); false } } } fn prepare_email( to: Vec<(String, String)>, subject: String, message: String, ) -> Result { let mut email = Email::builder().from(FROM).subject(subject).text(message); for (address, name) in to.iter() { email = email.to((address, name)); } email.build().map(|e| e.into()) }