Skip to content

Commit 7a39c0d

Browse files
authored
Set correct body field depending on template format (#154)
* Use correct body fields in Email struct Use `:html_body` for `.htm`, `.html` and `.xml` template extensions, `:text_body` otherwise * Add line about formats to render_body docs * mix.format * Add tests for body formats
1 parent cdae652 commit 7a39c0d

8 files changed

Lines changed: 33 additions & 5 deletions

File tree

lib/phoenix_swoosh.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ defmodule Phoenix.Swoosh do
6565
6666
Once the template is rendered the resulting string is stored on the email fields `html_body` and `text_body` depending
6767
on the format of the template.
68+
`.html`, `.htm`, and `.xml` are stored in `html_body`; all other extensions, (e.g. `.txt` and `.text`), in `text_body`.
6869
6970
## Arguments
7071
@@ -157,9 +158,12 @@ defmodule Phoenix.Swoosh do
157158
raise "a view module was not specified, set one with put_view/2"
158159

159160
content = Phoenix.View.render_to_string(view, template, Map.put(email.assigns, :email, email))
160-
Map.put(email, :"#{format}_body", content)
161+
Map.put(email, body_key(format), content)
161162
end
162163

164+
defp body_key(format) when format in ["html", "htm", "xml"], do: :html_body
165+
defp body_key(_other), do: :text_body
166+
163167
@doc """
164168
Stores the layout for rendering.
165169
@@ -258,8 +262,6 @@ defmodule Phoenix.Swoosh do
258262
end
259263
end
260264

261-
defp template_name(name, format) when is_atom(name), do:
262-
Atom.to_string(name) <> "." <> format
263-
defp template_name(name, _format) when is_binary(name), do:
264-
name
265+
defp template_name(name, format) when is_atom(name), do: Atom.to_string(name) <> "." <> format
266+
defp template_name(name, _format) when is_binary(name), do: name
265267
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an HTML template with the .htm extension.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an HTML template with the .html extension.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an HTML template with the .xml extension.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a text template with the .text extension.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a text template with the .txt extension.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a text template with an unknown extension.

test/phoenix_swoosh_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,24 @@ defmodule Phoenix.SwooshTest do
290290
end
291291
end
292292
end
293+
294+
test "body formats are set according to template file extension", %{email: email} do
295+
assert email |> render_body("format_html.html", %{}) |> Map.fetch!(:html_body) =~
296+
"This is an HTML template"
297+
298+
assert email |> render_body("format_html.htm", %{}) |> Map.fetch!(:html_body) =~
299+
"This is an HTML template"
300+
301+
assert email |> render_body("format_html.xml", %{}) |> Map.fetch!(:html_body) =~
302+
"This is an HTML template"
303+
304+
assert email |> render_body("format_text.txt", %{}) |> Map.fetch!(:text_body) =~
305+
"This is a text template"
306+
307+
assert email |> render_body("format_text.text", %{}) |> Map.fetch!(:text_body) =~
308+
"This is a text template"
309+
310+
assert email |> render_body("format_text.unknown", %{}) |> Map.fetch!(:text_body) =~
311+
"This is a text template"
312+
end
293313
end

0 commit comments

Comments
 (0)