forked from migadu/mailex
Compare commits
No commits in common. "8981033491ab2fe54638b516ea3de738edefb33d" and "6a6bd3c98432dc54cb05659dae60df0c78e5ef3f" have entirely different histories.
8981033491
...
6a6bd3c984
@ -14,17 +14,17 @@ defmodule Mailex do
|
||||
config = Keyword.merge(@config_defaults, config || [])
|
||||
|
||||
message = email |> Mailex.Render.render
|
||||
envelope_from = email.from |> Mailex.Address.envelope_format
|
||||
envelope_to = (email.to ++ (email.cc || []) ++ (email.bcc || [])) |> Mailex.Address.envelope_format
|
||||
from = email.from |> Mailex.Address.envelope_format
|
||||
to = email.to |> Mailex.Address.envelope_format
|
||||
|
||||
if Keyword.get(config, :relay) do
|
||||
envelope = { envelope_from, envelope_to, message }
|
||||
envelope = { from, 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: #{envelope_from}\nTO: #{envelope_to}\n\nRAW START -------------\n#{message}\nRAW END -------------\n\n"
|
||||
IO.puts "\n\n[[[ Mailex ]]]\n\nFROM: #{from}\nTO: #{to}\n\nRAW START -------------\n#{message}\nRAW END -------------\n\n"
|
||||
{:ok, "message dumped to console"}
|
||||
end
|
||||
end
|
||||
|
@ -7,19 +7,16 @@ defmodule Mailex.Address do
|
||||
|
||||
|
||||
def rfc_822_format(email) when is_map(email) do
|
||||
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}>"
|
||||
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}>"
|
||||
Enum.join " "
|
||||
"#{name} <#{email.address}>"
|
||||
end
|
||||
end
|
||||
|
||||
@ -28,10 +25,8 @@ defmodule Mailex.Address do
|
||||
emails |> Enum.map(&envelope_format(&1))
|
||||
|
||||
|
||||
def envelope_format(email) when is_map(email) do
|
||||
email_address = Map.get(email, "address", Map.get(email, :address))
|
||||
"<#{email_address}>"
|
||||
end
|
||||
def envelope_format(email) when is_map(email), do:
|
||||
"<#{email.address}>"
|
||||
|
||||
|
||||
end
|
@ -1,5 +1,5 @@
|
||||
defmodule Mailex.Email do
|
||||
|
||||
defstruct subject: nil, from: nil, reply_to: nil, to: nil, cc: nil, bcc: nil, headers: nil, attachments: nil, html: nil, text: nil
|
||||
defstruct subject: nil, from: nil, reply_to: nil, to: nil, cc: nil, bcc: nil, attachments: nil, html: nil, text: nil
|
||||
|
||||
end
|
||||
|
@ -112,18 +112,14 @@ defmodule Mailex.Render do
|
||||
if email.reply_to && (length(email.reply_to) > 0), do:
|
||||
headers = [ { "Reply-To", email.reply_to |> stringify_addresses } ]
|
||||
|
||||
# BCC should not go into headers
|
||||
if email.bcc && (length(email.bcc) > 0), do:
|
||||
headers = [ { "Bcc", email.bcc |> stringify_addresses } | headers ]
|
||||
|
||||
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 ]
|
||||
|
||||
if email.headers && (length(email.headers) > 0), do:
|
||||
headers = headers ++ email.headers
|
||||
|
||||
[ { "From", email.from |> stringify_addresses },
|
||||
{ "To", email.to |> stringify_addresses },
|
||||
{ "Subject", email.subject || "" } | headers ]
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user