2018-12-25 19:06:28 +00:00
|
|
|
use handlebars::Handlebars;
|
2019-02-07 19:58:31 +00:00
|
|
|
use lettre::{EmailTransport, SendmailTransport};
|
2018-11-02 10:50:57 +00:00
|
|
|
use lettre_email::EmailBuilder;
|
|
|
|
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use types::Email;
|
2019-02-07 19:58:31 +00:00
|
|
|
use Result;
|
2018-11-02 10:50:57 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Clone)]
|
2019-02-07 19:58:31 +00:00
|
|
|
pub struct Context {
|
2018-11-02 10:50:57 +00:00
|
|
|
pub token: String,
|
|
|
|
pub userid: String,
|
|
|
|
pub domain: String,
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
fn send_mail<T>(
|
|
|
|
to: &Email, subject: &str, mail_templates: &Handlebars, template: &str,
|
|
|
|
from: &str, ctx: T,
|
|
|
|
) -> Result<()>
|
|
|
|
where
|
|
|
|
T: Serialize + Clone,
|
2018-11-02 10:50:57 +00:00
|
|
|
{
|
2019-01-10 13:45:11 +00:00
|
|
|
let tmpl_html = format!("{}-html", template);
|
|
|
|
let tmpl_txt = format!("{}-txt", template);
|
2018-12-25 19:06:28 +00:00
|
|
|
let (html, txt) = {
|
2019-02-07 19:58:31 +00:00
|
|
|
if let (Ok(inner_html), Ok(inner_txt)) = (
|
|
|
|
mail_templates.render(&tmpl_html, &ctx),
|
|
|
|
mail_templates.render(&tmpl_txt, &ctx),
|
|
|
|
) {
|
|
|
|
(Some(inner_html), Some(inner_txt))
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
}
|
2018-12-25 19:06:28 +00:00
|
|
|
};
|
|
|
|
|
2018-11-02 10:50:57 +00:00
|
|
|
let email = EmailBuilder::new()
|
|
|
|
.to(to.to_string())
|
2019-01-09 12:35:47 +00:00
|
|
|
.from(from)
|
2018-11-02 10:50:57 +00:00
|
|
|
.subject(subject)
|
|
|
|
.alternative(
|
2019-02-22 20:29:53 +00:00
|
|
|
html.ok_or(failure::err_msg("Email template failed to render"))?,
|
|
|
|
txt.ok_or(failure::err_msg("Email template failed to render"))?,
|
2019-02-07 19:58:31 +00:00
|
|
|
)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2018-11-02 10:50:57 +00:00
|
|
|
|
|
|
|
let mut sender = SendmailTransport::new();
|
|
|
|
sender.send(&email)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
pub fn send_verification_mail(
|
|
|
|
userid: &Email, token: &str, mail_templates: &Handlebars, domain: &str,
|
|
|
|
from: &str,
|
|
|
|
) -> Result<()> {
|
|
|
|
let ctx = Context {
|
2018-11-02 10:50:57 +00:00
|
|
|
token: token.to_string(),
|
|
|
|
userid: userid.to_string(),
|
|
|
|
domain: domain.to_string(),
|
|
|
|
};
|
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
send_mail(
|
|
|
|
userid,
|
|
|
|
"Please verify your email address",
|
|
|
|
mail_templates,
|
|
|
|
"verify",
|
|
|
|
from,
|
|
|
|
ctx,
|
|
|
|
)
|
2018-11-02 10:50:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
pub fn send_confirmation_mail(
|
|
|
|
userid: &Email, token: &str, mail_templates: &Handlebars, domain: &str,
|
|
|
|
from: &str,
|
|
|
|
) -> Result<()> {
|
|
|
|
let ctx = Context {
|
2018-11-02 10:50:57 +00:00
|
|
|
token: token.to_string(),
|
|
|
|
userid: userid.to_string(),
|
|
|
|
domain: domain.to_string(),
|
|
|
|
};
|
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
send_mail(
|
|
|
|
userid,
|
|
|
|
"Please confirm deletion of your key",
|
|
|
|
mail_templates,
|
|
|
|
"confirm",
|
|
|
|
from,
|
|
|
|
ctx,
|
|
|
|
)
|
2018-11-02 10:50:57 +00:00
|
|
|
}
|