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

Handle tuple based Reply-To headers in Raw content renderer #57

Merged
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
13 changes: 11 additions & 2 deletions lib/bamboo/adapters/render/raw.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ defmodule BambooSes.Render.Raw do
@doc """
Returns a tuple with all data needed for the underlying adapter to send.
"""

alias BambooSes.Encoding

def render(email, extra_headers \\ []) do
email
# Returns a list of tuples
Expand Down Expand Up @@ -163,13 +166,19 @@ defmodule BambooSes.Render.Raw do
defp headers_for(email) do
headers =
[
{"From", BambooSes.Encoding.prepare_address(email.from)},
{"From", Encoding.prepare_address(email.from)},
{"Subject", email.subject}
] ++ Map.to_list(email.headers)
] ++ Enum.map(email.headers, &preprocess_header/1)

Enum.filter(headers, fn i -> elem(i, 1) != "" end)
end

defp preprocess_header({"Reply-To" = key, {_name, _address} = value}) do
{key, Encoding.prepare_address(value)}
end

defp preprocess_header({key, value}), do: {key, value}

defp compile_parts(email) do
[
{:plain, email.text_body},
Expand Down
28 changes: 28 additions & 0 deletions test/lib/bamboo/adapters/ses_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,34 @@ defmodule Bamboo.SesAdapterTest do
|> SesAdapter.deliver(%{})
end

test "does not crash with Reply-To tuple and sending raw content" do
expected_request_fn = fn _, _, body, _, _ ->
{:ok, message} = Jason.decode(body)

%{
"Content" => %{
"Raw" => %{
"Data" => raw_content
}
}
} = message

email = EmailParser.parse(raw_content)

assert header_value = EmailParser.header(email, "Reply-To")
assert header_value == "John Schmidt <[email protected]>"

{:ok, %{status_code: 200, body: body}}
end

expect(HttpMock, :request, expected_request_fn)

TestHelpers.new_email()
|> Email.put_header("X-Custom-Header", "header-value")
|> Email.put_header("Reply-To", {"John Schmidt", "[email protected]"})
|> SesAdapter.deliver(%{})
end

test "uses default aws region" do
expected_request_fn = fn _,
"https://email.us-east-1.amazonaws.com/v2/email/outbound-emails",
Expand Down
Loading