From 905405a46e5610043cc7862a26213e80c95a3492 Mon Sep 17 00:00:00 2001 From: Dejan Strbac Date: Wed, 23 Dec 2015 20:03:53 +0100 Subject: [PATCH] support both formats of email address --- lib/mailex/address.ex | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/mailex/address.ex b/lib/mailex/address.ex index 2247bd3..1cac119 100644 --- a/lib/mailex/address.ex +++ b/lib/mailex/address.ex @@ -7,16 +7,19 @@ defmodule Mailex.Address do def rfc_822_format(email) when is_map(email) do - if email.name do - "#{email.name} <#{email.address}>" + email_address = Map.get(email, "address", Map.get(email, :address)) + email_name = Map.get(email, "name", Map.get(email, :name)) + + if email_name do + "#{email_name} <#{email_address}>" else - name = email.address |> + name = email_address |> String.split("@") |> List.first |> String.split(~r/([^\w\s]|_)/) |> Enum.map(&String.capitalize/1) |> Enum.join " " - "#{name} <#{email.address}>" + "#{name} <#{email_address}>" end end @@ -25,8 +28,10 @@ defmodule Mailex.Address do emails |> Enum.map(&envelope_format(&1)) - def envelope_format(email) when is_map(email), do: - "<#{email.address}>" + def envelope_format(email) when is_map(email) do + email_address = Map.get(email, "address", Map.get(email, :address)) + "<#{email_address}>" + end end \ No newline at end of file