-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ability to define interceptors (#591)
This commit adds the ability to define custom email interceptors. Interceptors are modules that abide by an interceptor behaviour and are configured alongside Bamboo's config. Why interceptors? ------------------- Interceptors can be used to either modify emails or prevent them from going out. For example, we may want to: - rewrite subject for a given environment (e.g. prefix [environment] to subject) - rewrite email `to` address so that it only sends to allowed addresses in dev/staging - block outbound emails for a given environment (staging, dev,...) - block email in case the recipient is blocked (bounces, complaints, ...)
- Loading branch information
1 parent
7af7bf3
commit 733b9e5
Showing
8 changed files
with
189 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Bamboo.Interceptor do | ||
@moduledoc ~S""" | ||
Behaviour for creating an Interceptor. | ||
An interceptor allow to modify / block an email before it is sent. To block an email, it must be marked as intercepted with `Bamboo.Email.intercept/1`. | ||
## Example | ||
defmodule Bamboo.DenyListInterceptor do | ||
@behaviour Bamboo.Interceptor | ||
@deny_list ["bar@foo.com"] | ||
def call(email) do | ||
if email.to in @deny_list do | ||
Bamboo.Email.intercept(email) | ||
else | ||
end | ||
end | ||
end | ||
""" | ||
|
||
@callback call(email :: Bamboo.Email.t()) :: Bamboo.Email.t() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Bamboo.DenyListInterceptor do | ||
@behaviour Bamboo.Interceptor | ||
|
||
@deny_list ["blocked@blocked.com"] | ||
|
||
def call(email) do | ||
if Enum.any?(email.to, &(elem(&1, 1) in @deny_list)) do | ||
Bamboo.Email.block(email) | ||
else | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Bamboo.EnvInterceptor do | ||
@behaviour Bamboo.Interceptor | ||
|
||
@env Mix.env() | ||
|
||
def call(email) do | ||
%{email | subject: "#{@env} - #{email.subject}"} | ||
end | ||
end |