From 8435db5cc84d9ce0be2e3f5d4f7307ba4910e5ad Mon Sep 17 00:00:00 2001 From: BlackDex Date: Mon, 19 May 2025 21:47:00 +0200 Subject: [PATCH] Send an email when an account exists already When you want to change your email address into an account which already exists, upstream sends an email to the existing account. Lets do the same. Kinda fixes #5630 Signed-off-by: BlackDex --- src/api/core/accounts.rs | 5 +++++ src/config.rs | 1 + src/mail.rs | 14 ++++++++++++++ .../templates/email/change_email_existing.hbs | 6 ++++++ .../email/change_email_existing.html.hbs | 16 ++++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 src/static/templates/email/change_email_existing.hbs create mode 100644 src/static/templates/email/change_email_existing.html.hbs diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs index 1dfafca8..35d177d3 100644 --- a/src/api/core/accounts.rs +++ b/src/api/core/accounts.rs @@ -775,6 +775,11 @@ async fn post_email_token(data: Json, headers: Headers, mut conn } if User::find_by_mail(&data.new_email, &mut conn).await.is_some() { + if CONFIG.mail_enabled() { + if let Err(e) = mail::send_change_email_existing(&data.new_email, &user.email).await { + error!("Error sending change-email-existing email: {e:#?}"); + } + } err!("Email already in use"); } diff --git a/src/config.rs b/src/config.rs index bfe94a8f..847c3119 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1384,6 +1384,7 @@ where reg!("email/email_footer_text"); reg!("email/admin_reset_password", ".html"); + reg!("email/change_email_existing", ".html"); reg!("email/change_email", ".html"); reg!("email/delete_account", ".html"); reg!("email/emergency_access_invite_accepted", ".html"); diff --git a/src/mail.rs b/src/mail.rs index 8100815e..b1f37886 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -570,6 +570,20 @@ pub async fn send_change_email(address: &str, token: &str) -> EmptyResult { send_email(address, &subject, body_html, body_text).await } +pub async fn send_change_email_existing(address: &str, acting_address: &str) -> EmptyResult { + let (subject, body_html, body_text) = get_text( + "email/change_email_existing", + json!({ + "url": CONFIG.domain(), + "img_src": CONFIG._smtp_img_src(), + "existing_address": address, + "acting_address": acting_address, + }), + )?; + + send_email(address, &subject, body_html, body_text).await +} + pub async fn send_test(address: &str) -> EmptyResult { let (subject, body_html, body_text) = get_text( "email/smtp_test", diff --git a/src/static/templates/email/change_email_existing.hbs b/src/static/templates/email/change_email_existing.hbs new file mode 100644 index 00000000..ccbcb6ad --- /dev/null +++ b/src/static/templates/email/change_email_existing.hbs @@ -0,0 +1,6 @@ +Your Email Change + +A user ({{ acting_address }}) recently tried to change their account to use this email address ({{ existing_address }}). An account already exists with this email ({{ existing_address }}). + +If you did not try to change an email address, contact your administrator. +{{> email/email_footer_text }} diff --git a/src/static/templates/email/change_email_existing.html.hbs b/src/static/templates/email/change_email_existing.html.hbs new file mode 100644 index 00000000..aa15f612 --- /dev/null +++ b/src/static/templates/email/change_email_existing.html.hbs @@ -0,0 +1,16 @@ +Your Email Change + +{{> email/email_header }} + + + + + + + +
+ A user ({{ acting_address }}) recently tried to change their account to use this email address ({{ existing_address }}). An account already exists with this email ({{ existing_address }}). +
+ If you did not try to change an email address, contact your administrator. +
+{{> email/email_footer }}