Schedule Email with Sendgrid and Iron.io

Sendgrid

This integration was originally written by Nick Quinlan and published on the SendGrid Blog.

There are tons of reasons to schedule an email: maybe you want to send an email daily or weekly, or perhaps, you just want to send an email in the morning rather than the evening. Luckily, with Iron.io and SendGrid this is easy.

Iron.io is a cloud platform that gives developers tools to solve many common problems. One of these tools is the IronWorker, a way to asynchronously run code in a number of languages. Workers are run by "tasks" which can be queued, scheduled in advance, or even repeated. Tasks can contain JSON Payloads which the worker can then process.


Writing the Worker

Using a worker, scheduling an email becomes a simple task. In this case, our worker is written in Node.js so that it can take advantage of SendGrid’s Node.js Library and its ability to easily process JavaScript objects. Because of this, all the worker needs to do is accept the payload and give it to our client library.


Once the code is written, we must must tell Iron what to do with it, in our .worker file (like a Procfile on Heroku). After this is done, all that’s required is that we set up our worker on Iron.


Set Up the Worker

To set up our worker, we can use some pre-written code, SendGrid Scheduler that takes all the hassle out of the process. To use it:

1. Clone the SendGrid Scheduler repo:


 git clone https://github.com/nquinlan/sendgrid-scheduler


2. Move into the repo:

 cd sendgrid-scheduler


3. Place your iron.json file in the repo: e.g.


 mv ~/Downloads/iron.json ./

(You can download your iron.json by clicking credentials then “Download iron.json” found in your project, while on the Iron.io HUD)

4. Upload the worker:


 iron_worker upload SendGridScheduler


This requires the iron_worker gem, you can get it with:


 gem install iron_worker

Now that we’ve given Iron.io our worker, all that’s left is to use it! With Iron.io, you can use workers two ways: from the web interface or via the API. Both of these can prove useful when it comes to scheduling emails.

Either way, you need to provide the worker with a JSON payload of what you want to be in the email, plus your SendGrid API Credentials. The resulting payload will look something like:


 {
  "api_user" : "YOUR_SENDGRID_USERNAME",
  "api_key" : "YOUR_SENDGRID_PASSWORD",
  "to" : "nick@sendgrid.com",
  "from" : "nick@sendgrid.com",
  "subject" : "Look at this cool email!",
  "text" : "I mean seriously, it was scheduled with Iron.io-- so cool!"
 }



Using The Worker Through The Web

To use the worker through Iron.io’s web interface or Heads Up Display (HUD), simply navigate to your project and select the Scheduled Tasks pane. In this pane, there’s a button that allows you to schedule a task.

Here, you can schedule the task to repeat or just run once in the future. Personally, I have a task scheduled to run every Monday to email me and ask about my weekend.

Sendgrid1

Using The Worker Through Iron.io’s API

Iron.io being an awesome developer services company gives you the ability to schedule workers from their API by simply querying it or using one of their many client libraries.

Using their client libraries makes scheduling an email simple. A few languages are shown below.


Using the Iron Worker with Ruby

If you want to use the Iron Worker with Ruby, you can install the Gem:


 $ gem install iron_worker_ng


Then require the Gem in your Ruby code:


 require "iron_worker_ng"


Next initialize the client:


 client = IronWorkerNG::Client.new(
  :token => 'IRON_IO_TOKEN', 
  :project_id => 'IRON_IO_PROJECT_ID'
 )


Let’s assume we’ve loaded our JSON Payload into a Ruby Hash as described above and are ready to send it:


 payload = loaded_json_payload


Finally, let’s schedule the worker to run in one hour:


 schedule = client.schedules.create('SendGridScheduler', 
  payload, {:start_at => Time.now + 3600})


A working copy of this code may be found with the SendGrid Scheduler. More instructions for using the Iron.io Ruby Gem can be found on Github.


Using the Iron Worker with Node

First install the Node package:


 $ npm install iron_worker


Next require the package:


 var iron_worker = require('iron_worker');


Initialize the client:


 var worker = new iron_worker.Client(
  {
   token: "IRON_IO_TOKEN", 
   project_id: "IRON_IO_PROJECT_ID"
  }
 );


Again, let’s assume we’ve loaded our JSON Payload into a Javascript Object as described above and are ready to send it:


 var payload = loaded_json_payload;


Now schedule the worker to run in one hour:


 worker.schedulesCreate("SendGridScheduler", 
	payload, 
	{ "start_at" : Math.floor((new Date()).getTime()/1000) + 3600 }, function (err, body) {}
 );


A working copy of this code may be found with the SendGrid Scheduler. More instructions for using the Node Package can be found on Github.


Using the Iron Worker with PHP

To install the library, follow the instructions on GitHub. Next, require the two classes:


 require_once "IronCore.class.php";
 require_once "IronWorker.class.php";


Initialize the Client:


 $worker = new IronWorker(array(
    "token" => "IRON_IO_TOKEN",
    "project_id" => "IRON_IO_PROJECT_ID"
 ));


Again, let’s assume we’ve loaded our JSON Payload into a PHP Array as described above and are ready to send it:


 $payload = $loaded_json_payload;


Schedule the worker to run in one hour:


 $worker->postScheduleAdvanced("SendGridScheduler", $payload, time() + 3600);


A working copy of this code may be found with the SendGrid Scheduler. More instructions for using the PHP Library can be found on Github.

Now you are ready to start scheduling emails using SendGrid and Iron.io in Ruby, Node or PHP. If you’re looking for ideas, see this goal reminder application and use email to achieve them.


Start Building With Iron.io Today

Try all features right away.
No credit card required.
Get Iron For Free
Want Information on Professional and Enterprise Plans? Contact Us
Signup