1
0
forked from migadu/mailex

Compare commits

...

5 Commits

Author SHA1 Message Date
Dejan Strbac
8981033491 add missing envelope format 2016-02-21 23:11:37 +01:00
Dejan Strbac
fe8e2dfbe9 bcc / cc in envelope 2016-02-21 22:29:07 +01:00
Dejan Strbac
905405a46e support both formats of email address 2015-12-23 20:03:53 +01:00
Dejan Strbac
f40ecb33f8 add extra headers 2015-12-23 19:02:56 +01:00
Dejan Strbac
ae539c8d53 optional to 2015-12-20 11:28:04 +01:00
4 changed files with 24 additions and 15 deletions

View File

@ -14,17 +14,17 @@ defmodule Mailex do
config = Keyword.merge(@config_defaults, config || []) config = Keyword.merge(@config_defaults, config || [])
message = email |> Mailex.Render.render message = email |> Mailex.Render.render
from = email.from |> Mailex.Address.envelope_format envelope_from = email.from |> Mailex.Address.envelope_format
to = email.to |> Mailex.Address.envelope_format envelope_to = (email.to ++ (email.cc || []) ++ (email.bcc || [])) |> Mailex.Address.envelope_format
if Keyword.get(config, :relay) do 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 case :gen_smtp_client.send_blocking(envelope, config) do
{ :error, msg } -> { :error, msg } { :error, msg } -> { :error, msg }
msg -> { :ok, msg } msg -> { :ok, msg }
end end
else 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"} {:ok, "message dumped to console"}
end end
end end

View File

@ -7,16 +7,19 @@ defmodule Mailex.Address do
def rfc_822_format(email) when is_map(email) do def rfc_822_format(email) when is_map(email) do
if email.name do email_address = Map.get(email, "address", Map.get(email, :address))
"#{email.name} <#{email.address}>" email_name = Map.get(email, "name", Map.get(email, :name))
if email_name do
"#{email_name} <#{email_address}>"
else else
name = email.address |> name = email_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}>" "#{name} <#{email_address}>"
end end
end end
@ -25,8 +28,10 @@ defmodule Mailex.Address do
emails |> Enum.map(&envelope_format(&1)) emails |> Enum.map(&envelope_format(&1))
def envelope_format(email) when is_map(email), do: def envelope_format(email) when is_map(email) do
"<#{email.address}>" email_address = Map.get(email, "address", Map.get(email, :address))
"<#{email_address}>"
end
end end

View File

@ -1,5 +1,5 @@
defmodule Mailex.Email do 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 end

View File

@ -112,14 +112,18 @@ defmodule Mailex.Render do
if email.reply_to && (length(email.reply_to) > 0), do: if email.reply_to && (length(email.reply_to) > 0), do:
headers = [ { "Reply-To", email.reply_to |> stringify_addresses } ] headers = [ { "Reply-To", email.reply_to |> stringify_addresses } ]
if email.bcc && (length(email.bcc) > 0), do: # BCC should not go into headers
headers = [ { "Bcc", email.bcc |> stringify_addresses } | headers ]
if email.cc && (length(email.cc) > 0), do: if email.cc && (length(email.cc) > 0), do:
headers = [ { "Cc", email.cc |> stringify_addresses } | headers ] headers = [ { "Cc", email.cc |> stringify_addresses } | headers ]
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 }, [ { "From", email.from |> stringify_addresses },
{ "To", email.to |> stringify_addresses },
{ "Subject", email.subject || "" } | headers ] { "Subject", email.subject || "" } | headers ]
end end