From 13ea926ffd4621654a61c47d8559c0c1eb2794a6 Mon Sep 17 00:00:00 2001 From: Jeremy Woertink Date: Fri, 26 Apr 2024 08:21:24 -0700 Subject: [PATCH] Only attach the attachments when there's attachments to attach. (#21) * Only attach the attachments when there's attachments to attach. Fixes #20 * Fixing compilation error. Ensure we can build the email without attachments --- spec/carbon_sendgrid_adapter_spec.cr | 2 +- spec/support/fake_email.cr | 9 ------- spec/support/fake_email_with_attachments.cr | 30 +++++++++++++++++++++ src/carbon_sendgrid_adapter.cr | 7 ++++- 4 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 spec/support/fake_email_with_attachments.cr diff --git a/spec/carbon_sendgrid_adapter_spec.cr b/spec/carbon_sendgrid_adapter_spec.cr index 68a82c4..6ea5d18 100644 --- a/spec/carbon_sendgrid_adapter_spec.cr +++ b/spec/carbon_sendgrid_adapter_spec.cr @@ -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) diff --git a/spec/support/fake_email.cr b/spec/support/fake_email.cr index 7b6e7ae..fab39f0 100644 --- a/spec/support/fake_email.cr +++ b/spec/support/fake_email.cr @@ -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 diff --git a/spec/support/fake_email_with_attachments.cr b/spec/support/fake_email_with_attachments.cr new file mode 100644 index 0000000..6f59b81 --- /dev/null +++ b/spec/support/fake_email_with_attachments.cr @@ -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 diff --git a/src/carbon_sendgrid_adapter.cr b/src/carbon_sendgrid_adapter.cr index 32eacdc..9b36fd5 100644 --- a/src/carbon_sendgrid_adapter.cr +++ b/src/carbon_sendgrid_adapter.cr @@ -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 @@ -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"})