diff --git a/lib/bamboo/adapters/message/content.ex b/lib/bamboo/adapters/message/content.ex index d76f142..f8dce24 100644 --- a/lib/bamboo/adapters/message/content.ex +++ b/lib/bamboo/adapters/message/content.ex @@ -4,6 +4,8 @@ defmodule BambooSes.Message.Content do Depending on email it can generate simple, raw or template content. """ + alias BambooSes.Encoding + @type t :: %__MODULE__{ Template: %{ @@ -78,8 +80,8 @@ defmodule BambooSes.Message.Content do when is_map(template_params), do: %__MODULE__{Template: template_params} - defp build_content(_email, _template_params, subject, text, html, [], []), - do: build_simple_content(subject, text, html) + defp build_content(_email, _template_params, subject, text, html, headers, []), + do: build_simple_content(subject, text, html, headers) defp build_content(email, _template_params, _subject, _text, _html, _headers, _attachments) do raw_data = @@ -94,18 +96,26 @@ defmodule BambooSes.Message.Content do } end - defp build_simple_content(subject, text, html) do + defp build_simple_content(subject, text, html, headers) do %__MODULE__{ Simple: %{ Subject: %{ Charset: "UTF-8", Data: subject }, - Body: build_simple_body(text, html) + Body: build_simple_body(text, html), + Headers: build_headers(headers) } } end + defp build_headers(headers) do + Enum.map( + headers, + fn {name, value} -> %{"Name" => name, "Value" => Encoding.maybe_rfc1342_encode(value)} end + ) + end + defp build_simple_body(text, html) do %{} |> put_text(text) diff --git a/test/lib/bamboo/adapters/content_raw_test.exs b/test/lib/bamboo/adapters/content_raw_test.exs index ed2cae9..bc1aaae 100644 --- a/test/lib/bamboo/adapters/content_raw_test.exs +++ b/test/lib/bamboo/adapters/content_raw_test.exs @@ -7,6 +7,7 @@ defmodule BambooSes.ContentRawTest do test "generates raw content when there is a header" do content = TestHelpers.new_email() + |> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf")) |> Email.put_header("X-Custom-Header", "custom-header-value") |> Content.build_from_bamboo_email() @@ -18,12 +19,7 @@ defmodule BambooSes.ContentRawTest do parsed_content = EmailParser.parse(raw_data) - raw_data - |> EmailParser.parse() - assert EmailParser.subject(parsed_content) == "Welcome to the app." - assert EmailParser.text(parsed_content) == "Thanks for joining!" - assert EmailParser.html(parsed_content) == "Thanks for joining!" assert header = EmailParser.header(parsed_content, "X-Custom-Header") assert header == "custom-header-value" end @@ -100,6 +96,7 @@ defmodule BambooSes.ContentRawTest do content = TestHelpers.new_email() + |> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf")) |> Email.put_header("X-Custom-Header", custom_header) |> Content.build_from_bamboo_email() diff --git a/test/lib/bamboo/adapters/content_simple_test.exs b/test/lib/bamboo/adapters/content_simple_test.exs index f7abe85..61810ff 100644 --- a/test/lib/bamboo/adapters/content_simple_test.exs +++ b/test/lib/bamboo/adapters/content_simple_test.exs @@ -15,7 +15,36 @@ defmodule BambooSes.ContentSimpleTest do Html: %{Charset: "UTF-8", Data: "Thanks for joining!"}, Text: %{Charset: "UTF-8", Data: "Thanks for joining!"} }, - Subject: %{Charset: "UTF-8", Data: "Welcome to the app."} + Subject: %{Charset: "UTF-8", Data: "Welcome to the app."}, + Headers: [] + } + } + end + + test "generates simple content with headers" do + content = + new_email() + |> Email.put_header("X-Custom-Header", "custom-value") + |> Email.put_header("X-Custom-Non-Ascii-Header", "𐰴𐰀𐰽𐱄𐰆𐰢") + |> Content.build_from_bamboo_email() + + assert content == %Content{ + Simple: %{ + Body: %{ + Html: %{Charset: "UTF-8", Data: "Thanks for joining!"}, + Text: %{Charset: "UTF-8", Data: "Thanks for joining!"} + }, + Subject: %{Charset: "UTF-8", Data: "Welcome to the app."}, + Headers: [ + %{ + "Name" => "X-Custom-Header", + "Value" => "custom-value" + }, + %{ + "Name" => "X-Custom-Non-Ascii-Header", + "Value" => "=?utf-8?B?8JCwtPCQsIDwkLC98JCxhPCQsIbwkLCi?=" + } + ] } } end @@ -32,7 +61,8 @@ defmodule BambooSes.ContentSimpleTest do Html: %{Charset: "UTF-8", Data: "Thanks for joining!"}, Text: %{Charset: "UTF-8", Data: "Thanks for joining!"} }, - Subject: %{Charset: "UTF-8", Data: "Welcome to the app."} + Subject: %{Charset: "UTF-8", Data: "Welcome to the app."}, + Headers: [] } } end diff --git a/test/lib/bamboo/adapters/ses_adapter_test.exs b/test/lib/bamboo/adapters/ses_adapter_test.exs index 1d84719..ffe8962 100644 --- a/test/lib/bamboo/adapters/ses_adapter_test.exs +++ b/test/lib/bamboo/adapters/ses_adapter_test.exs @@ -323,6 +323,7 @@ defmodule Bamboo.SesAdapterTest do TestHelpers.new_email() |> Email.put_header("X-Custom-Header", "header-value; another-value") + |> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf")) |> SesAdapter.deliver(%{}) end @@ -351,6 +352,7 @@ defmodule Bamboo.SesAdapterTest do TestHelpers.new_email() |> Email.put_header("X-Custom-Header", "header-value") |> Email.from({"John [Schmidt]", "from@example.com"}) + |> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf")) |> SesAdapter.deliver(%{}) end @@ -377,7 +379,7 @@ defmodule Bamboo.SesAdapterTest do expect(HttpMock, :request, expected_request_fn) TestHelpers.new_email() - |> Email.put_header("X-Custom-Header", "header-value") + |> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf")) |> Email.put_header("Reply-To", {"John Schmidt", "reply-to@example.com"}) |> SesAdapter.deliver(%{}) end