Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
27 changes: 23 additions & 4 deletions lib/sendgrid_api/delivery_methods/sendgrid.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
begin
require 'mail/check_delivery_params'
require 'sendgrid_api/mail_part'
rescue LoadError
end

Expand All @@ -19,6 +20,8 @@ def initialize(options={})
end

def deliver!(mail)
text_body = nil
html_body = nil
check_delivery_params(mail)

# Extract the recipients, allows array of strings, array of addresses or comma separated string
Expand All @@ -43,6 +46,14 @@ def deliver!(mail)
xsmtp = parse_xsmtpapi_headers(mail)
@mailer = SendgridApi::Mail.new(@client, xsmtp: xsmtp)

if(check_content_type('text', mail))
text_body = mail.body
end

if(check_content_type('html', mail))
html_body = mail.body
end

# Pass everything through, .queue will remove nils
result = @mailer.queue(
to: to.collect(&:address),
Expand All @@ -51,13 +62,21 @@ def deliver!(mail)
fromname: from.display_name,
bcc: bcc,
subject: mail.subject,
text: mail.text_part.to_s.length > 0 && mail.text_part.body,
html: mail.html_part.to_s.length > 0 && mail.html_part.body,
text: (mail.text_part.to_s.length > 0 && mail.text_part.body) || text_body,
html: (mail.html_part.to_s.length > 0 && mail.html_part.body) || html_body,
headers: header_to_hash(mail).to_json
)
raise SendgridApi::Error::DeliveryError.new(result.message) if result.error?
return result
end
end

def check_content_type(value, mail)
if(mail.content_type.include?(value))
true
else
false
end
end

# Simple check of required Mail params if superclass doesn't exist from 2.5.0
# @param [Mail] mail
Expand All @@ -67,7 +86,7 @@ def check_delivery_params(mail)
super
else
blank = proc { |t| t.nil? || t.empty? }
if [mail.from, mail.to].any?(&blank) || [mail.html_part, mail.text_part].all?(&blank)
if [mail.from, mail.to].any?(&blank) || [mail.text_part, mail.html_part, mail.body].all?(&blank)
raise ArgumentError.new("Missing required mail part")
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/sendgrid_api/mail_part.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#add empty to the Mail::Part object so sendgrid_api gem doesn't fail when trying to check html_part and text_part
module Mail
class Part
def empty?
self.to_s.empty?
end
end
end
2 changes: 1 addition & 1 deletion lib/sendgrid_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SendgridApi
VERSION = "1.2.0"
VERSION = "2.0.0"
end