Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode name in address if necessary #189

Merged
merged 2 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mail/renderers/rfc_2822.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ defmodule Mail.Renderers.RFC2822 do
end
end

defp render_address({name, email}), do: ~s("#{name}" <#{validate_address(email)}>)
defp render_address({name, email}), do: "#{encode_header_value(~s("#{name}"), :quoted_printable)} <#{validate_address(email)}>"
defp render_address(email), do: validate_address(email)
defp render_subtypes([]), do: []

Expand Down
19 changes: 18 additions & 1 deletion test/mail/message_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ defmodule Mail.MessageTest do
assert %Mail.Message{headers: %{"subject" => ^subject}} = Mail.Parsers.RFC2822.parse(txt)
end

test "UTF-8 in subject (quoted printable with spaces, RFC 2047§4.2 (2)" do
test "UTF-8 in subject (quoted printable with spaces, RFC 2047§4.2 (2))" do
subject = "test 😀 test"

mail =
Expand All @@ -240,6 +240,23 @@ defmodule Mail.MessageTest do
assert %Mail.Message{headers: %{"subject" => ^subject}} = Mail.Parsers.RFC2822.parse(mail)
end

test "UTF-8 in addresses" do
from = {"Joachim Löw", "[email protected]"}
to = {"Wolfgang Schüler", "[email protected]"}

txt =
Mail.build()
|> Mail.put_from(from)
|> Mail.put_to(to)
|> Mail.render()

encoded_from = ~s(From: =?UTF-8?Q?"#{Mail.Encoders.QuotedPrintable.encode(elem(from, 0))}"?= <#{elem(from, 1)}>)
encoded_to = ~s(To: =?UTF-8?Q?"#{Mail.Encoders.QuotedPrintable.encode(elem(to, 0))}"?= <#{elem(to, 1)}>)

assert txt =~ encoded_from
assert txt =~ encoded_to
end

test "UTF-8 in other header" do
file_name = "READMEüä.md"

Expand Down
Loading