From 3e1ef7b1c8e2e683ce4abf9325f233ac13864811 Mon Sep 17 00:00:00 2001 From: Maarten Jacobs Date: Tue, 15 Oct 2024 18:09:11 +0200 Subject: [PATCH] interceptor before normalizing so we can use the rich user info before it gets stripped by the formatter --- lib/bamboo/mailer.ex | 8 ++++---- test/support/deny_list_interceptor.ex | 12 ++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/bamboo/mailer.ex b/lib/bamboo/mailer.ex index 7f44effa..2307dd1f 100644 --- a/lib/bamboo/mailer.ex +++ b/lib/bamboo/mailer.ex @@ -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) @@ -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 diff --git a/test/support/deny_list_interceptor.ex b/test/support/deny_list_interceptor.ex index 9750657e..bc2b001a 100644 --- a/test/support/deny_list_interceptor.ex +++ b/test/support/deny_list_interceptor.ex @@ -3,11 +3,19 @@ defmodule Bamboo.DenyListInterceptor do @deny_list ["blocked@blocked.com"] - 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 end