Skip to content

Commit 13ea926

Browse files
authored
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
1 parent 5ebf576 commit 13ea926

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

spec/carbon_sendgrid_adapter_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe Carbon::SendGridAdapter do
156156
end
157157

158158
it "handles attachments" do
159-
email = FakeEmail.new(text_body: "0")
159+
email = FakeEmailWithAttachments.new(text_body: "0")
160160
params = Carbon::SendGridAdapter::Email.new(email, api_key: "fake_key").params
161161
attachments = params["attachments"].as(Array)
162162
attachments.size.should eq(1)

spec/support/fake_email.cr

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,4 @@ class FakeEmail < Carbon::Email
1818
cc @cc
1919
bcc @bcc
2020
subject @subject
21-
attachment contract
22-
23-
def contract
24-
{
25-
io: IO::Memory.new("Sign here"),
26-
file_name: "contract.pdf",
27-
mime_type: "application/pdf",
28-
}
29-
end
3021
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class FakeEmailWithAttachments < Carbon::Email
2+
getter text_body, html_body
3+
4+
def initialize(
5+
@from = Carbon::Address.new("[email protected]"),
6+
@to = [] of Carbon::Address,
7+
@cc = [] of Carbon::Address,
8+
@bcc = [] of Carbon::Address,
9+
@headers = {} of String => String,
10+
@subject = "subject",
11+
@text_body : String? = nil,
12+
@html_body : String? = nil
13+
)
14+
end
15+
16+
from @from
17+
to @to
18+
cc @cc
19+
bcc @bcc
20+
subject @subject
21+
attachment contract
22+
23+
def contract
24+
{
25+
io: IO::Memory.new("Sign here"),
26+
file_name: "contract.pdf",
27+
mime_type: "application/pdf",
28+
}
29+
end
30+
end

src/carbon_sendgrid_adapter.cr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class Carbon::SendGridAdapter < Carbon::Adapter
4848
"attachments" => attachments,
4949
}.compact
5050

51+
# If Sendgrid sees an empty attachments array, it'll return an error
52+
if data["attachments"].empty?
53+
data.delete("attachments")
54+
end
55+
5156
if asm_data = email.asm
5257
data = data.merge!({"asm" => asm_data})
5358
else
@@ -141,7 +146,7 @@ class Carbon::SendGridAdapter < Carbon::Adapter
141146

142147
private def attachments : Array(Hash(String, String))
143148
files = [] of Hash(String, String)
144-
email.attachments.map do |attachment|
149+
email.attachments.each do |attachment|
145150
case attachment
146151
in AttachFile, ResourceFile
147152
files.push({"content" => Base64.encode(File.read(attachment[:file_path])), "type" => attachment[:mime_type].to_s, "filename" => attachment[:file_name].to_s, "disposition" => "attachment"})

0 commit comments

Comments
 (0)