Skip to content

Commit

Permalink
Punycode domain part of emails (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisaAPF authored Sep 14, 2022
1 parent 184d371 commit 4b5ba81
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
11 changes: 8 additions & 3 deletions lib/bamboo/adapters/smtp_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,14 @@ defmodule Bamboo.SMTPAdapter do
|> raise_on_missing_configuration(config)
end

defp format_email({nil, email}, _format), do: email
defp format_email({name, email}, true), do: "#{rfc822_encode(name)} <#{email}>"
defp format_email({_name, email}, false), do: email
defp puny_encode(email) do
[local_part, domain_part] = String.split(email, "@")
Enum.join([local_part, :idna.utf8_to_ascii(domain_part)], "@")
end

defp format_email({nil, email}, _format), do: puny_encode(email)
defp format_email({name, email}, true), do: "#{rfc822_encode(name)} <#{puny_encode(email)}>"
defp format_email({_name, email}, false), do: puny_encode(email)

defp format_email(emails, format) when is_list(emails) do
Enum.map(emails, &format_email(&1, format))
Expand Down
23 changes: 19 additions & 4 deletions test/lib/bamboo/adapters/smtp_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ defmodule Bamboo.SMTPAdapterTest do
]

@email_in_utf8 [
from: {"John Doe", "john@doe.com"},
to: [{"Jane Doe", "jane@doe.com"}],
cc: [{"Richard Roe", "richard@roe.com"}],
bcc: [{"Mary Major", "mary@major.com"}, {"Joe Major", "joe@major.com"}],
from: {"John Doe", "john@döé.com"},
to: [{"Jane Doe", "jane@döé.com"}],
cc: [{"Richard Roe", "richard@röé.com"}],
bcc: [{"Mary Major", "mary@major.com"}, {"Joe Major", "joe@mãjor.com"}],
subject: "日本語のsubject",
html_body: "<h1>Bamboo is awesome!</h1>",
text_body: "*Bamboo is awesome!*",
Expand Down Expand Up @@ -790,6 +790,21 @@ defmodule Bamboo.SMTPAdapterTest do
assert String.contains?(raw_email, rfc822_subject)
end

test "check punycode of domain part in email address for to, from, bcc and cc" do
bamboo_email =
@email_in_utf8
|> new_email()
bamboo_config = configuration()
{:ok, "200 Ok 1234567890"} = SMTPAdapter.deliver(bamboo_email, bamboo_config)
[{{from, to, _raw_email}, _gen_smtp_config}] = FakeGenSMTP.fetch_sent_emails()
assert from == "john@xn--d-bga2b.com"
assert Enum.member?(to, "jane@xn--d-bga2b.com")
assert Enum.member?(to, "richard@xn--r-bga2b.com")
assert Enum.member?(to, "joe@xn--mjor-goa.com")
assert Enum.member?(to, "mary@major.com")
end


defp format_email(emails), do: format_email(emails, true)

defp format_email({name, email}, true), do: "#{rfc822_encode(name)} <#{email}>"
Expand Down

0 comments on commit 4b5ba81

Please sign in to comment.