diff --git a/lib/mailex/render.ex b/lib/mailex/render.ex index e825452..026e3fc 100644 --- a/lib/mailex/render.ex +++ b/lib/mailex/render.ex @@ -109,13 +109,13 @@ defmodule Mailex.Render do def headers_for(email) do headers = [] - if email.reply_to, do: + if email.reply_to && (length(email.reply_to) > 0), do: headers = [ { "Reply-To", email.reply_to |> stringify_addresses } ] - if email.bcc, do: + if email.bcc && (length(email.bcc) > 0), do: headers = [ { "Bcc", email.bcc |> stringify_addresses } | headers ] - if email.cc, do: + if email.cc && (length(email.cc) > 0), do: headers = [ { "Cc", email.cc |> stringify_addresses } | headers ] [ { "From", email.from |> stringify_addresses }, @@ -124,6 +124,9 @@ defmodule Mailex.Render do end + def stringify_addresses(nil), do: "" + def stringify_addresses([]), do: "" + def stringify_addresses(addresses) do addresses = addresses |> Address.rfc_822_format if is_list(addresses) do diff --git a/test/mailex_test.exs b/test/mailex_test.exs index 81491e3..c285108 100644 --- a/test/mailex_test.exs +++ b/test/mailex_test.exs @@ -2,7 +2,47 @@ defmodule MailexTest do use ExUnit.Case doctest Mailex - test "the truth" do - assert 1 + 1 == 2 + defp email_minimal do + %Mailex.Email{ + from: %Mailex.Address{ address: "test_a@gmail.com" }, + to: [%Mailex.Address{ address: "test_b@gmail.com" }] + } end + + defp email_with_reply_to do + %Mailex.Email{ + from: %Mailex.Address{ address: "test_a@gmail.com" }, + to: [%Mailex.Address{ address: "test_b@gmail.com" }], + reply_to: [%Mailex.Address{ address: "test_aa@gmail.com" }, %Mailex.Address{ address: "test_a@gmx.com" }], + subject: "You've got mail", + text: "Hello World!" + } + end + + defp email_without_attachments do + %Mailex.Email{ + from: %Mailex.Address{ name: "Yankee", address: "test_a@gmail.com" }, + to: [%Mailex.Address{ address: "test_c@gmail.com" }], + subject: "You've got mail", + text: "Hello World!" + } + end + + defp email_with_attachments do + %Mailex.Email{ + from: %Mailex.Address{ name: "Yankee", address: "test_a@gmail.com" }, + to: [%Mailex.Address{ address: "test_c@gmail.com" }], + subject: "You've got mail", + text: "Hello World", + attachments: [Mailex.Attachment.inline!("test/data/logo.gif")] + } + end + + test "Messages render" do + assert email_minimal |> Mailex.Render.render + assert email_with_reply_to |> Mailex.Render.render + assert email_without_attachments |> Mailex.Render.render + assert email_with_attachments |> Mailex.Render.render + end + end