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

Integrate PostMark for email sending #19

Merged
merged 1 commit into from
Nov 21, 2023
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
8 changes: 4 additions & 4 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ if config_env() == :prod do
# Also, you may need to configure the Swoosh API client of your choice if you
# are not using SMTP. Here is an example of the configuration:
#
# config :dashfloat, DashFloat.Mailer,
# adapter: Swoosh.Adapters.Mailgun,
# api_key: System.get_env("MAILGUN_API_KEY"),
# domain: System.get_env("MAILGUN_DOMAIN")
config :dashfloat, DashFloat.Mailer,
adapter: Swoosh.Adapters.Postmark,
api_key: System.get_env("POSTMARK_API_KEY")

#
# For this example you need include a HTTP client required by Swoosh API client.
# Swoosh supports Hackney and Finch out of the box:
Expand Down
1 change: 1 addition & 0 deletions fly.production.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ kill_signal = "SIGTERM"
[env]
PHX_HOST = "www.dashfloat.com"
PORT = "8080"
MAIL_HOST = "dashfloat.com"

[http_service]
internal_port = 8080
Expand Down
1 change: 1 addition & 0 deletions fly.staging.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ kill_signal = "SIGTERM"
[env]
PHX_HOST = "staging.dashfloat.com"
PORT = "8080"
MAIL_HOST = "dashfloat.com"

[http_service]
internal_port = 8080
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule DashFloat.Identity.Emails.ConfirmationInstructionsEmail do

import Swoosh.Email

alias DashFloat.Identity.Helpers.EmailHelper
alias DashFloat.Identity.Schemas.User

@spec call(
Expand All @@ -16,8 +17,8 @@ defmodule DashFloat.Identity.Emails.ConfirmationInstructionsEmail do

new()
|> to(user.email)
|> from({"DashFloat", "[email protected]"})
|> subject("Confirmation instructions")
|> from(EmailHelper.no_reply())
|> subject("Confirmation Instructions")
|> text_body(body)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule DashFloat.Identity.Emails.ResetPasswordInstructionsEmail do

import Swoosh.Email

alias DashFloat.Identity.Helpers.EmailHelper
alias DashFloat.Identity.Schemas.User

@spec call(
Expand All @@ -16,8 +17,8 @@ defmodule DashFloat.Identity.Emails.ResetPasswordInstructionsEmail do

new()
|> to(user.email)
|> from({"DashFloat", "[email protected]"})
|> subject("Reset password instructions")
|> from(EmailHelper.no_reply())
|> subject("Reset Password Instructions")
|> text_body(body)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule DashFloat.Identity.Emails.UpdateEmailInstructionsEmail do

import Swoosh.Email

alias DashFloat.Identity.Helpers.EmailHelper
alias DashFloat.Identity.Schemas.User

@spec call(
Expand All @@ -16,8 +17,8 @@ defmodule DashFloat.Identity.Emails.UpdateEmailInstructionsEmail do

new()
|> to(user.email)
|> from({"DashFloat", "[email protected]"})
|> subject("Update email instructions")
|> from(EmailHelper.no_reply())
|> subject("Update Email Instructions")
|> text_body(body)
end

Expand Down
33 changes: 33 additions & 0 deletions lib/dashfloat/contexts/identity/helpers/email_helper.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule DashFloat.Identity.Helpers.EmailHelper do
@moduledoc """
Helper functions for building emails.
"""

@app_name "DashFloat"
@default_domain "example.com"

@doc """
Returns a tuple of the no-reply email address.

## Examples

iex> EmailHelper.no_reply()
{"DashFloat", "[email protected]"}
"""
@spec no_reply() :: {String.t(), String.t()}
def no_reply do
{sender_name(), "no-reply@#{mail_host()}"}
end

defp sender_name do
if String.contains?(mail_host(), "staging") do
@app_name <> " Staging"
else
@app_name
end
end

defp mail_host do
System.get_env("MAIL_HOST") || @default_domain
end
end
25 changes: 25 additions & 0 deletions test/dashfloat/contexts/identity/helpers/email_helper_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Dashfloat.Identity.Helpers.EmailHelperTest do
use DashFloat.DataCase, async: true

alias DashFloat.Identity.Helpers.EmailHelper

describe "no_reply/0" do
test "with MAIL_HOST returns a no-reply email address in the correct domain" do
System.put_env("MAIL_HOST", "dashfloat.com")

assert EmailHelper.no_reply() == {"DashFloat", "[email protected]"}
end

test "with MAIL_HOST that has staging returns a no-reply email address in the correct domain" do
System.put_env("MAIL_HOST", "staging.dashfloat.com")

assert EmailHelper.no_reply() == {"DashFloat Staging", "[email protected]"}
end

test "with no MAIL_HOST returns a no-reply email address in the default domain" do
System.delete_env("MAIL_HOST")

assert EmailHelper.no_reply() == {"DashFloat", "[email protected]"}
end
end
end
Loading