From 5f61607419fb329442ebbdd98832cb14349d33ab Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 26 Feb 2020 11:02:22 +0100 Subject: [PATCH] Added SMTP test button in the admin gui - Added a test button for checking the e-mail settings. - Fixed a bug with the _post JavaScript function: A function was overwriten with a variable and errors were not handled correctly like a 500 for example. --- src/api/admin.rs | 13 ++ src/config.rs | 1 + src/mail.rs | 11 ++ src/static/templates/admin/page.hbs | 47 +++++-- src/static/templates/email/smtp_test.hbs | 8 ++ src/static/templates/email/smtp_test.html.hbs | 129 ++++++++++++++++++ 6 files changed, 201 insertions(+), 8 deletions(-) create mode 100644 src/static/templates/email/smtp_test.hbs create mode 100644 src/static/templates/email/smtp_test.html.hbs diff --git a/src/api/admin.rs b/src/api/admin.rs index c4fad11..19a18f9 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -34,6 +34,7 @@ pub fn routes() -> Vec { post_config, delete_config, backup_db, + test_smtp, ] } @@ -164,6 +165,18 @@ fn invite_user(data: Json, _token: AdminToken, conn: DbConn) -> Empt } } +#[post("/test/smtp", data = "")] +fn test_smtp(data: Json, _token: AdminToken) -> EmptyResult { + let data: InviteData = data.into_inner(); + let email = data.email.clone(); + + if CONFIG.mail_enabled() { + mail::send_test(&email) + } else { + err!("Mail is not enabled") + } +} + #[get("/logout")] fn logout(mut cookies: Cookies) -> Result { cookies.remove(Cookie::named(COOKIE_NAME)); diff --git a/src/config.rs b/src/config.rs index 2c53a34..34f7f3a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -634,6 +634,7 @@ where reg!("email/verify_email", ".html"); reg!("email/welcome", ".html"); reg!("email/welcome_must_verify", ".html"); + reg!("email/smtp_test", ".html"); reg!("admin/base"); reg!("admin/login"); diff --git a/src/mail.rs b/src/mail.rs index 6e49f8f..77eb2ac 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -258,6 +258,17 @@ pub fn send_change_email(address: &str, token: &str) -> EmptyResult { send_email(&address, &subject, &body_html, &body_text) } +pub fn send_test(address: &str) -> EmptyResult { + let (subject, body_html, body_text) = get_text( + "email/smtp_test", + json!({ + "url": CONFIG.domain(), + }), + )?; + + send_email(&address, &subject, &body_html, &body_text) +} + fn send_email(address: &str, subject: &str, body_html: &str, body_text: &str) -> EmptyResult { let address_split: Vec<&str> = address.rsplitn(2, '@').collect(); if address_split.len() != 2 { diff --git a/src/static/templates/admin/page.hbs b/src/static/templates/admin/page.hbs index 1001c70..6136f4a 100644 --- a/src/static/templates/admin/page.hbs +++ b/src/static/templates/admin/page.hbs @@ -71,6 +71,17 @@ them to avoid confusion. This does not apply to the read-only section, which can only be set through the environment. + +
+
SMTP Test
+ Email: + +
+ + +
+
+
{{#each config}} {{#if groupdoc}} @@ -213,13 +224,24 @@ mode: "same-origin", credentials: "same-origin", headers: { "Content-Type": "application/json" } - }).then(e => { - if (e.ok) { return msg(successMsg); } - e.json().then(json => { - const msg = json ? json.ErrorModel.Message : "Unknown error"; - msg(errMsg + ": " + msg); - }); - }).catch(e => { msg(errMsg + ": Unknown error") }); + }).then( resp => { + if (resp.ok) { msg(successMsg); return Promise.reject({error: false}); } + respStatus = resp.status; + respStatusText = resp.statusText; + return resp.text(); + }).then( respText => { + try { + const respJson = JSON.parse(respText); + return respJson ? respJson.ErrorModel.Message : "Unknown error"; + } catch (e) { + return Promise.reject({body:respStatus + ' - ' + respStatusText, error: true}); + } + }).then( apiMsg => { + msg(errMsg + "\n" + apiMsg); + }).catch( e => { + if (e.error === false) { return true; } + else { msg(errMsg + "\n" + e.body); } + }); } function deleteUser(id, mail) { var input_mail = prompt("To delete user '" + mail + "', please type the email below") @@ -260,6 +282,15 @@ "Error inviting user", data); return false; } + function smtpTest() { + inv = document.getElementById("smtp-test-email"); + data = JSON.stringify({ "email": inv.value }); + inv.value = ""; + _post("/admin/test/smtp/", + "SMTP Test email sent correctly", + "Error sending SMTP test email", data); + return false; + } function getFormData() { let data = {}; @@ -331,7 +362,7 @@ e.title = orgtype.name; }); - // These are formatted because otherwise the + // These are formatted because otherwise the // VSCode formatter breaks But they still work // {{#each config}} {{#if grouptoggle}} masterCheck("input_{{grouptoggle}}", "#g_{{group}} input"); diff --git a/src/static/templates/email/smtp_test.hbs b/src/static/templates/email/smtp_test.hbs new file mode 100644 index 0000000..9d16d22 --- /dev/null +++ b/src/static/templates/email/smtp_test.hbs @@ -0,0 +1,8 @@ +Bitwarden_rs SMTP Test + + +

+This is a test email to verify the SMTP configuration for {{url}}. +

+

When you can read this email it is probably configured correctly.

+ diff --git a/src/static/templates/email/smtp_test.html.hbs b/src/static/templates/email/smtp_test.html.hbs new file mode 100644 index 0000000..154bda0 --- /dev/null +++ b/src/static/templates/email/smtp_test.html.hbs @@ -0,0 +1,129 @@ +Bitwarden_rs SMTP Test + + + + + + Bitwarden_rs + + + + + + + + + + +
+ + + + +
+ + + + +
+ + + + + + + +
+ This is a test email to verify the SMTP configuration for {{url}}. +
+ When you can read this email it is probably configured correctly. +
+
+ + + + + +
+
+ +