1use lettre::transport::smtp::authentication::Credentials;
7use lettre::{Message, AsyncSmtpTransport, AsyncTransport, Tokio1Executor};
8use crate::app::Config;
9
10pub struct MailService;
11
12impl MailService {
13 pub async fn send_email(
15 to: &str,
16 subject: &str,
17 body: &str,
18 ) -> Result<(), Box<dyn std::error::Error>> {
19 let config = Config::load();
20
21 let email = Message::builder()
23 .from(format!("{} <{}>", config.mail_from_name, config.mail_from_address).parse()?)
24 .to(to.parse()?)
25 .subject(subject)
26 .header(lettre::message::header::ContentType::TEXT_HTML)
27 .body(body.to_string())?;
28
29 let creds = Credentials::new(config.mail_username, config.mail_password);
31
32 let mailer = AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(&config.mail_host)?
33 .port(config.mail_port)
34 .credentials(creds)
35 .build();
36
37 mailer.send(email).await?;
39
40 Ok(())
41 }
42}