diff --git a/lib/bamboo/adapters/send_grid_adapter.ex b/lib/bamboo/adapters/send_grid_adapter.ex index 44ae8e69..a2cd53be 100644 --- a/lib/bamboo/adapters/send_grid_adapter.ex +++ b/lib/bamboo/adapters/send_grid_adapter.ex @@ -397,16 +397,34 @@ defmodule Bamboo.SendGridAdapter do attachments |> Enum.reverse() |> Enum.map(fn attachment -> - %{ - filename: attachment.filename, - type: attachment.content_type, - content: Base.encode64(attachment.data) - } + maybe_append_content_id( + %{ + filename: attachment.filename, + type: attachment.content_type, + content: Base.encode64(attachment.data), + disposition: attachment_disposition(attachment) + }, + attachment + ) end) Map.put(body, :attachments, transformed) end + defp maybe_append_content_id(map, %{content_id: nil}) do + map + end + + defp maybe_append_content_id(map, %{content_id: content_id}) do + Map.put(map, :content_id, content_id) + end + + defp attachment_disposition(%Bamboo.Attachment{content_id: cid}) when is_binary(cid) do + "inline" + end + + defp attachment_disposition(_), do: "attachment" + defp put_addresses(body, _, []), do: body defp put_addresses(body, field, addresses), diff --git a/test/lib/bamboo/adapters/send_grid_adapter_test.exs b/test/lib/bamboo/adapters/send_grid_adapter_test.exs index 5707f59c..07178849 100644 --- a/test/lib/bamboo/adapters/send_grid_adapter_test.exs +++ b/test/lib/bamboo/adapters/send_grid_adapter_test.exs @@ -161,7 +161,43 @@ defmodule Bamboo.SendGridAdapterTest do %{ "type" => "text/plain", "filename" => "attachment.txt", - "content" => "VGVzdCBBdHRhY2htZW50Cg==" + "content" => "VGVzdCBBdHRhY2htZW50Cg==", + "disposition" => "attachment" + } + ] + end + + test "deliver/2 sends from, html and text body, subject, headers and inline attachment" do + email = + [ + from: {"From", "from@foo.com"}, + subject: "My Subject", + text_body: "TEXT BODY", + html_body: "HTML BODY " + ] + |> new_email() + |> Email.put_header("Reply-To", "reply@foo.com") + |> Email.put_attachment(Path.join(__DIR__, "../../../support/attachment.txt"), content_id: "myimg") + + SendGridAdapter.deliver(email, @config) + + assert SendGridAdapter.supports_attachments?() + assert_receive {:fake_sendgrid, %{params: params, req_headers: headers}} + + assert params["from"]["name"] == elem(email.from, 0) + assert params["from"]["email"] == elem(email.from, 1) + assert params["subject"] == email.subject + assert Enum.member?(params["content"], %{"type" => "text/plain", "value" => email.text_body}) + assert Enum.member?(params["content"], %{"type" => "text/html", "value" => email.html_body}) + assert Enum.member?(headers, {"authorization", "Bearer #{@config[:api_key]}"}) + + assert params["attachments"] == [ + %{ + "type" => "text/plain", + "filename" => "attachment.txt", + "content" => "VGVzdCBBdHRhY2htZW50Cg==", + "disposition" => "inline", + "content_id" => "myimg" } ] end