Skip to content

Commit

Permalink
Merge pull request #418 from mozilla/custom-login-email
Browse files Browse the repository at this point in the history
Able to customize magic link login email message + subject from admin panel
  • Loading branch information
robinkwilson authored Oct 19, 2020
2 parents 1232401 + b5e2017 commit 22aec15
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions lib/ret_web/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ defmodule RetWeb.Email do
app_name = AppConfig.get_cached_config_value("translations|en|app-name")
app_full_name = AppConfig.get_cached_config_value("translations|en|app-full-name") || app_name
admin_email = Application.get_env(:ret, Ret.Account)[:admin_email]
custom_login_subject = AppConfig.get_cached_config_value("auth|login_subject")
custom_login_body = AppConfig.get_cached_config_value("auth|login_body")

email_subject =
if string_is_nil_or_empty(custom_login_subject),
do: "Your #{app_name} Sign-In Link",
else: custom_login_subject

email_body =
if string_is_nil_or_empty(custom_login_body),
do:
"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 #{
RetWeb.Endpoint.url()
}/?#{URI.encode_query(signin_args)}",
else: add_magic_link_to_custom_login_body(custom_login_body, signin_args)

email =
new_email()
|> to(to_address)
|> from({app_full_name, from_address()})
|> subject("Your #{app_name} Sign-In Link")
|> text_body(
"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 #{
RetWeb.Endpoint.url()
}/?#{URI.encode_query(signin_args)}"
)
|> subject(email_subject)
|> text_body(email_body)

if admin_email do
email |> put_header("Return-Path", admin_email)
Expand All @@ -25,6 +36,20 @@ defmodule RetWeb.Email do
end
end

defp string_is_nil_or_empty(check_string) do
check_string == nil || String.length(String.trim(check_string)) == 0
end

defp add_magic_link_to_custom_login_body(custom_message, signin_args) do
magic_link = "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}"

if Regex.match?(~r/{{ link }}/, custom_message) do
Regex.replace(~r/{{ link }}/, custom_message, magic_link)
else
custom_message <> "\n\n" <> magic_link
end
end

def enabled? do
!!Application.get_env(:ret, Ret.Mailer)[:adapter]
end
Expand Down

0 comments on commit 22aec15

Please sign in to comment.