Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install and configure Swoosh email library #54

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# App
APP_NAME=ex_commerce
export APP_VERSION=0.1.0
export MIX_ENV=dev
export PHX_HOST=0.0.0.0

# Email configuration
export [email protected]
export SENDGRID_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export EX_COMMERCE_FROM_NAME=ExCommerce
export [email protected]
export SMTP_HOST=smtp.host.email
export SMTP_PORT=1025
export SMTP_USERNAME=some-username
export SMTP_PASSWORD=some-password

# Database
export DB_USERNAME=postgres
Expand Down
8 changes: 6 additions & 2 deletions .env.prod.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ PORT=5555
INTERNAL_PORT=5555

# Email configuration
[email protected]
SENDGRID_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EX_COMMERCE_FROM_NAME=ExCommerce
[email protected]
SMTP_HOST=smtp.host.email
SMTP_PORT=1025
SMTP_USERNAME=some-username
SMTP_PASSWORD=some-password

# Database
DB_USERNAME=db_username
Expand Down
12 changes: 6 additions & 6 deletions .github/actions/prod/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ runs:
DB_HOSTNAME: ${{ inputs.POSTGRES_HOSTNAME }}
DB_PASSWORD: ${{ inputs.POSTGRES_PASSWORD }}
DB_USERNAME: ${{ inputs.POSTGRES_USER }}
# Sendgrid env
EX_COMMERCE_FROM_EMAIL: test@email.com
SENDGRID_API_KEY: some-sendgrid-api-key
# Email configuration env
EX_COMMERCE_FROM_EMAIL: [email protected].com.ar
EX_COMMERCE_FROM_NAME: Local Production Team

# Test release -------------------------------------------------------------
- name: Test release
Expand All @@ -162,9 +162,9 @@ runs:
DB_HOSTNAME: ${{ inputs.POSTGRES_HOSTNAME }}
DB_PASSWORD: ${{ inputs.POSTGRES_PASSWORD }}
DB_USERNAME: ${{ inputs.POSTGRES_USER }}
# Sendgrid env
EX_COMMERCE_FROM_EMAIL: test@email.com
SENDGRID_API_KEY: some-sendgrid-api-key
# Email configuration env
EX_COMMERCE_FROM_EMAIL: [email protected].com.ar
EX_COMMERCE_FROM_NAME: Local Production Team

# Assert application output ------------------------------------------------
- name: Assert output
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ cp .env.example .env

#### Mailing Service

+ `SENDGRID_API_KEY`: Sendgrid API key.
+ `EX_COMMERCE_FROM_NAME`: Sender name.
+ `EX_COMMERCE_FROM_EMAIL`: Sender email.
+ `SMTP_HOST`: The smtp hostname.
+ `SMTP_PORT`: The smtp port (1025 by default)
+ `SMTP_USERNAME`: The authentication username.
+ `SMTP_PASSWORD`: The authentication password.

#### Cloudex credentials

Expand Down
3 changes: 2 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ config :phoenix, :stacktrace_depth, 20
#

config :ex_commerce, from_email: "[email protected]"
config :ex_commerce, from_name: "ExCommerce Development Team"

config :ex_commerce, ExCommerce.Mailer, adapter: Bamboo.LocalAdapter
config :ex_commerce, ExCommerce.Mailer, adapter: Swoosh.Adapters.Local

# ------------------------------------------------------------------------------
# Cloudex configuration
Expand Down
11 changes: 7 additions & 4 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ config :ex_commerce, ex_commerce_assets_driver: :cloudex
# Email configuration
#
config :ex_commerce, ExCommerce.Mailer,
adapter: Bamboo.SendGridAdapter,
hackney_opts: [
recv_timeout: :timer.minutes(1)
]
adapter: Swoosh.Adapters.SMTP,
auth: :always,
retries: 2,
# can be `true`
ssl: true,
# can be `:always` or `:never`
tls: :if_available

# ------------------------------------------------------------------------------
# Cloudex configuration (runtime.exs)
Expand Down
27 changes: 22 additions & 5 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ if config_env() == :prod do
host = System.fetch_env!("PHX_HOST")
port = String.to_integer(System.get_env("PORT", "443"))

case System.get_env("STAGE") do
stage = System.get_env("STAGE")

