Gatling Gun

Gatling Gun is a library that helps you blast out a group of emails. It's a simple wrapper over SendGrid's Newsletter API.

Install

If you want to use this code in a Rails application, just add it to your Gemfile:

gem "gatling_gun"

To use the code in a non-Rails application, first install the gem:

gem install gatling_gun

Then require the library in your code:

require "gatling_gun"

Either way, you will want to setup Gatling Gun as your application loads. I recommend just setting a constant you can then refer to throughout your application. In Rails, I would put the following code in config/initializers/gatling_gun.rb. Setup is easy, just add you SendGrid credentials:

SendGrid = GatlingGun.new("USERNAME", "PASSWORD")

Usage

The minimal steps to create and send a newsletter would be something like the following. First, you need an identity to send from:

SendGrid.add_identity( "test", name:    "Test User",
                               email:   "[email protected]",
                               address: "513 Cinnamon Dr.",
                               city:    "Edmond",
                               state:   "OK",
                               zip:     "73003",
                               country: "USA" )

Then you can create a newsletter using that identity:

SendGrid.add_newsletter( "episode1", identity: "test",
                                     subject:  "Episode 1",
                                     text:     "The Text Body",
                                     html:     "<h1>The HTML Body</h1>" )

Next you need to create a list of recipients, add some emails to it, and attach that list to the newsletter:

SendGrid.add_list("subscribers")
SendGrid.add_emails( "subscribers", [ { name:  "James Edward Gray II",
                                        email: "[email protected]" },
                                      { name:  "Admin",
                                        email: "[email protected]" } ] )
SendGrid.add_recipients("episode1", "subscribers")

Of course, you could reuse identities and recipient lists for future newsletter messages without needing to recreate them.

Finally, you can schedule when to have the message sent:

SendGrid.add_schedule("episode1", at: Time.now + 10 * 60)

That would sent it 10 minutes from now, but you can adjust the time to whenever you desire or even leave it out to send now.