SendgridWebhook help you to generate the templates you need when you want to track the state of you email in your app. You could assign the controller name you want to receive the sendgrid webhook and model name to track the email.
Add this line to your application's Gemfile:
gem 'sendgrid'
gem 'sendgrid_webhook'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sendgrid_webhook
$ rails generate sendgrid_webhook
- Generate templates that controller you need to receive sendgrid webhook and model you want to track the email state. The default controller name is Webhook and model name is EmailHistroy. You assgin the name you want or skip it. For example
rails generate sendgrid_webhook hook EmailLog
class MessageMailer < ActionMailer::Base
include SendGrid
def messaging(history_id)
email_history = EmailHistory.find(history_id)
@body = email_history.body
@school = email_history.school
sendgrid_unique_args :email_history_id => email_history.id, :env => Rails.env
mail(:from => email_history.from, :to => email_history.to, :subject => email_history.subject) do |format|
format.text { render :layout => false }
end
end
end- Then Include the SendGrid in your Mailer, and setup the unique_args that you need to track the email. For example, the email_histroy_id
# webhook_controller.rb
def email
params["_json"].group_by{|rsp| rsp["email_histroy_id"]}.each do |email_histroy_id, rsp_hash|
last_email_rsp = rsp_hash.sort_by{|h| h["timestamp"]}.last
if last_email_rsp["env"] == Rails.env
EmailHistroy.find(email_histroy_id).update_attributes(:status => last_email_rsp["event"])
end
end
render :nothing => true
endThen handle the webhook whatever you want, it is the default template.
Other thing you may need to know.
-
The sendgrid don't have their message id when you sending the mail, so you need to create your own (In this example is
sendgrid_unique_args :email_history_id => email_history.id) through X-SMTPAPI insert the unique argument, you could know more from Unique Arguments -
Sendgrid account only could bind one webhook url, so if you have several applications or environments, you may need to use their new service reflector.io that will broadcast your webhook. Then you could check the environment or application through the params you insert in the header(In this example is
sendgrid_unique_args :env => Rails.env)
- Fork it ( http://github.com//sendgrid_webhook/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request