From ae539c8d53245ebe5a05341476ba48d7802fe240 Mon Sep 17 00:00:00 2001 From: Dejan Strbac Date: Sun, 20 Dec 2015 11:28:04 +0100 Subject: [PATCH 1/5] optional to --- lib/mailex/render.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/mailex/render.ex b/lib/mailex/render.ex index 026e3fc..64d9b30 100644 --- a/lib/mailex/render.ex +++ b/lib/mailex/render.ex @@ -118,8 +118,10 @@ defmodule Mailex.Render do if email.cc && (length(email.cc) > 0), do: headers = [ { "Cc", email.cc |> stringify_addresses } | headers ] + if email.to && (length(email.to) > 0), do: + headers = [ { "To", email.to |> stringify_addresses } | headers ] + [ { "From", email.from |> stringify_addresses }, - { "To", email.to |> stringify_addresses }, { "Subject", email.subject || "" } | headers ] end From f40ecb33f881acbb9d46e6543dffd1993f6dd344 Mon Sep 17 00:00:00 2001 From: Dejan Strbac Date: Wed, 23 Dec 2015 19:02:56 +0100 Subject: [PATCH 2/5] add extra headers --- lib/mailex/email.ex | 2 +- lib/mailex/render.ex | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/mailex/email.ex b/lib/mailex/email.ex index 504db55..d3e1ea0 100644 --- a/lib/mailex/email.ex +++ b/lib/mailex/email.ex @@ -1,5 +1,5 @@ defmodule Mailex.Email do - defstruct subject: nil, from: nil, reply_to: nil, to: nil, cc: nil, bcc: nil, attachments: nil, html: nil, text: nil + defstruct subject: nil, from: nil, reply_to: nil, to: nil, cc: nil, bcc: nil, headers: nil, attachments: nil, html: nil, text: nil end diff --git a/lib/mailex/render.ex b/lib/mailex/render.ex index 64d9b30..be78136 100644 --- a/lib/mailex/render.ex +++ b/lib/mailex/render.ex @@ -121,6 +121,9 @@ defmodule Mailex.Render do if email.to && (length(email.to) > 0), do: headers = [ { "To", email.to |> stringify_addresses } | headers ] + if email.headers && (length(email.headers) > 0), do: + headers = headers ++ email.headers + [ { "From", email.from |> stringify_addresses }, { "Subject", email.subject || "" } | headers ] end From 905405a46e5610043cc7862a26213e80c95a3492 Mon Sep 17 00:00:00 2001 From: Dejan Strbac Date: Wed, 23 Dec 2015 20:03:53 +0100 Subject: [PATCH 3/5] 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 From fe8e2dfbe91d397f820d92ce6aa50c8a715dcd03 Mon Sep 17 00:00:00 2001 From: Dejan Strbac Date: Sun, 21 Feb 2016 22:29:07 +0100 Subject: [PATCH 4/5] bcc / cc in envelope --- lib/mailex.ex | 8 ++++---- lib/mailex/address.ex | 2 +- lib/mailex/render.ex | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/mailex.ex b/lib/mailex.ex index 8db767e..1674204 100644 --- a/lib/mailex.ex +++ b/lib/mailex.ex @@ -14,17 +14,17 @@ defmodule Mailex do config = Keyword.merge(@config_defaults, config || []) message = email |> Mailex.Render.render - from = email.from |> Mailex.Address.envelope_format - to = email.to |> Mailex.Address.envelope_format + envelope_from = email.from |> Mailex.Address.envelope_format + envelope_to = email.to ++ (email.cc || []) ++ (email.bcc || []) if Keyword.get(config, :relay) do - envelope = { from, to, message } + envelope = { envelope_from, envelope_to, message } case :gen_smtp_client.send_blocking(envelope, config) do { :error, msg } -> { :error, msg } msg -> { :ok, msg } end else - IO.puts "\n\n[[[ Mailex ]]]\n\nFROM: #{from}\nTO: #{to}\n\nRAW START -------------\n#{message}\nRAW END -------------\n\n" + IO.puts "\n\n[[[ Mailex ]]]\n\nFROM: #{envelope_from}\nTO: #{envelope_to}\n\nRAW START -------------\n#{message}\nRAW END -------------\n\n" {:ok, "message dumped to console"} end end diff --git a/lib/mailex/address.ex b/lib/mailex/address.ex index 1cac119..1e51e60 100644 --- a/lib/mailex/address.ex +++ b/lib/mailex/address.ex @@ -18,7 +18,7 @@ defmodule Mailex.Address do List.first |> String.split(~r/([^\w\s]|_)/) |> Enum.map(&String.capitalize/1) |> - Enum.join " " + Enum.join(" ") "#{name} <#{email_address}>" end end diff --git a/lib/mailex/render.ex b/lib/mailex/render.ex index be78136..7003d95 100644 --- a/lib/mailex/render.ex +++ b/lib/mailex/render.ex @@ -112,8 +112,7 @@ defmodule Mailex.Render do if email.reply_to && (length(email.reply_to) > 0), do: headers = [ { "Reply-To", email.reply_to |> stringify_addresses } ] - if email.bcc && (length(email.bcc) > 0), do: - headers = [ { "Bcc", email.bcc |> stringify_addresses } | headers ] + # BCC should not go into headers if email.cc && (length(email.cc) > 0), do: headers = [ { "Cc", email.cc |> stringify_addresses } | headers ] From 8981033491ab2fe54638b516ea3de738edefb33d Mon Sep 17 00:00:00 2001 From: Dejan Strbac Date: Sun, 21 Feb 2016 23:11:37 +0100 Subject: [PATCH 5/5] add missing envelope format --- lib/mailex.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mailex.ex b/lib/mailex.ex index 1674204..fc4f2c7 100644 --- a/lib/mailex.ex +++ b/lib/mailex.ex @@ -15,7 +15,7 @@ defmodule Mailex do message = email |> Mailex.Render.render envelope_from = email.from |> Mailex.Address.envelope_format - envelope_to = email.to ++ (email.cc || []) ++ (email.bcc || []) + envelope_to = (email.to ++ (email.cc || []) ++ (email.bcc || [])) |> Mailex.Address.envelope_format if Keyword.get(config, :relay) do envelope = { envelope_from, envelope_to, message }