Generate reset password URL
This commit is contained in:
33
geneit_backend/src/services/mail_service.rs
Normal file
33
geneit_backend/src/services/mail_service.rs
Normal file
@ -0,0 +1,33 @@
|
||||
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<D: Display>(to: &str, subject: &str, body: D) -> anyhow::Result<()> {
|
||||
let conf = AppConfig::get();
|
||||
|
||||
let email = Message::builder()
|
||||
.from(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(())
|
||||
}
|
Reference in New Issue
Block a user