server: Send an email for password resets

This commit is contained in:
Valentin Tolmer
2021-11-21 18:56:16 +01:00
committed by nitnelave
parent db2b5cbae0
commit 789c8f367e
3 changed files with 75 additions and 7 deletions
+34 -3
View File
@@ -6,7 +6,7 @@ use lettre::{
};
use log::debug;
pub fn send_test_email(to: Mailbox, options: &MailOptions) -> Result<()> {
fn send_email(to: Mailbox, subject: &str, body: String, options: &MailOptions) -> Result<()> {
let from = options
.from
.clone()
@@ -20,8 +20,8 @@ pub fn send_test_email(to: Mailbox, options: &MailOptions) -> Result<()> {
.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())?;
.subject(subject)
.body(body)?;
let creds = Credentials::new(
options.user.clone(),
options.password.unsecure().to_string(),
@@ -32,3 +32,34 @@ pub fn send_test_email(to: Mailbox, options: &MailOptions) -> Result<()> {
mailer.send(&email)?;
Ok(())
}
pub fn send_password_reset_email(
username: &str,
to: &str,
token: &str,
domain: &str,
options: &MailOptions,
) -> Result<()> {
let to = to.parse()?;
let body = format!(
"Hello {},
This email has been sent to you in order to validate your identity.
If you did not initiate the process your credentials might have been
compromised. You should reset your password and contact an administrator.
To reset your password please visit the following URL: {}/reset-password/step2/{}
Please contact an administrator if you did not initiate the process.",
username, domain, token
);
send_email(to, "[LLDAP] Password reset requested", body, options)
}
pub fn send_test_email(to: Mailbox, options: &MailOptions) -> Result<()> {
send_email(
to,
"LLDAP test email",
"The test is successful! You can send emails from LLDAP".to_string(),
options,
)
}