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 Iron Worker, 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
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:
- Clone the SendGrid Scheduler repo:
git clone https://github.com/nquinlan/sendgrid-scheduler
- Move into the repo:
- 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)
- 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" : "email@example.com",
"from" : "firstname.lastname@example.org",
"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.
Using The Worker Through Iron.io’s API
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:
Then require the Gem in your Ruby code:
Next initialize the client:
Let’s assume we’ve loaded our JSON Payload into a Ruby Hash as described above and are ready to send it:
Finally, let’s schedule the worker to run in one hour:
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:
Next require the package:
Initialize the client:
Now schedule the worker to run in one hour:
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:
Initialize the Client:
"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:
Schedule the worker to run in one hour:
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.