mirror of
https://github.com/lldap/lldap.git
synced 2026-07-25 17:58:44 +00:00
server: Send an email with the test command
This commit is contained in:
committed by
nitnelave
parent
2a90443ed8
commit
617a0f53fa
@@ -0,0 +1,31 @@
|
||||
use crate::infra::configuration::MailOptions;
|
||||
use anyhow::Result;
|
||||
use lettre::{
|
||||
message::Mailbox, transport::smtp::authentication::Credentials, Message, SmtpTransport,
|
||||
Transport,
|
||||
};
|
||||
use log::debug;
|
||||
|
||||
pub fn send_test_email(to: Mailbox, options: &MailOptions) -> Result<()> {
|
||||
let from = options
|
||||
.from
|
||||
.clone()
|
||||
.unwrap_or_else(|| "LLDAP <nobody@lldap>".parse().unwrap());
|
||||
let reply_to = options.reply_to.clone().unwrap_or_else(|| from.clone());
|
||||
debug!(
|
||||
"Sending email to '{}' as '{}' via '{}'@'{}':'{}'",
|
||||
&to, &from, &options.user, &options.server, options.port
|
||||
);
|
||||
let email = Message::builder()
|
||||
.from(from)
|
||||
.reply_to(reply_to)
|
||||
.to(to)
|
||||
.subject("LLDAP test email")
|
||||
.body("The test is successful! You can send emails from LLDAP".to_string())?;
|
||||
let creds = Credentials::new(options.user.clone(), options.password.clone());
|
||||
let mailer = SmtpTransport::relay(&options.server)?
|
||||
.credentials(creds)
|
||||
.build();
|
||||
mailer.send(&email)?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user