Skip to content

Commit 22aec15

Browse files
authored
Merge pull request #418 from mozilla/custom-login-email
Able to customize magic link login email message + subject from admin panel
2 parents 1232401 + b5e2017 commit 22aec15

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

lib/ret_web/email.ex

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,28 @@ defmodule RetWeb.Email do
66
app_name = AppConfig.get_cached_config_value("translations|en|app-name")
77
app_full_name = AppConfig.get_cached_config_value("translations|en|app-full-name") || app_name
88
admin_email = Application.get_env(:ret, Ret.Account)[:admin_email]
9+
custom_login_subject = AppConfig.get_cached_config_value("auth|login_subject")
10+
custom_login_body = AppConfig.get_cached_config_value("auth|login_body")
11+
12+
email_subject =
13+
if string_is_nil_or_empty(custom_login_subject),
14+
do: "Your #{app_name} Sign-In Link",
15+
else: custom_login_subject
16+
17+
email_body =
18+
if string_is_nil_or_empty(custom_login_body),
19+
do:
20+
"To sign-in to #{app_name}, please visit the link below. If you did not make this request, please ignore this e-mail.\n\n #{
21+
RetWeb.Endpoint.url()
22+
}/?#{URI.encode_query(signin_args)}",
23+
else: add_magic_link_to_custom_login_body(custom_login_body, signin_args)
924

1025
email =
1126
new_email()
1227
|> to(to_address)
1328
|> from({app_full_name, from_address()})
14-
|> subject("Your #{app_name} Sign-In Link")
15-
|> text_body(
16-
"To sign-in to #{app_name}, please visit the link below. If you did not make this request, please ignore this e-mail.\n\n #{
17-
RetWeb.Endpoint.url()
18-
}/?#{URI.encode_query(signin_args)}"
19-
)
29+
|> subject(email_subject)
30+
|> text_body(email_body)
2031

2132
if admin_email do
2233
email |> put_header("Return-Path", admin_email)
@@ -25,6 +36,20 @@ defmodule RetWeb.Email do
2536
end
2637
end
2738

39+
defp string_is_nil_or_empty(check_string) do
40+
check_string == nil || String.length(String.trim(check_string)) == 0
41+
end
42+
43+
defp add_magic_link_to_custom_login_body(custom_message, signin_args) do
44+
magic_link = "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}"
45+
46+
if Regex.match?(~r/{{ link }}/, custom_message) do
47+
Regex.replace(~r/{{ link }}/, custom_message, magic_link)
48+
else
49+
custom_message <> "\n\n" <> magic_link
50+
end
51+
end
52+
2853
def enabled? do
2954
!!Application.get_env(:ret, Ret.Mailer)[:adapter]
3055
end

0 commit comments

Comments
 (0)