Giter Club home page Giter Club logo

special-delivery's Introduction

Code Climate Coverage Status Build Status Gem Version

Special Delivery

A Mailgun Webhook Event Manager

Special Delivery is a Rails engine that enables powerful and easy-to-use callbacks in response to Mailgun events POSTed via webhook.

Say what now? Example time:

Let's say you have an application that sends an email to notify a user that they have won the lottery. This email is obviously pretty important, so we'd like to send a message to an admin if the lottery-winner email bounces. How the heck do we do that?

Special Delivery saves a reference to each outgoing email messages to recreate state when Mailgun informs your application that a particular event occured for that message.

So by using Special Delivery when sending out a lottery-winner email, the application is then able to listen for bounce events for that particular email, and evaluate a callback with knowlege of the user record to which the email was originally sent.

Installation

You know the drill. Add the gem to your Gemfile.

gem 'special-delivery'

Use

Set Your API Key

Your mailgun API key is used to authenticate webhook requests from Mailgun, and must be accessible via MAILGUN_CONFIG[:api_key].

Routes File

Mount Special Delivery as an engine within your config/routes.rb at a path of your choosing. Make sure to then point your Mailgun webhooks to this URL.

mount SpecialDelivery::Engine => "/email_events"

Your Custom Callback Class File

Create a new class to handle callbacks initiated by Mailgun POST requests made to your app. Callbacks for the specific types of Mailgun events should be defined as methods within your class. Your class can respond to as many or as few of Mailgun's event types as you would like. Event types include bounced, clicked, complained, delivered, dropped, opened, and unsubscribed.

So if we want a callback class to manage sending a message to an admin when our lottery winner emails bounce, we would write:

class LotteryEmail::WinnerCallback < SpecialDelivery::Callback
  def bounced(user)
  	send_message_to_admin "#{user.name} did not receive their lottery winner email."
  end
end

And if we wanted to send a different message to the admin when the email was opened, we could add a method to the callback class:

def opened(user)
 send_message_to_admin "#{user.name} just opened their lottery winner email."
end

Mailer Files

Include the Mailer helper module in your mailer class (e.g. app/mailers/lottery_mailer.rb if you wanted to use Special Delivery in your LotteryMailer.

class LotteryMailer < ActionMailer::Base
  include SpecialDelivery::Mailer
  
  ...

Within your mailer's methods, pass your mail method calls into special_delivery as a block. Pass the special_delivery method call a hash containing at least your callback class (at :callback_class of course). You may optionally provide an ActiveRecord object for :callback_record that will be passed into your callback method as an argument.

def winner_email(user)
  special_delivery(
    :callback_class  => LotteryEmail::WinnerCallback,
    :callback_record => user
  ) do
  	mail(:to => user.email, :subject => 'All the Monies!')
  end
end

Code At Viget

Visit code.viget.com to see more projects from Viget.

special-delivery's People

Contributors

bhushangahire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

special-delivery's Issues

How do you pass the "special_delivery" object back base on the event?

Hi,

Thanks for creating this gem, :- )
I got a quick question on how do you pass back the delivery object back during the callback trigger from mailgun?

In this example: you pass back the user object but i would like to pass in the delivery object [table: special_delivery_deliveries] as well since it has other data's that i need.
The "special_delivery_deliveries" will give more context on what the mail is base on this user.
Thanks :- )

def opened(user)
 send_message_to_admin "#{user.name} just opened their lottery winner email."
end

to be

def opened(user, special_delivery_deliveries)
 send_message_to_admin "#{user.name} just opened their lottery winner email."

send_message_to_admin "#{special_delivery_deliveries.lottery_id} is the valid lottery winner identity."
end

Can't get my `EmailCallback::SomeMail` class to be included in execution.

I can't get my EmailCallback::MailgunEmail < SpecialDelivery::Callback class to be included in execution. My problem is I'm not clear on where and how to create and call the new class mentioned in the readme. Currently when I fire of a POST request to the mount SpecialDelivery::Engine => "/email_events" endpoint with all correct params, it is processed by SpecialDelivery::EventsController#create. An object is instantiated but not saved.

I ran the undocumented rake task (special_delivery:install:migrations) and migrated to new migration file.

I currently have:

# /lib/email_callback/mailgun_email.rb
module EmailCallback
  class MailgunEmail < SpecialDelivery::Callback
    def opened
      require 'pry'; binding.pry
    end

    def create
      require 'pry'; binding.pry
    end
  end
end

Note: I'm trying to hit the pry debugger to confirm the request is hitting the appropriate method and to see what I have to work with.

Thanks in advance for any help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.