diff --git a/lib/bamboo/attachment.ex b/lib/bamboo/attachment.ex index ab82f2cc..aff17877 100644 --- a/lib/bamboo/attachment.ex +++ b/lib/bamboo/attachment.ex @@ -2,14 +2,15 @@ defmodule Bamboo.Attachment do @moduledoc """ """ - defstruct filename: nil, content_type: nil, path: nil, data: nil, content_id: nil + defstruct filename: nil, content_type: nil, path: nil, data: nil, content_id: nil, headers: nil @type t :: %__MODULE__{ path: nil | String.t(), filename: nil | String.t(), content_type: nil | String.t(), data: nil | binary(), - content_id: nil | String.t() + content_id: nil | String.t(), + headers: nil | [] } @doc ~S""" @@ -26,12 +27,39 @@ defmodule Bamboo.Attachment do Bamboo.Attachment.new("/path/to/attachment.png") Bamboo.Attachment.new("/path/to/attachment.png", filename: "image.png") - Bamboo.Attachment.new("/path/to/attachment.png", filename: "image.png", content_type: "image/png", content_id: "12387432") + Bamboo.Attachment.new("/path/to/attachment.png", + filename: "image.png", + content_type: "image/png", + content_id: "12387432" + ) + Bamboo.Attachment.new( + "/path/to/attachment.png", + filename: "image.png", + content_type: "image/png", + content_id: "<12387432>", + headers: [content_disposition: "inline", x_attachment_id: "12387432"] + ) Bamboo.Attachment.new(params["file"]) # Where params["file"] is a %Plug.Upload email |> put_html_layout({LayoutView, "email.html"}) - |> put_attachment(%Bamboo.Attachment{content_type: "image/png", filename: "logo.png", data: "content", content_id: "2343333333"}) + |> put_attachment( + %Bamboo.Attachment{ + content_type: "image/png", + filename: "logo.png", + data: "content", + content_id: "2343333333" + } + ) + |> put_attachment( + %Bamboo.Attachment{ + content_type: "image/png", + filename: "logo.png", + data: "content", + content_id: "<12387432>", + headers: [content_disposition: "inline", x_attachment_id: "12387432"] + } + ) """ def new(path, opts \\ []) @@ -44,6 +72,7 @@ defmodule Bamboo.Attachment do filename = opts[:filename] || Path.basename(path) content_type = opts[:content_type] || determine_content_type(path) content_id = opts[:content_id] + headers = opts[:headers] data = File.read!(path) %__MODULE__{ @@ -51,7 +80,8 @@ defmodule Bamboo.Attachment do data: data, filename: filename, content_type: content_type, - content_id: content_id + content_id: content_id, + headers: headers } end diff --git a/test/lib/bamboo/attachments_test.exs b/test/lib/bamboo/attachments_test.exs index a43f07e3..e081a036 100644 --- a/test/lib/bamboo/attachments_test.exs +++ b/test/lib/bamboo/attachments_test.exs @@ -58,4 +58,20 @@ defmodule Bamboo.AttachmentTest do assert attachment.filename == "my-attachment.doc" assert attachment.data end + + test "create an attachment with headers and content ID (like an inline image)" do + path = Path.join(__DIR__, "../../support/attachment.docx") + + attachment = + Attachment.new( + path, + filename: "image.png", + content_type: "image/png", + content_id: "<12387432>", + headers: [content_disposition: "inline", x_attachment_id: "12387432"] + ) + + assert attachment.content_id == "<12387432>" + assert [content_disposition: "inline", x_attachment_id: "12387432"] = attachment.headers + end end diff --git a/test/lib/bamboo/email_test.exs b/test/lib/bamboo/email_test.exs index d1b7b638..73c22f3a 100644 --- a/test/lib/bamboo/email_test.exs +++ b/test/lib/bamboo/email_test.exs @@ -93,7 +93,7 @@ defmodule Bamboo.EmailTest do attachment = %Bamboo.Attachment{filename: nil, data: "content"} msg = - "You must provide a filename for the attachment, instead got: %Bamboo.Attachment{filename: nil, content_type: nil, path: nil, data: \"content\", content_id: nil}" + "You must provide a filename for the attachment, instead got: %Bamboo.Attachment{filename: nil, content_type: nil, path: nil, data: \"content\", content_id: nil, headers: nil}" assert_raise RuntimeError, msg, fn -> put_attachment(new_email(), attachment) @@ -104,7 +104,7 @@ defmodule Bamboo.EmailTest do attachment = %Bamboo.Attachment{filename: "attachment.docx", data: nil} msg = - "The attachment must contain data, instead got: %Bamboo.Attachment{filename: \"attachment.docx\", content_type: nil, path: nil, data: nil, content_id: nil}" + "The attachment must contain data, instead got: %Bamboo.Attachment{filename: \"attachment.docx\", content_type: nil, path: nil, data: nil, content_id: nil, headers: nil}" assert_raise RuntimeError, msg, fn -> put_attachment(new_email(), attachment)