Skip to content

Commit

Permalink
Fix From header format and encoding in Raw content renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
michallepicki authored and kalys committed Sep 10, 2024
1 parent 9c4fa60 commit bdee330
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/bamboo/adapters/render/raw.ex
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,13 @@ defmodule BambooSes.Render.Raw do
defp headers_for(email) do
headers =
[
{"From", mailbox(email.from)},
{"From", BambooSes.Encoding.prepare_address(email.from)},
{"Subject", email.subject}
] ++ Map.to_list(email.headers)

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

defp mailbox({name, address}), do: "#{name} <#{address}>"

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 @@ -326,6 +326,34 @@ defmodule Bamboo.SesAdapterTest do
|> SesAdapter.deliver(%{})
end

test "does not crash with special characters in From 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, "From")
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.from({"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

0 comments on commit bdee330

Please sign in to comment.