@@ -6,17 +6,28 @@ defmodule RetWeb.Email do
6
6
app_name = AppConfig . get_cached_config_value ( "translations|en|app-name" )
7
7
app_full_name = AppConfig . get_cached_config_value ( "translations|en|app-full-name" ) || app_name
8
8
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 )
9
24
10
25
email =
11
26
new_email ( )
12
27
|> to ( to_address )
13
28
|> 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 )
20
31
21
32
if admin_email do
22
33
email |> put_header ( "Return-Path" , admin_email )
@@ -25,6 +36,20 @@ defmodule RetWeb.Email do
25
36
end
26
37
end
27
38
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
+
28
53
def enabled? do
29
54
! ! Application . get_env ( :ret , Ret.Mailer ) [ :adapter ]
30
55
end
0 commit comments