1
0
Fork 0

Implemented HTML emails with text alternative

Dieser Commit ist enthalten in:
Daniel García 2019-02-10 19:12:34 +01:00
Ursprung 22b0b95209
Commit d086a99e5b
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: FC8A7D14C3CD543A
2 geänderte Dateien mit 28 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -426,14 +426,18 @@ fn load_templates(path: &str) -> Handlebars {
let template = include_str!(concat!("static/templates/", $name, ".hbs")); let template = include_str!(concat!("static/templates/", $name, ".hbs"));
hb.register_template_string($name, template).unwrap(); hb.register_template_string($name, template).unwrap();
}}; }};
($name:expr, $ext:expr) => {{
reg!($name);
reg!(concat!($name, $ext));
}};
} }
// First register default templates here // First register default templates here
reg!("email/invite_accepted"); reg!("email/invite_accepted", ".html");
reg!("email/invite_confirmed"); reg!("email/invite_confirmed", ".html");
reg!("email/pw_hint_none"); reg!("email/pw_hint_none", ".html");
reg!("email/pw_hint_some"); reg!("email/pw_hint_some", ".html");
reg!("email/send_org_invite"); reg!("email/send_org_invite", ".html");
reg!("admin/base"); reg!("admin/base");
reg!("admin/login"); reg!("admin/login");

Datei anzeigen

@ -36,8 +36,14 @@ fn mailer() -> SmtpTransport {
.transport() .transport()
} }
fn get_text(template_name: &'static str, data: serde_json::Value) -> Result<(String, String), Error> { fn get_text(template_name: &'static str, data: serde_json::Value) -> Result<(String, String, String), Error> {
let text = CONFIG.render_template(template_name, &data)?; let (subject_html, body_html) = get_template(&format!("{}.html", template_name), &data)?;
let (_subject_text, body_text) = get_template(template_name, &data)?;
Ok((subject_html, body_html, body_text))
}
fn get_template(template_name: &str, data: &serde_json::Value) -> Result<(String, String), Error> {
let text = CONFIG.render_template(template_name, data)?;
let mut text_split = text.split("<!---------------->"); let mut text_split = text.split("<!---------------->");
let subject = match text_split.next() { let subject = match text_split.next() {
@ -60,9 +66,9 @@ pub fn send_password_hint(address: &str, hint: Option<String>) -> EmptyResult {
"email/pw_hint_none" "email/pw_hint_none"
}; };
let (subject, body) = get_text(template_name, json!({ "hint": hint }))?; let (subject, body_html, body_text) = get_text(template_name, json!({ "hint": hint }))?;
send_email(&address, &subject, &body) send_email(&address, &subject, &body_html, &body_text)
} }
pub fn send_invite( pub fn send_invite(
@ -82,7 +88,7 @@ pub fn send_invite(
); );
let invite_token = encode_jwt(&claims); let invite_token = encode_jwt(&claims);
let (subject, body) = get_text( let (subject, body_html, body_text) = get_text(
"email/send_org_invite", "email/send_org_invite",
json!({ json!({
"url": CONFIG.domain(), "url": CONFIG.domain(),
@ -94,11 +100,11 @@ pub fn send_invite(
}), }),
)?; )?;
send_email(&address, &subject, &body) send_email(&address, &subject, &body_html, &body_text)
} }
pub fn send_invite_accepted(new_user_email: &str, address: &str, org_name: &str) -> EmptyResult { pub fn send_invite_accepted(new_user_email: &str, address: &str, org_name: &str) -> EmptyResult {
let (subject, body) = get_text( let (subject, body_html, body_text) = get_text(
"email/invite_accepted", "email/invite_accepted",
json!({ json!({
"url": CONFIG.domain(), "url": CONFIG.domain(),
@ -107,11 +113,11 @@ pub fn send_invite_accepted(new_user_email: &str, address: &str, org_name: &str)
}), }),
)?; )?;
send_email(&address, &subject, &body) send_email(&address, &subject, &body_html, &body_text)
} }
pub fn send_invite_confirmed(address: &str, org_name: &str) -> EmptyResult { pub fn send_invite_confirmed(address: &str, org_name: &str) -> EmptyResult {
let (subject, body) = get_text( let (subject, body_html, body_text) = get_text(
"email/invite_confirmed", "email/invite_confirmed",
json!({ json!({
"url": CONFIG.domain(), "url": CONFIG.domain(),
@ -119,16 +125,15 @@ pub fn send_invite_confirmed(address: &str, org_name: &str) -> EmptyResult {
}), }),
)?; )?;
send_email(&address, &subject, &body) send_email(&address, &subject, &body_html, &body_text)
} }
fn send_email(address: &str, subject: &str, body: &str) -> EmptyResult { fn send_email(address: &str, subject: &str, body_html: &str, body_text: &str) -> EmptyResult {
let email = EmailBuilder::new() let email = EmailBuilder::new()
.to(address) .to(address)
.from((CONFIG.smtp_from().as_str(), CONFIG.smtp_from_name().as_str())) .from((CONFIG.smtp_from().as_str(), CONFIG.smtp_from_name().as_str()))
.subject(subject) .subject(subject)
.header(("Content-Type", "text/html")) .alternative(body_html, body_text)
.body(body)
.build() .build()
.map_err(|e| Error::new("Error building email", e.to_string()))?; .map_err(|e| Error::new("Error building email", e.to_string()))?;