-
Notifications
You must be signed in to change notification settings - Fork 341
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
Attachments: Support file data in attachment struct + mailgun attachment support #292
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ defmodule Bamboo.Email do | |
assigns: %{}, | ||
private: %{} | ||
|
||
alias Bamboo.Email | ||
alias Bamboo.{Email, Attachment} | ||
|
||
@address_functions ~w(from to cc bcc)a | ||
@attribute_pipe_functions ~w(subject text_body html_body)a | ||
|
@@ -203,7 +203,27 @@ defmodule Bamboo.Email do | |
end | ||
|
||
@doc ~S""" | ||
Adds an attachment to the email | ||
Adds an data attachment to the email | ||
|
||
## Example | ||
put_attachment(email, %Bamboo.Attachment{}) | ||
|
||
Requires the fields filename and data of the %Bamboo.Attachment{} struct to be set. | ||
|
||
## Example | ||
def create(conn, params) do | ||
#... | ||
|> put_attachment(%Bamboo.Attachment{filname: "event.ics", data: "BEGIN:VCALENDAR..."}) | ||
#... | ||
end | ||
""" | ||
def put_attachment(%__MODULE__{attachments: attachments} = email, %Attachment{filename: _filename, data: _data} = attachment) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you can shorten the pattern match of the attach meant to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You actually can. Then the values are set to iex(1)> %Bamboo.Attachment{}
%Bamboo.Attachment{content_type: nil, data: nil, filename: nil, path: nil} But you're right - since we don't check the actual value it doesn't make sense for that check. But it would only make sense to put pass an Attachment struct with both keys ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we have 2 functions. One for the happy path and another for attachments that are missing data and filename def put_attachment(_email, %Attachment{filename: nil, data: nil} = attachment) do
raise "You must pass a valid attachment, instead got: #{inspect attachment}"
end
def put_attachment(%__MODULE__{attachments: attachments} = email, %Attachment{} = attachment) do
# happy path!
end I think this would cover the common error case. We can add different error cases later if people run into things often. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paulcsmith I've made the error messages more specific since it would still jump into the happy path if one of the params is |
||
%{email | attachments: [attachment | attachments]} | ||
end | ||
|
||
@doc ~S""" | ||
Adds an file attachment to the email | ||
|
||
## Example | ||
put_attachment(email, path, opts \\ []) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!