From 44cc2aa31358661ed6c0705fc0811602e205f624 Mon Sep 17 00:00:00 2001 From: robin Date: Wed, 7 Oct 2020 16:35:40 -0700 Subject: [PATCH 1/7] added logic for custom email message and subject --- lib/ret_web/email.ex | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/ret_web/email.ex b/lib/ret_web/email.ex index 0dea46397..7a032b4be 100644 --- a/lib/ret_web/email.ex +++ b/lib/ret_web/email.ex @@ -25,6 +25,19 @@ defmodule RetWeb.Email do end end + def get_magic_link_email() do + # if custom_email_subject is null or "" use default else use custom_email_subject + # if custom_email_message is null or "" use default else use custom_email_message + custom_email_subject = Application.get_env(:ret, Ret.Account)[:custom_email_subject] + custom_email_message = Application.get_env(:ret, Ret.Account)[:custom_email_message] + + if config do + + else + + end + end + def enabled? do !!Application.get_env(:ret, Ret.Mailer)[:adapter] end From a6d10fbff5409b7b3888fc4627f5255f57bba9f6 Mon Sep 17 00:00:00 2001 From: robin Date: Wed, 7 Oct 2020 17:27:00 -0700 Subject: [PATCH 2/7] custom email logic integrated --- habitat/config/config.toml | 2 ++ lib/ret_web/email.ex | 42 +++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/habitat/config/config.toml b/habitat/config/config.toml index e937f483c..88636776d 100644 --- a/habitat/config/config.toml +++ b/habitat/config/config.toml @@ -242,6 +242,8 @@ server = {{ toToml cfg.email.server }} port = {{ toToml cfg.email.port }} username = {{ toToml cfg.email.username }} password = {{ toToml cfg.email.password }} +custom_email_subject = {{ toToml cfg.email.custom_email_subject }} +custom_email_message = {{ toToml cfg.email.custom_email_message }} [ret."Elixir.Ret.Support"] slack_webhook_url = "{{ cfg.support.slack_webhook_url }}" diff --git a/lib/ret_web/email.ex b/lib/ret_web/email.ex index 7a032b4be..2eab621b1 100644 --- a/lib/ret_web/email.ex +++ b/lib/ret_web/email.ex @@ -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_email_subject = Application.get_env(:ret, Ret.Mailer)[:custom_email_subject] + custom_email_message = Application.get_env(:ret, Ret.Mailer)[:custom_email_message] + + email_subject = + if string_is_nil_or_empty(custom_email_subject), + do: "Your #{app_name} Sign-In Link", + else: custom_email_subject + + email_body = + if string_is_nil_or_empty(custom_email_message), + 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_email_body(custom_email_message, 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) @@ -25,16 +36,15 @@ defmodule RetWeb.Email do end end - def get_magic_link_email() do - # if custom_email_subject is null or "" use default else use custom_email_subject - # if custom_email_message is null or "" use default else use custom_email_message - custom_email_subject = Application.get_env(:ret, Ret.Account)[:custom_email_subject] - custom_email_message = Application.get_env(:ret, Ret.Account)[:custom_email_message] - - if config do + 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_email_body(custom_message, signin_args) do + if match?(~r/{{ token }}/, custom_message) do + replace?(~r/{{ token }}/, custom_message, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}") else - + custom_message + "\n\n #{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}" end end @@ -45,4 +55,8 @@ defmodule RetWeb.Email do defp from_address do Application.get_env(:ret, __MODULE__)[:from] end + + defp module_config(key) do + Application.get_env(:ret, __MODULE__)[key] + end end From 318d60f1b67d86e0899e6cee37261027bf02ba43 Mon Sep 17 00:00:00 2001 From: robin Date: Fri, 9 Oct 2020 16:28:40 -0700 Subject: [PATCH 3/7] subject and email body working --- lib/ret_web/email.ex | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/ret_web/email.ex b/lib/ret_web/email.ex index 2eab621b1..21906c52b 100644 --- a/lib/ret_web/email.ex +++ b/lib/ret_web/email.ex @@ -6,21 +6,21 @@ 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_email_subject = Application.get_env(:ret, Ret.Mailer)[:custom_email_subject] - custom_email_message = Application.get_env(:ret, Ret.Mailer)[:custom_email_message] + custom_login_subject = AppConfig.get_config_value("email|login-subject") + custom_login_body = AppConfig.get_config_value("email|login_body") email_subject = - if string_is_nil_or_empty(custom_email_subject), + if string_is_nil_or_empty(custom_login_subject), do: "Your #{app_name} Sign-In Link", - else: custom_email_subject + else: custom_login_subject email_body = - if string_is_nil_or_empty(custom_email_message), + 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_email_body(custom_email_message, signin_args) + else: add_magic_link_to_custom_login_body(custom_login_body, signin_args) email = new_email() @@ -40,11 +40,11 @@ defmodule RetWeb.Email do check_string == nil || String.length(String.trim(check_string)) == 0 end - defp add_magic_link_to_custom_email_body(custom_message, signin_args) do - if match?(~r/{{ token }}/, custom_message) do - replace?(~r/{{ token }}/, custom_message, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}") + defp add_magic_link_to_custom_login_body(custom_message, signin_args) do + if Regex.match?(~r/{{ link }}/, custom_message) do + Regex.replace(~r/{{ link }}/, custom_message, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}") else - custom_message + "\n\n #{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}" + custom_message <> "\n\n #{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}" end end @@ -56,7 +56,4 @@ defmodule RetWeb.Email do Application.get_env(:ret, __MODULE__)[:from] end - defp module_config(key) do - Application.get_env(:ret, __MODULE__)[key] - end end From 5793057e457904636eb4327073a478f3e8ba4a2d Mon Sep 17 00:00:00 2001 From: robin Date: Fri, 9 Oct 2020 17:46:12 -0700 Subject: [PATCH 4/7] custom email works --- lib/ret_web/email.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ret_web/email.ex b/lib/ret_web/email.ex index 21906c52b..bfa2bda25 100644 --- a/lib/ret_web/email.ex +++ b/lib/ret_web/email.ex @@ -6,8 +6,8 @@ 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_config_value("email|login-subject") - custom_login_body = AppConfig.get_config_value("email|login_body") + 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), @@ -42,7 +42,7 @@ defmodule RetWeb.Email do defp add_magic_link_to_custom_login_body(custom_message, signin_args) do if Regex.match?(~r/{{ link }}/, custom_message) do - Regex.replace(~r/{{ link }}/, custom_message, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}") + Regex.replace(~r/{{ link }}/, custom_message, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}", global: false) else custom_message <> "\n\n #{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}" end From bb7cf47afbecbc43483e513bfbfb3d2a52dab797 Mon Sep 17 00:00:00 2001 From: robin Date: Mon, 12 Oct 2020 11:56:38 -0700 Subject: [PATCH 5/7] format --- lib/ret_web/email.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ret_web/email.ex b/lib/ret_web/email.ex index bfa2bda25..245ca7d42 100644 --- a/lib/ret_web/email.ex +++ b/lib/ret_web/email.ex @@ -42,7 +42,9 @@ defmodule RetWeb.Email do defp add_magic_link_to_custom_login_body(custom_message, signin_args) do if Regex.match?(~r/{{ link }}/, custom_message) do - Regex.replace(~r/{{ link }}/, custom_message, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}", global: false) + Regex.replace(~r/{{ link }}/, custom_message, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}", + global: false + ) else custom_message <> "\n\n #{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}" end @@ -55,5 +57,4 @@ defmodule RetWeb.Email do defp from_address do Application.get_env(:ret, __MODULE__)[:from] end - end From 52e4d7c2c8c793dfbfba474b74c59567d29a9bb9 Mon Sep 17 00:00:00 2001 From: robin Date: Mon, 19 Oct 2020 13:45:10 -0700 Subject: [PATCH 6/7] removed from config.toml + add with statement --- habitat/config/config.toml | 2 -- lib/ret_web/email.ex | 33 ++++++++++++++++++++------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/habitat/config/config.toml b/habitat/config/config.toml index 88636776d..e937f483c 100644 --- a/habitat/config/config.toml +++ b/habitat/config/config.toml @@ -242,8 +242,6 @@ server = {{ toToml cfg.email.server }} port = {{ toToml cfg.email.port }} username = {{ toToml cfg.email.username }} password = {{ toToml cfg.email.password }} -custom_email_subject = {{ toToml cfg.email.custom_email_subject }} -custom_email_message = {{ toToml cfg.email.custom_email_message }} [ret."Elixir.Ret.Support"] slack_webhook_url = "{{ cfg.support.slack_webhook_url }}" diff --git a/lib/ret_web/email.ex b/lib/ret_web/email.ex index 245ca7d42..2f136b88e 100644 --- a/lib/ret_web/email.ex +++ b/lib/ret_web/email.ex @@ -6,21 +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 + with custom_login_subject <- + AppConfig.get_config_value("auth|login_subject"), + false <- string_is_nil_or_empty(custom_login_subject) do + custom_login_subject + else + _ -> + "Your #{app_name} Sign-In Link" + end email_body = - if string_is_nil_or_empty(custom_login_body), - do: + with custom_login_body <- + AppConfig.get_cached_config_value("auth|login_body"), + false <- string_is_nil_or_empty(custom_login_body) do + add_magic_link_to_custom_login_body(custom_login_body, signin_args) + else + _ -> "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) + }/?#{URI.encode_query(signin_args)}" + end email = new_email() @@ -41,12 +48,12 @@ defmodule RetWeb.Email do 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, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}", - global: false - ) + Regex.replace(~r/{{ link }}/, custom_message, magic_link, global: false) else - custom_message <> "\n\n #{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}" + custom_message <> "\n\n" <> magic_link end end From b5e2017dfa3e35a34a589d296344a29b9b105551 Mon Sep 17 00:00:00 2001 From: robin Date: Mon, 19 Oct 2020 14:21:44 -0700 Subject: [PATCH 7/7] remove with --- lib/ret_web/email.ex | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/ret_web/email.ex b/lib/ret_web/email.ex index 2f136b88e..1a382b3e9 100644 --- a/lib/ret_web/email.ex +++ b/lib/ret_web/email.ex @@ -6,28 +6,21 @@ 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 = - with custom_login_subject <- - AppConfig.get_config_value("auth|login_subject"), - false <- string_is_nil_or_empty(custom_login_subject) do - custom_login_subject - else - _ -> - "Your #{app_name} Sign-In Link" - end + if string_is_nil_or_empty(custom_login_subject), + do: "Your #{app_name} Sign-In Link", + else: custom_login_subject email_body = - with custom_login_body <- - AppConfig.get_cached_config_value("auth|login_body"), - false <- string_is_nil_or_empty(custom_login_body) do - add_magic_link_to_custom_login_body(custom_login_body, signin_args) - else - _ -> + 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)}" - end + }/?#{URI.encode_query(signin_args)}", + else: add_magic_link_to_custom_login_body(custom_login_body, signin_args) email = new_email() @@ -51,7 +44,7 @@ defmodule RetWeb.Email 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, global: false) + Regex.replace(~r/{{ link }}/, custom_message, magic_link) else custom_message <> "\n\n" <> magic_link end