Skip to content

Commit

Permalink
support attachments (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
manveru committed Feb 9, 2024
1 parent b97c402 commit 4185691
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license: MIT
dependencies:
carbon:
github: luckyframework/carbon
version: ">= 0.4.0"
version: ">= 0.5.0"
email:
github: arcage/crystal-email
version: ">= 0.7.0, < 0.8"
Expand Down
23 changes: 23 additions & 0 deletions spec/carbon_smtp_adapter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ class TestEmail < BaseEmail
header "Message-ID", "<abc123@myapp.com>"
header "Return-Path", "support@myapp.com"
header "Sender", "support@myapp.com"
attachment hello
attachment bye

def hello
{
io: IO::Memory.new("Hello"),
file_name: "hello.txt",
mime_type: "text/plain",
}
end

def bye
{
io: IO::Memory.new("Bye"),
cid: "unique_bar@myapp.com",
file_name: "bye.txt",
mime_type: "text/plain",
}
end
end

class NoHtmlEmail < BaseEmail
Expand All @@ -50,6 +69,10 @@ describe CarbonSmtpAdapter do
received_email.should match(/Content-Type: text\/plain/)
received_email.should match(/Content-Type: text\/html/)
received_email.should match(/X-Crystal-Version: 0\.27/)
received_email.should match(/hello\.txt/)
received_email.should match(/SGVsbG8=/)
received_email.should match(/bye\.txt/)
received_email.should match(/Qnll/)
end

it "sends with just text template" do
Expand Down
13 changes: 13 additions & 0 deletions src/carbon/adapters/smtp_adapter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ class Carbon::SmtpAdapter < Carbon::Adapter
if html = email.html_body.presence
message_html(html)
end

email.attachments.each do |attachment|
case attachment
in AttachFile
attach(file_path: attachment[:file_path], file_name: attachment[:file_name], mime_type: attachment[:mime_type])
in AttachIO
attach(io: attachment[:io], file_name: attachment[:file_name], mime_type: attachment[:mime_type])
in ResourceFile
message_resource(file_path: attachment[:file_path], cid: attachment[:cid], file_name: attachment[:file_name], mime_type: attachment[:mime_type])
in ResourceIO
message_resource(io: attachment[:io], cid: attachment[:cid], file_name: attachment[:file_name], mime_type: attachment[:mime_type])
end
end
end
end

Expand Down

0 comments on commit 4185691

Please sign in to comment.