Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/resend/swoosh/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions test/resend/emails_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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|<img src="cid:logo.jpg">|)
|> 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,
Expand Down