diff --git a/lib/resend/swoosh/adapter.ex b/lib/resend/swoosh/adapter.ex index 4cca05f..7288edd 100644 --- a/lib/resend/swoosh/adapter.ex +++ b/lib/resend/swoosh/adapter.ex @@ -68,11 +68,13 @@ defmodule Resend.Swoosh.Adapter do end defp format_attachment(%Swoosh.Attachment{} = attachment) do + cid = if is_nil(attachment.cid), do: attachment.filename, else: attachment.cid + %Resend.Emails.Attachment{ content: Swoosh.Attachment.get_content(attachment, :base64), content_type: attachment.content_type, filename: attachment.filename, - content_id: attachment.cid + content_id: cid } end diff --git a/test/resend/emails_test.exs b/test/resend/emails_test.exs index 14c2e63..32e1678 100644 --- a/test/resend/emails_test.exs +++ b/test/resend/emails_test.exs @@ -179,6 +179,41 @@ defmodule Resend.EmailsTest do assert {:ok, _} = Resend.Swoosh.Adapter.deliver(email, config) end + test "Swoosh adapter uses filename as content_id when cid is not set", context do + email = + Swoosh.Email.new() + |> Swoosh.Email.to(context.to) + |> Swoosh.Email.from(context.from) + |> Swoosh.Email.subject("Test Inline Image") + |> Swoosh.Email.html_body(~s||) + |> Swoosh.Email.attachment( + Swoosh.Attachment.new( + {:data, "fake_binary_data"}, + filename: "logo.jpg", + content_type: "image/jpeg", + type: :inline + ) + ) + + Req.Test.stub(Resend.ReqStub, fn conn -> + assert conn.method == "POST" + assert conn.request_path == "/emails" + + {:ok, body, conn} = Plug.Conn.read_body(conn) + body = Jason.decode!(body) + + assert [attachment] = body["attachments"] + assert attachment["content_id"] == "logo.jpg" + + conn + |> Plug.Conn.put_resp_content_type("application/json") + |> Plug.Conn.send_resp(200, Jason.encode!(%{"id" => context.sent_email_id})) + end) + + config = [api_key: context.api_key] + assert {:ok, _} = Resend.Swoosh.Adapter.deliver(email, config) + end + test "list/1 lists all sent emails", context do ClientMock.mock_request(context, method: :get,