case stage do
stage when stage in ["local", "dev", "staging", "prod"] ->
:ok =
Logger.warn(
Expand Down Expand Up @@ -75,10 +77,25 @@ if config_env() == :prod do
# ----------------------------------------------------------------------------
# Email configuration
#
config :ex_commerce, from_email: System.fetch_env!("EX_COMMERCE_FROM_EMAIL")

config :ex_commerce, ExCommerce.Mailer,
api_key: System.fetch_env!("SENDGRID_API_KEY")
case stage do
"local" ->
config :ex_commerce,
from_email: System.fetch_env!("EX_COMMERCE_FROM_EMAIL"),
from_name: System.fetch_env!("EX_COMMERCE_FROM_NAME")

config :ex_commerce, ExCommerce.Mailer, adapter: Swoosh.Adapters.Local

stage when stage in ["dev", "staging", "prod"] ->
config :ex_commerce,
from_email: System.fetch_env!("EX_COMMERCE_FROM_EMAIL"),
from_name: System.fetch_env!("EX_COMMERCE_FROM_NAME")

config :ex_commerce, ExCommerce.Mailer,
relay: System.fetch_env!("SMTP_HOST"),
port: System.fetch_env!("SMTP_PORT"),
username: System.fetch_env!("SMTP_USERNAME"),
password: System.fetch_env!("SMTP_PASSWORD")
end

# ----------------------------------------------------------------------------
# Cloudex configuration
Expand Down
3 changes: 2 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ config :ex_commerce, ex_commerce_assets_driver: :test
#

config :ex_commerce, from_email: "[email protected]"
config :ex_commerce, from_name: "ExCommerce Test Team"

config :ex_commerce, ExCommerce.Mailer, adapter: Bamboo.LocalAdapter
config :ex_commerce, ExCommerce.Mailer, adapter: Swoosh.Adapters.Local

# ------------------------------------------------------------------------------
# Cloudex configuration
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_commerce/accounts/user_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule ExCommerce.Accounts.UserNotifier do
Logger.debug(body)

Email.new(to, body, subject)
|> Mailer.deliver_now!()
|> Mailer.deliver()

{:ok, %{to: to, body: body}}
end
Expand Down
21 changes: 13 additions & 8 deletions lib/ex_commerce/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ defmodule ExCommerce.Mailer do
@moduledoc """
Responsible for configuring the Mailer module
"""
use Bamboo.Mailer, otp_app: :ex_commerce
use Swoosh.Mailer, otp_app: :ex_commerce
end

defmodule ExCommerce.Email do
@moduledoc """
Responsible for sending emails
"""
import Bamboo.Email
import Swoosh.Email

def new(to, body, subject) do
new_email(
to: to,
from: Application.fetch_env!(:ex_commerce, :from_email),
subject: subject,
text_body: body
)
new()
|> to(to)
|> from({from_name(), from_email()})
|> subject(subject)
|> text_body(body)
end

defp from_email, do: fetch_env!(:from_email)

defp from_name, do: fetch_env!(:from_name)

defp fetch_env!(key), do: Application.fetch_env!(:ex_commerce, key)
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ defmodule ExCommerce.MixProject do
{:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"},
# Email apps
{:bamboo, "~> 2.2.0"},
{:swoosh, "~> 1.17.2"},
{:gen_smtp, "~> 1.1"},
# i18n and l10n
{:ex_cldr, "~> 2.34"},
{:ex_cldr_plugs, "~> 1.2"},
Expand Down
4 changes: 4 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
"bamboo": {:hex, :bamboo, "2.2.0", "f10a406d2b7f5123eb1f02edfa043c259db04b47ab956041f279eaac776ef5ce", [:mix], [{:hackney, ">= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.4", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "8c3b14ba7d2f40cb4be04128ed1e2aff06d91d9413d38bafb4afccffa3ade4fc"},
"bamboo_smtp": {:hex, :bamboo_smtp, "4.1.0", "ba547be4146ae592f63af05c6c7b7b5195b2b6ca57eeea9d80070b38eacd528b", [:mix], [{:bamboo, "~> 2.2.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:gen_smtp, "~> 1.1.1", [hex: :gen_smtp, repo: "hexpm", optional: false]}], "hexpm", "cb1a2856ab0507d10df609428314aa5e18231e8b1801a5bc6e42f319eeb50ad9"},
"bcrypt_elixir": {:hex, :bcrypt_elixir, "2.3.1", "5114d780459a04f2b4aeef52307de23de961b69e13a5cd98a911e39fda13f420", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "42182d5f46764def15bf9af83739e3bf4ad22661b1c34fc3e88558efced07279"},
"blankable": {:hex, :blankable, "1.0.0", "89ab564a63c55af117e115144e3b3b57eb53ad43ba0f15553357eb283e0ed425", [:mix], [], "hexpm", "7cf11aac0e44f4eedbee0c15c1d37d94c090cb72a8d9fddf9f7aec30f9278899"},
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
Expand Down Expand Up @@ -28,11 +29,13 @@
"expo": {:hex, :expo, "0.4.1", "1c61d18a5df197dfda38861673d392e642649a9cef7694d2f97a587b2cfb319b", [:mix], [], "hexpm", "2ff7ba7a798c8c543c12550fa0e2cbc81b95d4974c65855d8d15ba7b37a1ce47"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"floki": {:hex, :floki, "0.34.3", "5e2dcaec5d7c228ce5b1d3501502e308b2d79eb655e4191751a1fe491c37feac", [:mix], [], "hexpm", "9577440eea5b97924b4bf3c7ea55f7b8b6dce589f9b28b096cc294a8dc342341"},
"gen_smtp": {:hex, :gen_smtp, "1.1.1", "bf9303c31735100631b1d708d629e4c65944319d1143b5c9952054f4a1311d85", [:rebar3], [{:hut, "1.3.0", [hex: :hut, repo: "hexpm", optional: false]}, {:ranch, ">= 1.7.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "51bc50cc017efd4a4248cbc39ea30fb60efa7d4a49688986fafad84434ff9ab7"},
"gettext": {:hex, :gettext, "0.23.1", "821e619a240e6000db2fc16a574ef68b3bd7fe0167ccc264a81563cc93e67a31", [:mix], [{:expo, "~> 0.4.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "19d744a36b809d810d610b57c27b934425859d158ebd56561bc41f7eeb8795db"},
"git_hooks": {:hex, :git_hooks, "0.7.3", "09489e94d88dfc767662e22aff2b6208bd7cf555a19dd0e1477cca4683ce0701", [:mix], [{:blankable, "~> 1.0.0", [hex: :blankable, repo: "hexpm", optional: false]}, {:recase, "~> 0.7.0", [hex: :recase, repo: "hexpm", optional: false]}], "hexpm", "d6ddedeb4d3a8602bc3f84e087a38f6150a86d9e790628ed8bc70e6d90681659"},
"hackney": {:hex, :hackney, "1.18.2", "d7ff544ddae5e1cb49e9cf7fa4e356d7f41b283989a1c304bfc47a8cc1cf966f", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "af94d5c9f97857db257090a4a10e5426ecb6f4918aa5cc666798566ae14b65fd"},
"heroicons": {:hex, :heroicons, "0.5.3", "ee8ae8335303df3b18f2cc07f46e1cb6e761ba4cf2c901623fbe9a28c0bc51dd", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:phoenix_live_view, ">= 0.18.2", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}], "hexpm", "a210037e8a09ac17e2a0a0779d729e89c821c944434c3baa7edfc1f5b32f3502"},
"httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"},
"hut": {:hex, :hut, "1.3.0", "71f2f054e657c03f959cf1acc43f436ea87580696528ca2a55c8afb1b06c85e7", [:"erlang.mk", :rebar, :rebar3], [], "hexpm", "7e15d28555d8a1f2b5a3a931ec120af0753e4853a4c66053db354f35bf9ab563"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"live_motion": {:hex, :live_motion, "0.3.0", "8444f15b1589073c0f0c67147d30a723de0240f656250c2605f18d72857483dd", [:mix], [{:jason, "~> 1.3.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.18", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "72953e2b84a5e15da94a812e1c33dfa4814eec4cae8379683f47f10f108cf17c"},
Expand Down Expand Up @@ -64,6 +67,7 @@
"recaptcha": {:hex, :recaptcha, "3.0.0", "9c4d3ee688d69605d6de9629f0ae51670748d023f7b5700d0fe788877932c95c", [:mix], [{:httpoison, ">= 0.12.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:poison, "~> 1.5.2 or ~> 2.2.0 or ~> 3.1.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "894b2a8d7ce2cf22145fa8b900379f78478f9a652960f56aec280352d8467d1c"},
"recase": {:hex, :recase, "0.7.0", "3f2f719f0886c7a3b7fe469058ec539cb7bbe0023604ae3bce920e186305e5ae", [:mix], [], "hexpm", "36f5756a9f552f4a94b54a695870e32f4e72d5fad9c25e61bc4a3151c08a4e0c"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"swoosh": {:hex, :swoosh, "1.17.2", "73611f08fc7cb9fa15f4909db36eeb12b70727d5c8b6a7fa0d4a31c6575db29e", [:mix], [{:bandit, ">= 1.0.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mua, "~> 0.2.3", [hex: :mua, repo: "hexpm", optional: true]}, {:multipart, "~> 0.4", [hex: :multipart, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:req, "~> 0.5 or ~> 1.0", [hex: :req, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "de914359f0ddc134dc0d7735e28922d49d0503f31e4bd66b44e26039c2226d39"},
"tailwind": {:hex, :tailwind, "0.1.10", "21ed80ae1f411f747ee513470578acaaa1d0eb40170005350c5b0b6d07e2d624", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "e0fc474dfa8ed7a4573851ac69c5fd3ca70fbb0a5bada574d1d657ebc6f2f1f1"},
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"},
Expand Down
Loading