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

Conditionally include the adapter response in deliver_now #452

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 4 additions & 2 deletions lib/bamboo/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ defmodule Bamboo.Email do
text_body: nil | String.t(),
headers: %{String.t() => String.t()},
assigns: %{atom => any},
private: %{atom => any}
private: %{atom => any},
response: nil | %{atom => any}
}

defstruct from: nil,
Expand All @@ -88,7 +89,8 @@ defmodule Bamboo.Email do
headers: %{},
attachments: [],
assigns: %{},
private: %{}
private: %{},
response: nil

alias Bamboo.{Email, Attachment}

Expand Down
12 changes: 9 additions & 3 deletions lib/bamboo/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ defmodule Bamboo.Mailer do

if email.to == [] && email.cc == [] && email.bcc == [] do
debug_unsent(email)
email
else
debug_sent(email, adapter)
adapter.deliver(email, config)
response = adapter.deliver(email, config)
maybe_merge_response(email, response)
end

email
end

@doc false
Expand All @@ -140,6 +140,12 @@ defmodule Bamboo.Mailer do
email
end

defp maybe_merge_response(email, %{status_code: _, headers: _, body: _} = response) do
%{email | response: response}
end

defp maybe_merge_response(email, _), do: email

defp debug_sent(email, adapter) do
Logger.debug(fn ->
"""
Expand Down
24 changes: 24 additions & 0 deletions test/lib/bamboo/mailer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ defmodule Bamboo.MailerTest do
def supports_attachments?, do: true
end

defmodule ResponseAdapter do
def deliver(_email, _config) do
send(:mailer_test, %{status_code: 202, headers: [%{}], body: ""})
end

def handle_config(config), do: config
end

defmodule CustomConfigAdapter do
def deliver(email, config) do
send(:mailer_test, {:deliver, email, config})
Expand Down Expand Up @@ -54,6 +62,14 @@ defmodule Bamboo.MailerTest do
use Bamboo.Mailer, otp_app: :bamboo
end

@response_config adapter: ResponseAdapter, foo: :bar

Application.put_env(:bamboo, __MODULE__.ResponseAdapterMailer, @response_config)

defmodule ResponseAdapterMailer do
use Bamboo.Mailer, otp_app: :bamboo
end

setup do
Process.register(self(), :mailer_test)
:ok
Expand Down Expand Up @@ -125,6 +141,14 @@ defmodule Bamboo.MailerTest do
assert config == config_with_default_strategy
end

test "deliver_now/1 returns the email with the response if there is a conforming one" do
email = new_email(to: "foo@bar.com")

%Email{response: response} = ResponseAdapterMailer.deliver_now(email)

assert %{body: _, headers: _, status_code: _} = response
end

test "deliver_now/1 with no from address" do
assert_raise Bamboo.EmptyFromAddressError, fn ->
FooMailer.deliver_now(new_email(from: nil))
Expand Down