Skip to content

Commit

Permalink
Only attach the attachments when there's attachments to attach. (#21)
Browse files Browse the repository at this point in the history
* Only attach the attachments when there's attachments to attach. Fixes #20

* Fixing compilation error. Ensure we can build the email without attachments
  • Loading branch information
jwoertink authored Apr 26, 2024
1 parent 5ebf576 commit 13ea926
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion spec/carbon_sendgrid_adapter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe Carbon::SendGridAdapter do
end

it "handles attachments" do
email = FakeEmail.new(text_body: "0")
email = FakeEmailWithAttachments.new(text_body: "0")
params = Carbon::SendGridAdapter::Email.new(email, api_key: "fake_key").params
attachments = params["attachments"].as(Array)
attachments.size.should eq(1)
Expand Down
9 changes: 0 additions & 9 deletions spec/support/fake_email.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,4 @@ class FakeEmail < Carbon::Email
cc @cc
bcc @bcc
subject @subject
attachment contract

def contract
{
io: IO::Memory.new("Sign here"),
file_name: "contract.pdf",
mime_type: "application/pdf",
}
end
end
30 changes: 30 additions & 0 deletions spec/support/fake_email_with_attachments.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class FakeEmailWithAttachments < Carbon::Email
getter text_body, html_body

def initialize(
@from = Carbon::Address.new("from@example.com"),
@to = [] of Carbon::Address,
@cc = [] of Carbon::Address,
@bcc = [] of Carbon::Address,
@headers = {} of String => String,
@subject = "subject",
@text_body : String? = nil,
@html_body : String? = nil
)
end

from @from
to @to
cc @cc
bcc @bcc
subject @subject
attachment contract

def contract
{
io: IO::Memory.new("Sign here"),
file_name: "contract.pdf",
mime_type: "application/pdf",
}
end
end
7 changes: 6 additions & 1 deletion src/carbon_sendgrid_adapter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class Carbon::SendGridAdapter < Carbon::Adapter
"attachments" => attachments,
}.compact

# If Sendgrid sees an empty attachments array, it'll return an error
if data["attachments"].empty?
data.delete("attachments")
end

if asm_data = email.asm
data = data.merge!({"asm" => asm_data})
else
Expand Down Expand Up @@ -141,7 +146,7 @@ class Carbon::SendGridAdapter < Carbon::Adapter

private def attachments : Array(Hash(String, String))
files = [] of Hash(String, String)
email.attachments.map do |attachment|
email.attachments.each do |attachment|
case attachment
in AttachFile, ResourceFile
files.push({"content" => Base64.encode(File.read(attachment[:file_path])), "type" => attachment[:mime_type].to_s, "filename" => attachment[:file_name].to_s, "disposition" => "attachment"})
Expand Down

0 comments on commit 13ea926

Please sign in to comment.