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