Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.
Merged
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
50 changes: 50 additions & 0 deletions app/lib/send_grid_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,56 @@ 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],
"substitutions" => {
"-user_count-" => options[:user_count].to_s
},
"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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reraise the error here so we know if it fails?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rickr - Done 419f7f8

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we just need a solution for this:

@rickr - I was having issues with the substitutions portion during testing:

"substitutions" => {
"-user_count-" => options[:user_count]
}

We will have to investigate this further.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved the user_count issue: 6d5668d

raise "Failed to send email to #{options[:to]} re: #{options[:subject]} due to: #{e}"
end

private

def add_recipient(data)
Expand Down