Multiple email attachments in a single SendGrid email

Hello,

What is the best way to dynamically add multiple attachments to a SendGrid email?

Email.build()
|> Email.add_to(to)
|> Email.put_from(from)
|> Email.put_subject(subject)
|> Email.put_text(emailbody)
|> Email.add_attachment(%{content: image_data, filename: filename.png})
|> SendGrid.Mail.send()

Hi Jerry!
You have to call Email.add_attachment/1 multiple times on email structure.
Perhaps you’re looking for this kind of helper:

def add_attachments(email, files) do
  files
  |> Enum.reduce(email, &Email.add_attachment(&2, &1))
end

...
|> add_attachments([
  %{content: content1, filename: "filename1.png"},
  %{content: content2, filename: "filename2.png"}
])
...
1 Like

Many thanks, @fuelen.
It worked.

Regards,

Jerry