Skip to content

Commit c1c21f2

Browse files
committed
Send as a simple content with headers
1 parent 7418893 commit c1c21f2

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

lib/bamboo/adapters/message/content.ex

+14-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule BambooSes.Message.Content do
44
Depending on email it can generate simple, raw or template content.
55
"""
66

7+
alias BambooSes.Encoding
8+
79
@type t :: %__MODULE__{
810
Template:
911
%{
@@ -78,8 +80,8 @@ defmodule BambooSes.Message.Content do
7880
when is_map(template_params),
7981
do: %__MODULE__{Template: template_params}
8082

81-
defp build_content(_email, _template_params, subject, text, html, [], []),
82-
do: build_simple_content(subject, text, html)
83+
defp build_content(_email, _template_params, subject, text, html, headers, []),
84+
do: build_simple_content(subject, text, html, headers)
8385

8486
defp build_content(email, _template_params, _subject, _text, _html, _headers, _attachments) do
8587
raw_data =
@@ -94,18 +96,26 @@ defmodule BambooSes.Message.Content do
9496
}
9597
end
9698

97-
defp build_simple_content(subject, text, html) do
99+
defp build_simple_content(subject, text, html, headers) do
98100
%__MODULE__{
99101
Simple: %{
100102
Subject: %{
101103
Charset: "UTF-8",
102104
Data: subject
103105
},
104-
Body: build_simple_body(text, html)
106+
Body: build_simple_body(text, html),
107+
Headers: build_headers(headers)
105108
}
106109
}
107110
end
108111

112+
defp build_headers(headers) do
113+
Enum.map(
114+
headers,
115+
fn {name, value} -> %{"Name" => name, "Value" => Encoding.maybe_rfc1342_encode(value) } end
116+
)
117+
end
118+
109119
defp build_simple_body(text, html) do
110120
%{}
111121
|> put_text(text)
@@ -121,7 +131,6 @@ defmodule BambooSes.Message.Content do
121131
defp put_html(content, value) when is_binary(value) do
122132
Map.put(content, :Html, %{Data: value, Charset: "UTF-8"})
123133
end
124-
125134
defp put_html(content, _value), do: content
126135

127136
defp fetch_template_params(name, nil, nil) when is_binary(name),

test/lib/bamboo/adapters/content_raw_test.exs

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule BambooSes.ContentRawTest do
77
test "generates raw content when there is a header" do
88
content =
99
TestHelpers.new_email()
10+
|> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf"))
1011
|> Email.put_header("X-Custom-Header", "custom-header-value")
1112
|> Content.build_from_bamboo_email()
1213

@@ -18,12 +19,7 @@ defmodule BambooSes.ContentRawTest do
1819

1920
parsed_content = EmailParser.parse(raw_data)
2021

21-
raw_data
22-
|> EmailParser.parse()
23-
2422
assert EmailParser.subject(parsed_content) == "Welcome to the app."
25-
assert EmailParser.text(parsed_content) == "Thanks for joining!"
26-
assert EmailParser.html(parsed_content) == "<strong>Thanks for joining!</strong>"
2723
assert header = EmailParser.header(parsed_content, "X-Custom-Header")
2824
assert header == "custom-header-value"
2925
end
@@ -100,6 +96,7 @@ defmodule BambooSes.ContentRawTest do
10096

10197
content =
10298
TestHelpers.new_email()
99+
|> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf"))
103100
|> Email.put_header("X-Custom-Header", custom_header)
104101
|> Content.build_from_bamboo_email()
105102

test/lib/bamboo/adapters/content_simple_test.exs

+32-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,36 @@ defmodule BambooSes.ContentSimpleTest do
1515
Html: %{Charset: "UTF-8", Data: "<strong>Thanks for joining!</strong>"},
1616
Text: %{Charset: "UTF-8", Data: "Thanks for joining!"}
1717
},
18-
Subject: %{Charset: "UTF-8", Data: "Welcome to the app."}
18+
Subject: %{Charset: "UTF-8", Data: "Welcome to the app."},
19+
Headers: []
20+
}
21+
}
22+
end
23+
24+
test "generates simple content with headers" do
25+
content =
26+
new_email()
27+
|> Email.put_header("X-Custom-Header", "custom-value")
28+
|> Email.put_header("X-Custom-Non-Ascii-Header", "𐰴𐰀𐰽𐱄𐰆𐰢")
29+
|> Content.build_from_bamboo_email()
30+
31+
assert content == %Content{
32+
Simple: %{
33+
Body: %{
34+
Html: %{Charset: "UTF-8", Data: "<strong>Thanks for joining!</strong>"},
35+
Text: %{Charset: "UTF-8", Data: "Thanks for joining!"}
36+
},
37+
Subject: %{Charset: "UTF-8", Data: "Welcome to the app."},
38+
Headers: [
39+
%{
40+
"Name" => "X-Custom-Header",
41+
"Value" => "custom-value"
42+
},
43+
%{
44+
"Name" => "X-Custom-Non-Ascii-Header",
45+
"Value" => "=?utf-8?B?8JCwtPCQsIDwkLC98JCxhPCQsIbwkLCi?="
46+
}
47+
]
1948
}
2049
}
2150
end
@@ -32,7 +61,8 @@ defmodule BambooSes.ContentSimpleTest do
3261
Html: %{Charset: "UTF-8", Data: "<strong>Thanks for joining!</strong>"},
3362
Text: %{Charset: "UTF-8", Data: "Thanks for joining!"}
3463
},
35-
Subject: %{Charset: "UTF-8", Data: "Welcome to the app."}
64+
Subject: %{Charset: "UTF-8", Data: "Welcome to the app."},
65+
Headers: []
3666
}
3767
}
3868
end

test/lib/bamboo/adapters/ses_adapter_test.exs

+3-1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ defmodule Bamboo.SesAdapterTest do
323323

324324
TestHelpers.new_email()
325325
|> Email.put_header("X-Custom-Header", "header-value; another-value")
326+
|> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf"))
326327
|> SesAdapter.deliver(%{})
327328
end
328329

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

@@ -377,7 +379,7 @@ defmodule Bamboo.SesAdapterTest do
377379
expect(HttpMock, :request, expected_request_fn)
378380

379381
TestHelpers.new_email()
380-
|> Email.put_header("X-Custom-Header", "header-value")
382+
|> Email.put_attachment(Path.join(__DIR__, "../../../support/invoice.pdf"))
381383
|> Email.put_header("Reply-To", {"John Schmidt", "[email protected]"})
382384
|> SesAdapter.deliver(%{})
383385
end

0 commit comments

Comments
 (0)