From 746b2833d1b8dea7fb79d3b93c2db78c6c310e55 Mon Sep 17 00:00:00 2001 From: Harry Levine Date: Mon, 17 Jul 2017 20:34:56 -0600 Subject: [PATCH 1/4] Method to send a transaction email with a template via Sendgrid I was having issues with the substitutions portion during testing: "substitutions" => { "-user_count-" => options[:user_count] } We will have to investigate this further. --- app/lib/send_grid_client.rb | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/app/lib/send_grid_client.rb b/app/lib/send_grid_client.rb index 2fcb4b62..bb6f5ee7 100644 --- a/app/lib/send_grid_client.rb +++ b/app/lib/send_grid_client.rb @@ -17,6 +17,52 @@ def add_user(user) Rails.logger.error e.backtrace.join("\n") end + # Sends a transactional email with a template, based on the passed in options hash. + # + # @param options [Hash] Sample options hash: + # { + # to: 'tom@gmail.com', + # from: 'frank@oc.com', + # subject: 'Hello', + # user_count: 5, + # body: 'Hello world!', + # template_id: 'asdfas-123123-asdfasf-2qwerqwe' + # } + # @return [SendGrid::Response] Includes a body and status_code + # @see https://github.com/sendgrid/sendgrid-ruby/blob/7c8973f7b8de51a98dce73a52138791b19434573/USE_CASES.md#with-mail-helper-class + # + def send_email_with_template(options={}) + Rails.logger.info("Sending email to #{options[:to]} at #{Time.current} re: #{options[:subject]}") + + mail = { + "personalizations" => [ + { + "to" => [ + { + "email" => options[:to] + } + ], + "subject" => options[:subject], + "template_id" => options[:transactional_template_id] + } + ], + "from" => { + "email" => options[:from] + }, + "content" => [ + { + "type" => "text/plain", + "value" => options[:body] + } + ] + } + + @sendgrid.client.mail._("send").post(request_body: mail) + rescue => e + Rails.logger.error "Failed to send email to #{options[:to]} re: #{options[:subject]} due to: #{e}" + Rails.logger.error e.backtrace.join("\n") + end + private def add_recipient(data) From 011097eab65506ffe4a6e1462f4d1714dd39bec3 Mon Sep 17 00:00:00 2001 From: Harry Levine Date: Thu, 20 Jul 2017 15:23:59 -0600 Subject: [PATCH 2/4] Re-raises the error so we know if it fails --- app/lib/send_grid_client.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/lib/send_grid_client.rb b/app/lib/send_grid_client.rb index bb6f5ee7..8bf538b7 100644 --- a/app/lib/send_grid_client.rb +++ b/app/lib/send_grid_client.rb @@ -61,6 +61,7 @@ def send_email_with_template(options={}) rescue => e Rails.logger.error "Failed to send email to #{options[:to]} re: #{options[:subject]} due to: #{e}" Rails.logger.error e.backtrace.join("\n") + raise "Failed to send email to #{options[:to]} re: #{options[:subject]} due to: #{e}" end private From 281a37744c28596b1b06ab2a2dbf2020f04342a3 Mon Sep 17 00:00:00 2001 From: Harry Levine Date: Fri, 21 Jul 2017 11:19:36 -0600 Subject: [PATCH 3/4] Adds substitution for user count The value for user_count needs to be a string, not an integer. --- app/lib/send_grid_client.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/lib/send_grid_client.rb b/app/lib/send_grid_client.rb index 8bf538b7..92f0bc8b 100644 --- a/app/lib/send_grid_client.rb +++ b/app/lib/send_grid_client.rb @@ -24,7 +24,7 @@ def add_user(user) # to: 'tom@gmail.com', # from: 'frank@oc.com', # subject: 'Hello', - # user_count: 5, + # user_count: '5', # body: 'Hello world!', # template_id: 'asdfas-123123-asdfasf-2qwerqwe' # } @@ -43,6 +43,9 @@ def send_email_with_template(options={}) } ], "subject" => options[:subject], + "substitutions" => { + "-user_count-" => options[:user_count] + }, "template_id" => options[:transactional_template_id] } ], From 4bf02cb0bdd2058daa4e42a14477ef404f304eb5 Mon Sep 17 00:00:00 2001 From: Harry Levine Date: Sat, 22 Jul 2017 08:52:54 -0600 Subject: [PATCH 4/4] =?UTF-8?q?Ensures=20compliance=20with=20Sendgrid?= =?UTF-8?q?=E2=80=99s=20API=20by=20coercing=20to=20string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lib/send_grid_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/send_grid_client.rb b/app/lib/send_grid_client.rb index 92f0bc8b..b67c67d8 100644 --- a/app/lib/send_grid_client.rb +++ b/app/lib/send_grid_client.rb @@ -44,7 +44,7 @@ def send_email_with_template(options={}) ], "subject" => options[:subject], "substitutions" => { - "-user_count-" => options[:user_count] + "-user_count-" => options[:user_count].to_s }, "template_id" => options[:transactional_template_id] }