2015-11-25 17:10:43 +01:00
|
|
|
defmodule Mailex do
|
|
|
|
|
|
|
|
@config_defaults [
|
|
|
|
relay: nil,
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
port: 25,
|
|
|
|
ssl: false,
|
|
|
|
tls: :never,
|
|
|
|
auth: :always
|
|
|
|
]
|
|
|
|
|
|
|
|
def deliver(email, config \\ []) do
|
2015-11-25 19:58:56 +01:00
|
|
|
config = Keyword.merge(@config_defaults, config || [])
|
2015-11-25 17:10:43 +01:00
|
|
|
|
|
|
|
message = email |> Mailex.Render.render
|
2016-02-21 22:29:07 +01:00
|
|
|
envelope_from = email.from |> Mailex.Address.envelope_format
|
2016-02-21 23:11:37 +01:00
|
|
|
envelope_to = (email.to ++ (email.cc || []) ++ (email.bcc || [])) |> Mailex.Address.envelope_format
|
2015-11-25 17:10:43 +01:00
|
|
|
|
|
|
|
if Keyword.get(config, :relay) do
|
2016-02-21 22:29:07 +01:00
|
|
|
envelope = { envelope_from, envelope_to, message }
|
2015-11-25 17:10:43 +01:00
|
|
|
case :gen_smtp_client.send_blocking(envelope, config) do
|
|
|
|
{ :error, msg } -> { :error, msg }
|
|
|
|
msg -> { :ok, msg }
|
|
|
|
end
|
|
|
|
else
|
2016-02-21 22:29:07 +01:00
|
|
|
IO.puts "\n\n[[[ Mailex ]]]\n\nFROM: #{envelope_from}\nTO: #{envelope_to}\n\nRAW START -------------\n#{message}\nRAW END -------------\n\n"
|
2015-11-25 19:58:56 +01:00
|
|
|
{:ok, "message dumped to console"}
|
2015-11-25 17:10:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-05 22:32:03 +02:00
|
|
|
end
|