Skip to content

Commit

Permalink
Default to UTF-8 (DockYard#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska authored Feb 4, 2024
1 parent c728ffc commit d83c47f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 24 deletions.
8 changes: 6 additions & 2 deletions lib/mail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ defmodule Mail do
["text/plain", {"charset", charset}]

_else ->
"text/plain"
["text/plain", {"charset", default_charset()}]
end

Mail.Message.put_body(message, body)
Expand Down Expand Up @@ -132,7 +132,7 @@ defmodule Mail do
["text/html", {"charset", charset}]

_else ->
"text/html"
["text/html", {"charset", default_charset()}]
end

Mail.Message.put_body(message, body)
Expand Down Expand Up @@ -162,6 +162,10 @@ defmodule Mail do

def get_html(%Mail.Message{}), do: nil

defp default_charset do
"UTF-8"
end

@doc """
Add an attachment part to the message
Expand Down
8 changes: 6 additions & 2 deletions lib/mail/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ defmodule Mail.Message do
["text/plain", {"charset", charset}]

_else ->
"text/plain"
["text/plain", {"charset", default_charset()}]
end

put_content_type(%Mail.Message{}, content_type)
Expand Down Expand Up @@ -231,14 +231,18 @@ defmodule Mail.Message do
["text/html", {"charset", charset}]

_else ->
"text/html"
["text/html", {"charset", default_charset()}]
end

put_content_type(%Mail.Message{}, content_type)
|> put_header(:content_transfer_encoding, :quoted_printable)
|> put_body(body)
end

defp default_charset do
"UTF-8"
end

@doc """
Add attachment meta data to a `Mail.Message`
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/multipart-jpeg-attachment-rendering.eml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Content-Type: multipart/mixed; boundary="2753118483D4F18DC5FD1F1A"
Content-Type: multipart/alternative; boundary="DB28EC13A6576C8F20B92FD0"
--DB28EC13A6576C8F20B92FD0
Content-Type: text/plain
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Some text
--DB28EC13A6576C8F20B92FD0
Content-Type: text/html
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<h1>Some HTML</h1>
Expand All @@ -28,4 +28,4 @@ Content-Disposition: attachment; filename=tiny_jpeg.jpg
/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8Q
EBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=
--2753118483D4F18DC5FD1F1A--
--2753118483D4F18DC5FD1F1A--
4 changes: 2 additions & 2 deletions test/fixtures/recursive-part-rendering.eml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Content-Type: multipart/alternative; boundary="foobar"

--foobar
Content-Type: text/plain
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Hello there! 1 + 1 =3D 2
--foobar
Content-Type: text/html
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<a href=3D"/">Hello there! 1 + 1 =3D 2</a>
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/simple-multipart-rendering.eml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="foobar"

--foobar
Content-Type: text/plain
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Some text
--foobar
Content-Type: text/html
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<h1>Some HTML</h1>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/simple-plain-rendering.eml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
To: [email protected]
Subject: Test email
Content-Type: text/plain
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Some text
12 changes: 6 additions & 6 deletions test/mail/message_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,28 @@ defmodule Mail.MessageTest do

test "build_text" do
message = Mail.Message.build_text("Some text")
assert Mail.Message.get_content_type(message) == ["text/plain"]
assert Mail.Message.get_content_type(message) == ["text/plain", {"charset", "UTF-8"}]
assert Mail.Message.get_header(message, :content_transfer_encoding) == :quoted_printable
assert message.body == "Some text"
end

test "build_text when given charset" do
message = Mail.Message.build_text("Some text", charset: "UTF-8")
assert Mail.Message.get_content_type(message) == ["text/plain", {"charset", "UTF-8"}]
message = Mail.Message.build_text("Some text", charset: "US-ASCII")
assert Mail.Message.get_content_type(message) == ["text/plain", {"charset", "US-ASCII"}]
assert Mail.Message.get_header(message, :content_transfer_encoding) == :quoted_printable
assert message.body == "Some text"
end

test "build_html" do
message = Mail.Message.build_html("<h1>Some HTML</h1>")
assert Mail.Message.get_content_type(message) == ["text/html"]
assert Mail.Message.get_content_type(message) == ["text/html", {"charset", "UTF-8"}]
assert Mail.Message.get_header(message, :content_transfer_encoding) == :quoted_printable
assert message.body == "<h1>Some HTML</h1>"
end

test "build_html when given charset" do
message = Mail.Message.build_html("<h1>Some HTML</h1>", charset: "UTF-8")
assert Mail.Message.get_content_type(message) == ["text/html", {"charset", "UTF-8"}]
message = Mail.Message.build_html("<h1>Some HTML</h1>", charset: "US-ASCII")
assert Mail.Message.get_content_type(message) == ["text/html", {"charset", "US-ASCII"}]
assert Mail.Message.get_header(message, :content_transfer_encoding) == :quoted_printable
assert message.body == "<h1>Some HTML</h1>"
end
Expand Down
12 changes: 6 additions & 6 deletions test/mail_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ defmodule MailTest do

assert length(mail.parts) == 0
assert mail.body == "Some text"
assert Mail.Message.get_content_type(mail) == ["text/plain"]
assert Mail.Message.get_content_type(mail) == ["text/plain", {"charset", "UTF-8"}]
end

test "put_text with a multipart" do
Expand All @@ -147,7 +147,7 @@ defmodule MailTest do
part = List.first(mail.parts)

assert part.body == "Some text"
assert Mail.Message.get_content_type(part) == ["text/plain"]
assert Mail.Message.get_content_type(part) == ["text/plain", {"charset", "UTF-8"}]
end

test "put_text replaces existing text part in multipart" do
Expand All @@ -159,7 +159,7 @@ defmodule MailTest do
part = List.first(mail.parts)

assert part.body == "Some other text"
assert Mail.Message.get_content_type(part) == ["text/plain"]
assert Mail.Message.get_content_type(part) == ["text/plain", {"charset", "UTF-8"}]
end

test "get_text with singlepart" do
Expand Down Expand Up @@ -237,7 +237,7 @@ defmodule MailTest do

assert length(mail.parts) == 0
assert mail.body == "<h1>Some HTML</h1>"
assert Mail.Message.get_content_type(mail) == ["text/html"]
assert Mail.Message.get_content_type(mail) == ["text/html", {"charset", "UTF-8"}]
end

test "put_html with a multipart" do
Expand All @@ -247,7 +247,7 @@ defmodule MailTest do
part = List.first(mail.parts)

assert part.body == "<h1>Some HTML</h1>"
assert Mail.Message.get_content_type(part) == ["text/html"]
assert Mail.Message.get_content_type(part) == ["text/html", {"charset", "UTF-8"}]
end

test "put_html replaces existing html part in multipart" do
Expand All @@ -259,7 +259,7 @@ defmodule MailTest do
part = List.first(mail.parts)

assert part.body == "<h1>Some other html</h1>"
assert Mail.Message.get_content_type(part) == ["text/html"]
assert Mail.Message.get_content_type(part) == ["text/html", {"charset", "UTF-8"}]
end

test "get_html with singlepart" do
Expand Down

0 comments on commit d83c47f

Please sign in to comment.