Skip to content

Commit e364b4f

Browse files
authored
Update to latest Carbon. Added support for attachments. Ref #13 (#18)
1 parent 53c2536 commit e364b4f

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license: MIT
1111
dependencies:
1212
carbon:
1313
github: luckyframework/carbon
14-
version: ~> 0.4.0
14+
version: ~> 0.5.1
1515
habitat:
1616
github: luckyframework/habitat
1717
version: ~> 0.4.8

spec/carbon_sendgrid_adapter_spec.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ describe Carbon::SendGridAdapter do
154154

155155
params["personalizations"].as(Array).first.has_key?("asm").should eq(false)
156156
end
157+
158+
it "handles attachments" do
159+
email = FakeEmail.new(text_body: "0")
160+
params = Carbon::SendGridAdapter::Email.new(email, api_key: "fake_key").params
161+
attachments = params["attachments"].as(Array)
162+
attachments.size.should eq(1)
163+
attachments.first["filename"].should eq("contract.pdf")
164+
Base64.decode_string(attachments.first["content"].to_s).should eq("Sign here")
165+
end
157166
end
158167
end
159168

spec/support/fake_email.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ 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
2130
end

src/carbon_sendgrid_adapter.cr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "http"
22
require "json"
33
require "carbon"
4+
require "base64"
45
require "./errors"
56
require "./carbon_sendgrid_extensions"
67

@@ -35,7 +36,6 @@ class Carbon::SendGridAdapter < Carbon::Adapter
3536
end
3637

3738
# :nodoc:
38-
# Used only for testing
3939
def params
4040
data = {
4141
"personalizations" => [personalizations],
@@ -45,6 +45,7 @@ class Carbon::SendGridAdapter < Carbon::Adapter
4545
"reply_to" => reply_to_params,
4646
"asm" => {"group_id" => 0, "groups_to_display" => [] of Int32},
4747
"mail_settings" => {sandbox_mode: {enable: sandbox?}},
48+
"attachments" => attachments,
4849
}.compact
4950

5051
if asm_data = email.asm
@@ -138,6 +139,19 @@ class Carbon::SendGridAdapter < Carbon::Adapter
138139
].compact
139140
end
140141

142+
private def attachments : Array(Hash(String, String))
143+
files = [] of Hash(String, String)
144+
email.attachments.map do |attachment|
145+
case attachment
146+
in AttachFile, ResourceFile
147+
files.push({"content" => Base64.encode(File.read(attachment[:file_path])), "type" => attachment[:mime_type].to_s, "filename" => attachment[:file_name].to_s, "disposition" => "attachment"})
148+
in AttachIO, ResourceIO
149+
files.push({"content" => Base64.encode(attachment[:io].to_s), "type" => attachment[:mime_type].to_s, "filename" => attachment[:file_name].to_s, "disposition" => "attachment"})
150+
end
151+
end
152+
files
153+
end
154+
141155
private def text_content : Hash(String, String)?
142156
body = email.text_body
143157
if body && !body.empty?

0 commit comments

Comments
 (0)