From df12ca85faf99838f85096c9b6201a18a78594c0 Mon Sep 17 00:00:00 2001 From: Rachael Fleischmann Date: Thu, 28 Sep 2017 16:02:54 -0700 Subject: [PATCH 1/2] Added patches to sendgrid_api to allow for single part emails --- .gitignore | 1 + lib/sendgrid_api/delivery_methods/sendgrid.rb | 27 ++++++++++++++++--- lib/sendgrid_api/mail_part.rb | 8 ++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 lib/sendgrid_api/mail_part.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/lib/sendgrid_api/delivery_methods/sendgrid.rb b/lib/sendgrid_api/delivery_methods/sendgrid.rb index 7383c56..92ba2d5 100644 --- a/lib/sendgrid_api/delivery_methods/sendgrid.rb +++ b/lib/sendgrid_api/delivery_methods/sendgrid.rb @@ -1,5 +1,6 @@ begin require 'mail/check_delivery_params' + require 'sendgrid_api/mail_part' rescue LoadError end @@ -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 @@ -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), @@ -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 @@ -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 diff --git a/lib/sendgrid_api/mail_part.rb b/lib/sendgrid_api/mail_part.rb new file mode 100644 index 0000000..1496fa0 --- /dev/null +++ b/lib/sendgrid_api/mail_part.rb @@ -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 \ No newline at end of file From 7cfb85e058aad23456daeb63fb8cd2981450f9e3 Mon Sep 17 00:00:00 2001 From: Rachael Fleischmann Date: Fri, 29 Sep 2017 09:30:41 -0700 Subject: [PATCH 2/2] update version to 2.0.0 --- lib/sendgrid_api/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sendgrid_api/version.rb b/lib/sendgrid_api/version.rb index cbf6e70..3bc4da2 100644 --- a/lib/sendgrid_api/version.rb +++ b/lib/sendgrid_api/version.rb @@ -1,3 +1,3 @@ module SendgridApi - VERSION = "1.2.0" + VERSION = "2.0.0" end