From 4b5ba810f0b0b58cc0c83c6e505372d2d6052a4a Mon Sep 17 00:00:00 2001 From: Luisa Amorim <19151643+LuisaAPF@users.noreply.github.com> Date: Wed, 14 Sep 2022 12:23:50 +0200 Subject: [PATCH] Punycode domain part of emails (#198) --- lib/bamboo/adapters/smtp_adapter.ex | 11 ++++++--- .../lib/bamboo/adapters/smtp_adapter_test.exs | 23 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/bamboo/adapters/smtp_adapter.ex b/lib/bamboo/adapters/smtp_adapter.ex index 6dc57ea..b638d0d 100644 --- a/lib/bamboo/adapters/smtp_adapter.ex +++ b/lib/bamboo/adapters/smtp_adapter.ex @@ -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)) diff --git a/test/lib/bamboo/adapters/smtp_adapter_test.exs b/test/lib/bamboo/adapters/smtp_adapter_test.exs index 04d3107..b35e8b1 100644 --- a/test/lib/bamboo/adapters/smtp_adapter_test.exs +++ b/test/lib/bamboo/adapters/smtp_adapter_test.exs @@ -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: "

Bamboo is awesome!

", text_body: "*Bamboo is awesome!*", @@ -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}>"