handle special characters in names
This commit is contained in:
parent
bb9abe2cdf
commit
ee242ca92a
@ -29,4 +29,4 @@ defmodule Mailex do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -8,18 +8,28 @@ defmodule Mailex.Address do
|
|||||||
|
|
||||||
def rfc_822_format(email) when is_map(email) do
|
def rfc_822_format(email) when is_map(email) do
|
||||||
email_address = Map.get(email, "address", Map.get(email, :address))
|
email_address = Map.get(email, "address", Map.get(email, :address))
|
||||||
email_name = Map.get(email, "name", Map.get(email, :name))
|
email_name = email
|
||||||
|
|> Map.get("name", Map.get(email, :name))
|
||||||
|
|> prepare_name(email_address)
|
||||||
|
"#{email_name} <#{email_address}>"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
defp prepare_name(email_name, address) do
|
||||||
if email_name do
|
if email_name do
|
||||||
"#{email_name} <#{email_address}>"
|
email_name = String.strip(email_name)
|
||||||
else
|
else
|
||||||
name = email_address |>
|
email_name = address |>
|
||||||
String.split("@") |>
|
String.split("@") |>
|
||||||
List.first |>
|
List.first |>
|
||||||
String.split(~r/([^\w\s]|_)/) |>
|
String.split(~r/([^\w\s]|_)/) |>
|
||||||
Enum.map(&String.capitalize/1) |>
|
Enum.map(&String.capitalize/1) |>
|
||||||
Enum.join(" ")
|
Enum.join(" ")
|
||||||
"#{name} <#{email_address}>"
|
end
|
||||||
|
if String.match?(email_name, ~r/[\(\)\<\>\@\,\;\:\"\.\[\]\\]/) do
|
||||||
|
"\"\\\"" <> String.replace(email_name, "\"", "\\\"") <> "\\\"\""
|
||||||
|
else
|
||||||
|
email_name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -34,4 +44,4 @@ defmodule Mailex.Address do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -38,11 +38,19 @@ defmodule MailexTest do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp email_with_special_chars_in_names do
|
||||||
|
%Mailex.Email{
|
||||||
|
from: %Mailex.Address{ name: ", Test", address: "test_a@gmail.com" },
|
||||||
|
to: [%Mailex.Address{ name: ":,<>[]()'\"Whatever", address: "test_b@gmail.com" }]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
test "Messages render" do
|
test "Messages render" do
|
||||||
assert email_minimal |> Mailex.Render.render
|
assert email_minimal |> Mailex.Render.render
|
||||||
assert email_with_reply_to |> Mailex.Render.render
|
assert email_with_reply_to |> Mailex.Render.render
|
||||||
assert email_without_attachments |> Mailex.Render.render
|
assert email_without_attachments |> Mailex.Render.render
|
||||||
assert email_with_attachments |> Mailex.Render.render
|
assert email_with_attachments |> Mailex.Render.render
|
||||||
|
assert email_with_special_chars_in_names |> Mailex.Render.render
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user