From fbcb3528c774805809a2a6adf5940aaf7d86eb8e Mon Sep 17 00:00:00 2001 From: Jarrod Moldrich Date: Fri, 22 Oct 2021 18:10:06 +1100 Subject: [PATCH 1/3] Add arbitrary header overrides for attachments This allows bamboo adapters to be accept overriding headers for special use cases. Updates to those adapters would be required to use this functionality. For e.g., in the specific use-case of adding inlined attachments, some special case headers are required. Now it's possible to add `X-Attachment-Id` and override the Content-Disposition header with `inline` to permit the proper display of inlined content, without an email client showing them as explicit attachments for the user to download. --- lib/bamboo/attachment.ex | 40 +++++++++++++++++++++++++++++----- test/lib/bamboo/email_test.exs | 4 ++-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/bamboo/attachment.ex b/lib/bamboo/attachment.ex index ab82f2cc..66bd7d8a 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/email_test.exs b/test/lib/bamboo/email_test.exs index f60112b1..68d7e17b 100644 --- a/test/lib/bamboo/email_test.exs +++ b/test/lib/bamboo/email_test.exs @@ -92,7 +92,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{content_id: nil, content_type: nil, data: \"content\", filename: nil, path: nil}" + "You must provide a filename for the attachment, instead got: %Bamboo.Attachment{content_id: nil, content_type: nil, data: \"content\", filename: nil, headers: nil, path: nil}" assert_raise RuntimeError, msg, fn -> new_email() |> put_attachment(attachment) @@ -103,7 +103,7 @@ defmodule Bamboo.EmailTest do attachment = %Bamboo.Attachment{filename: "attachment.docx", data: nil} msg = - "The attachment must contain data, instead got: %Bamboo.Attachment{content_id: nil, content_type: nil, data: nil, filename: \"attachment.docx\", path: nil}" + "The attachment must contain data, instead got: %Bamboo.Attachment{content_id: nil, content_type: nil, data: nil, filename: \"attachment.docx\", headers: nil, path: nil}" assert_raise RuntimeError, msg, fn -> new_email() |> put_attachment(attachment) From a77701869ad328f107211d0fe33c3d1125687b7e Mon Sep 17 00:00:00 2001 From: Jarrod Moldrich Date: Sat, 16 Nov 2024 17:35:12 +1100 Subject: [PATCH 2/3] feat: add test for header overrides --- lib/bamboo/attachment.ex | 4 ++-- test/lib/bamboo/attachments_test.exs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/bamboo/attachment.ex b/lib/bamboo/attachment.ex index 66bd7d8a..aff17877 100644 --- a/lib/bamboo/attachment.ex +++ b/lib/bamboo/attachment.ex @@ -37,7 +37,7 @@ defmodule Bamboo.Attachment do filename: "image.png", content_type: "image/png", content_id: "<12387432>", - headers: [content_disposition, "inline", x_attachment_id: "12387432"] + headers: [content_disposition: "inline", x_attachment_id: "12387432"] ) Bamboo.Attachment.new(params["file"]) # Where params["file"] is a %Plug.Upload @@ -57,7 +57,7 @@ defmodule Bamboo.Attachment do filename: "logo.png", data: "content", content_id: "<12387432>", - headers: [content_disposition, "inline", x_attachment_id: "12387432"] + headers: [content_disposition: "inline", x_attachment_id: "12387432"] } ) """ diff --git a/test/lib/bamboo/attachments_test.exs b/test/lib/bamboo/attachments_test.exs index a43f07e3..e7ac35cf 100644 --- a/test/lib/bamboo/attachments_test.exs +++ b/test/lib/bamboo/attachments_test.exs @@ -58,4 +58,21 @@ 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"] + ) + |> IO.inspect() + + assert attachment.content_id == "<12387432>" + assert [content_disposition: "inline", x_attachment_id: "12387432"] = attachment.headers + end end From 8f5fe0757bd1826a01c9d3595964c5df3af69e35 Mon Sep 17 00:00:00 2001 From: Jarrod Moldrich Date: Sun, 17 Nov 2024 10:08:00 +1100 Subject: [PATCH 3/3] fix: remove IO.inspect --- test/lib/bamboo/attachments_test.exs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/lib/bamboo/attachments_test.exs b/test/lib/bamboo/attachments_test.exs index e7ac35cf..e081a036 100644 --- a/test/lib/bamboo/attachments_test.exs +++ b/test/lib/bamboo/attachments_test.exs @@ -70,7 +70,6 @@ defmodule Bamboo.AttachmentTest do content_id: "<12387432>", headers: [content_disposition: "inline", x_attachment_id: "12387432"] ) - |> IO.inspect() assert attachment.content_id == "<12387432>" assert [content_disposition: "inline", x_attachment_id: "12387432"] = attachment.headers