diff --git a/.env.example b/.env.example index d81ffb3..c79d863 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,15 @@ DATABASE_URL=ecto://postgres:postgres@db/decentralised_book_index_prod SECRET_KEY_BASE= TOKEN_SIGNING_SECRET= PHX_HOST=example.com +#PHX_HOST=127.0.0.1 +#PORT=4000 + +#SYSTEM_EMAIL=noreply@example.com + +#SMTP_EMAIL_RELAY=mail.example.com +#SMTP_EMAIL_USERNAME=noreply@example.com +#SMTP_EMAIL_PASSWORD=password +#SMTP_EMAIL_PORT=587 + +#SCALEWAY_EMAIL_PROJECT_ID=uuid +#SCALEWAY_EMAIL_SECRET_KEY=secret \ No newline at end of file diff --git a/config/runtime.exs b/config/runtime.exs index 5826496..321c46b 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -1,5 +1,6 @@ import Config +require Logger # config/runtime.exs is executed for all environments, including # during releases. It is executed after compilation and before the # system starts, so it is typically used to load production configuration @@ -119,4 +120,33 @@ if config_env() == :prod do # config :swoosh, :api_client, Swoosh.ApiClient.Hackney # # See https://hexdocs.pm/swoosh/Swoosh.html#module-installation for details. + + if System.get_env("SMTP_EMAIL_RELAY") do + Logger.info("Using SMTP for email delivery") + + config :decentralised_book_index, DecentralisedBookIndex.Mailer, + adapter: Swoosh.Adapters.SMTP, + relay: System.get_env("SMTP_EMAIL_RELAY"), + username: System.get_env("SMTP_EMAIL_USERNAME"), + password: System.get_env("SMTP_EMAIL_PASSWORD"), + ssl: true, + tls: :always, + auth: :always, + port: System.get_env("SMTP_EMAIL_PORT"), + retries: 2, + no_mx_lookups: false + + config :swoosh, :api_client, Swoosh.ApiClient.Finch + end + + if System.get_env("SCALEWAY_EMAIL_PROJECT_ID") do + Logger.info("Using Scaleway for email delivery") + + config :decentralised_book_index, DecentralisedBookIndex.Mailer, + adapter: Swoosh.Adapters.Scaleway, + project_id: System.get_env("SCALEWAY_EMAIL_PROJECT_ID"), + secret_key: System.get_env("SCALEWAY_EMAIL_SECRET_KEY") + + config :swoosh, :api_client, Swoosh.ApiClient.Finch + end end diff --git a/lib/decentralised_book_index/accounts/user/senders/send_new_user_confirmation_email.ex b/lib/decentralised_book_index/accounts/user/senders/send_new_user_confirmation_email.ex index 81fc638..81db75f 100644 --- a/lib/decentralised_book_index/accounts/user/senders/send_new_user_confirmation_email.ex +++ b/lib/decentralised_book_index/accounts/user/senders/send_new_user_confirmation_email.ex @@ -13,8 +13,7 @@ defmodule DecentralisedBookIndex.Accounts.User.Senders.SendNewUserConfirmationEm @impl true def send(user, token, _) do new() - # TODO: Replace with your email - |> from({"noreply", "noreply@example.com"}) + |> from({"noreply", System.get_env("SYSTEM_EMAIL", "noreply@example.com")}) |> to(to_string(user.email)) |> subject("Confirm your email address") |> html_body(body(token: token)) diff --git a/lib/decentralised_book_index/accounts/user/senders/send_password_reset_email.ex b/lib/decentralised_book_index/accounts/user/senders/send_password_reset_email.ex index 1c2bc32..176cbae 100644 --- a/lib/decentralised_book_index/accounts/user/senders/send_password_reset_email.ex +++ b/lib/decentralised_book_index/accounts/user/senders/send_password_reset_email.ex @@ -13,8 +13,7 @@ defmodule DecentralisedBookIndex.Accounts.User.Senders.SendPasswordResetEmail do @impl true def send(user, token, _) do new() - # TODO: Replace with your email - |> from({"noreply", "noreply@example.com"}) + |> from({"noreply", System.get_env("SYSTEM_EMAIL", "noreply@example.com")}) |> to(to_string(user.email)) |> subject("Reset your password") |> html_body(body(token: token)) diff --git a/mix.exs b/mix.exs index efa6ee6..6e96230 100644 --- a/mix.exs +++ b/mix.exs @@ -66,6 +66,7 @@ defmodule DecentralisedBookIndex.MixProject do compile: false, depth: 1}, {:swoosh, "~> 1.5"}, + {:gen_smtp, "~> 1.1"}, {:finch, "~> 0.13"}, {:telemetry_metrics, "~> 1.0"}, {:telemetry_poller, "~> 1.0"}, diff --git a/mix.lock b/mix.lock index d4907d2..798998d 100644 --- a/mix.lock +++ b/mix.lock @@ -25,6 +25,7 @@ "file_system": {:hex, :file_system, "1.1.0", "08d232062284546c6c34426997dd7ef6ec9f8bbd090eb91780283c9016840e8f", [:mix], [], "hexpm", "bfcf81244f416871f2a2e15c1b515287faa5db9c6bcf290222206d120b3d43f6"}, "finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"}, "floki": {:hex, :floki, "0.37.0", "b83e0280bbc6372f2a403b2848013650b16640cd2470aea6701f0632223d719e", [:mix], [], "hexpm", "516a0c15a69f78c47dc8e0b9b3724b29608aa6619379f91b1ffa47109b5d0dd3"}, + "gen_smtp": {:hex, :gen_smtp, "1.2.0", "9cfc75c72a8821588b9b9fe947ae5ab2aed95a052b81237e0928633a13276fd3", [:rebar3], [{:ranch, ">= 1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "5ee0375680bca8f20c4d85f58c2894441443a743355430ff33a783fe03296779"}, "gettext": {:hex, :gettext, "0.26.2", "5978aa7b21fada6deabf1f6341ddba50bc69c999e812211903b169799208f2a8", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "aa978504bcf76511efdc22d580ba08e2279caab1066b76bb9aa81c4a1e0a32a5"}, "glob_ex": {:hex, :glob_ex, "0.1.11", "cb50d3f1ef53f6ca04d6252c7fde09fd7a1cf63387714fe96f340a1349e62c93", [:mix], [], "hexpm", "342729363056e3145e61766b416769984c329e4378f1d558b63e341020525de4"}, "heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized", depth: 1]}, @@ -61,6 +62,7 @@ "plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"}, "plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"}, "postgrex": {:hex, :postgrex, "0.20.0", "363ed03ab4757f6bc47942eff7720640795eb557e1935951c1626f0d303a3aed", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "d36ef8b36f323d29505314f704e21a1a038e2dc387c6409ee0cd24144e187c0f"}, + "ranch": {:hex, :ranch, "2.2.0", "25528f82bc8d7c6152c57666ca99ec716510fe0925cb188172f41ce93117b1b0", [:make, :rebar3], [], "hexpm", "fa0b99a1780c80218a4197a59ea8d3bdae32fbff7e88527d7d8a4787eff4f8e7"}, "reactor": {:hex, :reactor, "0.14.0", "8dc5d4946391010bf9fa7b58dd1e75d3c1cf97315e5489b7797cf64b82ae27a4", [:mix], [{:igniter, "~> 0.4", [hex: :igniter, repo: "hexpm", optional: true]}, {:iterex, "~> 0.1", [hex: :iterex, repo: "hexpm", optional: false]}, {:libgraph, "~> 0.16", [hex: :libgraph, repo: "hexpm", optional: false]}, {:spark, "~> 2.0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.2", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9cf5068e4042791c150f0dfbc00f4f435433eb948036b44b95b940e457b35a6a"}, "req": {:hex, :req, "0.5.8", "50d8d65279d6e343a5e46980ac2a70e97136182950833a1968b371e753f6a662", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "d7fc5898a566477e174f26887821a3c5082b243885520ee4b45555f5d53f40ef"}, "rewrite": {:hex, :rewrite, "1.1.2", "f5a5d10f5fed1491a6ff48e078d4585882695962ccc9e6c779bae025d1f92eda", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}, {:text_diff, "~> 0.1", [hex: :text_diff, repo: "hexpm", optional: false]}], "hexpm", "7f8b94b1e3528d0a47b3e8b7bfeca559d2948a65fa7418a9ad7d7712703d39d4"},