Skip to content

Commit

Permalink
Send as a simple content with headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kalys committed Oct 13, 2024
1 parent 7418893 commit 432de8f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
18 changes: 14 additions & 4 deletions lib/bamboo/adapters/message/content.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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:
%{
Expand Down Expand Up @@ -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 =
Expand All @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions test/lib/bamboo/adapters/content_raw_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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) == "<strong>Thanks for joining!</strong>"
assert header = EmailParser.header(parsed_content, "X-Custom-Header")
assert header == "custom-header-value"
end
Expand Down Expand Up @@ -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()

Expand Down
34 changes: 32 additions & 2 deletions test/lib/bamboo/adapters/content_simple_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,36 @@ defmodule BambooSes.ContentSimpleTest do
Html: %{Charset: "UTF-8", Data: "<strong>Thanks for joining!</strong>"},
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: "<strong>Thanks for joining!</strong>"},
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
Expand All @@ -32,7 +61,8 @@ defmodule BambooSes.ContentSimpleTest do
Html: %{Charset: "UTF-8", Data: "<strong>Thanks for joining!</strong>"},
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
Expand Down
4 changes: 3 additions & 1 deletion test/lib/bamboo/adapters/ses_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -351,6 +352,7 @@ defmodule Bamboo.SesAdapterTest do
TestHelpers.new_email()
|> Email.put_header("X-Custom-Header", "header-value")
|> Email.from({"John [Schmidt]", "[email protected]"})
|> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf"))
|> SesAdapter.deliver(%{})
end

Expand All @@ -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", "[email protected]"})
|> SesAdapter.deliver(%{})
end
Expand Down

0 comments on commit 432de8f

Please sign in to comment.