Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add arbitrary header overrides for attachments #631

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
40 changes: 35 additions & 5 deletions lib/bamboo/attachment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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 \\ [])

Expand All @@ -44,14 +72,16 @@ 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__{
path: path,
data: data,
filename: filename,
content_type: content_type,
content_id: content_id
content_id: content_id,
headers: headers
}
end

Expand Down
4 changes: 2 additions & 2 deletions test/lib/bamboo/email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down