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

feat!: Use interceptor before stripping the recipients with the formatter #682

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 lib/bamboo/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ defmodule Bamboo.Mailer do

@doc false
def deliver_now(adapter, email, config, opts) do
with {:ok, email} <- validate_and_normalize(email, adapter),
%Bamboo.Email{blocked: false} = email <- apply_interceptors(email, config) do
with %Bamboo.Email{blocked: false} = email <- apply_interceptors(email, config),
{:ok, email} <- validate_and_normalize(email, adapter) do
if empty_recipients?(email) do
debug_unsent(email)

Expand Down Expand Up @@ -244,8 +244,8 @@ defmodule Bamboo.Mailer do

@doc false
def deliver_later(adapter, email, config) do
with {:ok, email} <- validate_and_normalize(email, adapter),
%Bamboo.Email{blocked: false} = email <- apply_interceptors(email, config) do
with %Bamboo.Email{blocked: false} = email <- apply_interceptors(email, config),
{:ok, email} <- validate_and_normalize(email, adapter) do
if empty_recipients?(email) do
debug_unsent(email)
else
Expand Down
12 changes: 10 additions & 2 deletions test/support/deny_list_interceptor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ defmodule Bamboo.DenyListInterceptor do

@deny_list ["[email protected]"]

def call(email) do
if Enum.any?(email.to, &(elem(&1, 1) in @deny_list)) do
def call(%{to: recipients} = email) when is_list(recipients) do
if Enum.any?(recipients, &(&1 in @deny_list)) do
Bamboo.Email.block(email)
else
email
end
end

def call(%{to: recipient} = email) when recipient in @deny_list do
Bamboo.Email.block(email)
end

def call(email) do
email
end
Comment on lines +6 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tarzan can you walk me through this part of the change? It's not immediately clear why we made these changes other than to use multi-function heads instead of the prior solution.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. Previously this interceptor (created for test scenario's) would expect the to: field to be populated by a list of tuples: {name, email}.
The actual test scenario's just put strings containing the email(s) on the to: field, so I had to check for a single binary that is an email or a list of binaries.

end