Skip to content

Commit

Permalink
support attachments (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
manveru authored Jan 31, 2024
1 parent b3658e3 commit b47843b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/carbon/attachment.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Carbon
alias AttachFile = NamedTuple(file_path: String, file_name: String?, mime_type: String?)
alias AttachIO = NamedTuple(io: IO, file_name: String, mime_type: String?)
alias ResourceFile = NamedTuple(file_path: String, cid: String, file_name: String?, mime_type: String?)
alias ResourceIO = NamedTuple(io: IO, cid: String, file_name: String, mime_type: String?)
alias Attachment = AttachFile | AttachIO | ResourceFile | ResourceIO
end
14 changes: 13 additions & 1 deletion src/carbon/email.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract class Carbon::Email
abstract def from : Carbon::Address
abstract def to : Array(Carbon::Address)

def_equals subject, from, to, cc, bcc, headers, text_body, html_body
def_equals subject, from, to, cc, bcc, headers, text_body, html_body, attachments

# Set this value to `false` to prevent the email from
# being delivered
Expand Down Expand Up @@ -91,6 +91,18 @@ abstract class Carbon::Email
end
end

@attachments = [] of Carbon::Attachment
getter attachments

macro attachment(value)
def attachments : Array(Carbon::Attachment)
{% if @type.methods.map(&.name).includes?("attachments".id) %}
previous_def
{% end %}
@attachments << {{value}}
end
end

{% for method in [:to, :cc, :bcc] %}
macro {{ method.id }}(value)
def {{ method.id }} : Array(Carbon::Address)
Expand Down

0 comments on commit b47843b

Please sign in to comment.