SparkPost – Control engagement tracking with ActionMailer

In a recent post, I’ve shown how to configure action mailer to send emails through SparkPost. Today, I have faced a situation where it was needed to enable/disable tracking on a specific email. It’s to be noted that, you can globally enable or disable engagement tracking in SMTP. However, if you want to enable tracking (when globally disabled) or disabled tracking (when globally enabled) for a specific email, keep reading.


I assume, you’ve created separate mailer for each type of emails (or for at least the mails you want different tracking behavior for). Follow official doc if you’re not sure what that means or how to do it. Now, let’s configure a mailer to enable engagement tracking. For that I’ve created a HelloMailer.


In the above snippet, we’ve configured welcome email of HelloMailer, hello_mailer.rb, to enable engagement tracking. So links in your template will be automatically converted to trackable links.


  • If email is HTML (text/html), only links will be converted to a tracking URL. It’ll not convert any URLs (because you can use it for, like, anchor text and it won’t make sense to convert)
  • If email is text (text/plain), all URLs in the email will be converted to a tracking URL.
    (See the template, hello.html.erb, to see which URLs will be converted)

If you want to disable engagement tracking in an email, just set the values to false.

headers 'X-MSYS-API': { "options": { "open_tracking": false, "click_tracking": false } }.to_json

It’s that simple :).

Integrate SparkPost in your Rails app

SparkPost (SP) is an advanced email delivery service. For a number of reasons including reputation based sending, high deliverability, scalability etc. SP is trusted by big senders like facebook, twitter, linkedin, pinterest, paypal and you name it.

While SP is suitable for most, SPE (Sparkpost Elite) is for senders who wants to run these services on dedicated boxes. SPE also comes with white gloves support.

Integrating SP with your current application is extremely easy. It’s because:

  • You can start FREE :). Its free plan comes with 10000 100,000 emails per month.
  • Unlike others, there is no manual activation required. You can just open account, verify your domain and start sending immediately.
  • Because SP supports both SMTP and API, integrating with your app is just simple.

If you’re still not sure how to integrate SP in your Rails app, keep reading.

Step 1: Open SP account

  • Visit signup page and complete the form. It’s a very tiny form which takes less a than a minute completeScreenshot 2015-10-13 11.31.50Upon signup, you need to verify your email address. Unless you verify your email, you can’t add sending domain that we’ll do in next step.

Step 2: Add and verify your sending domain

Before we add it, you might ask what is sending domain and why you need this for. Here is the answer from official doc.

A sending domain is a domain that is used to indicate who an email is from via the “From:” header. Using a custom sending domain enables you to control what recipients see as the From value in their email clients. DNS records can be configured for a sending domain, which allows recipient mail servers to authenticate your messages.

Screenshot 2015-10-13 12.24.47

Go to Account -> Sending Domains to add a sending domain (you can leave Linked Tracking Domains option as it is for now). Once you’ve added the domain, you need to verify the domain by adding SPF records. You can also follow the official doc for instructions to verify domain.

Step 3: Generate API Key

SP is really simple in terms of this. If you create an API key with “Send via SMTP”, you can use this same key for SMTP authentication. So simply create an API key from Account -> API & SMTP and select Send via SMTP from API Permission.

Screenshot 2015-10-13 12.36.31

If you’re still not sure, follow this doc for detail instruction.

Step 4: Integrate with Rails

So you’ve done all the required ground jobs. The last part is configuring Rails to use your fresh SP credentials. This part is very simple. All you need is to put the SMTP configuration, ideally, in an initializer script. You can simply copy the following gist in a file inside your Rails app’s initializer directory (config/initializers).

Be sure to replace password and from with appropriate values. Password is the API key that you’ve created with “Send via SMTP” permission in previous step.

Now What?

That’s all. You application now has all the goodness of world’s best email sending platform. Start sending now!  You’ve earned it to yourself :).

Definitely you can separate your development and production configuration and/or move this config to environment variables. Also, you can configure your development environment to raise mail sending exceptions. Set

config.action_mailer.raise_delivery_errors = true 

in config/environments/development.rb.


[disclaimer: I work for SparkPost with awesome people :)]

Prevent “whenever” creating separate cron job on each deployment

I am using whenever gem for handling cron job in a Ruby on Rails application. It’s fantastic because of it’s very easy DSL to create tasks and schedule it. It will do all the dirty works to update crontab and run your task periodically as per your definition.

However, I ran into one problem that upon each deploy, whenever was creating a separate item in crontab. So when I’ve tried to check cron jobs (crontab -e), I see there are so many tasks.

It was doing this because I didn’t have :application param set in my capistrano settings (deploy.rb). Whenever depends on this name to match the existing names and update it. So all you need is to set application to your deploy.rb file.

set :application, 'your-fancy-app'

That should solve the problem. However, you need to manually cleanup current cronjob as it won’t update the current mess.

SSH Tunneling for Ruby on Rails app

It is quite annoying to develop and test an app which is likely to receive data/notification from a third party site if your development computer isn’t globally reachable. You have to deploy to a server and test it and do this each time until it completely works. Examples of such cases include:

  • Facebook app development
  • PayPal or any other payment gateway integration

While there are probably several options, SSH Tunneling is the simplest, at least to me. It needs less than 2 minutes to configure. Let’s do it.

I’m assuming:

  • You’ve a dedicated server/VPS with root access or somehow access to sshd_config file (we’re going to call this remote server)
  • Your domain is and which points to the above server with IP
  • You wanted the remote server to listen to 80 port
  • You’re running your rails server at port 3000 in your development computer (we gonna call this local server)

(You may have to change the values marked in italic font as your own settings)
Continue reading →

Development/Staging server on Heroku

Deploying with heroku is as easy as just pushing your master branch. However, this does not work for me as I manage different branch for development and staging. If I push these branches to heroku (different instance), it won’t work. It is because Heroku considers only master branch for deployment.

The work-around is very easy with the power of git. All you need to push your WHAT-EVER local branch to master on git. Please make sure you’re not doing this to your production instance.

I’ve created another instance that I will use solely for development test. My local development branch of git is named ‘develop’. So this is what I need to run

git push heroku-dev develop:master

Here, heroku-dev is the name of my git remote server for testing. I’m pushing my local develop branch to remote’s master branch. Now, heroku will respect your wish :).

Rails 4 “Routing Concerns”

Today, while digging routing guides of Ruby on Rails, I spot this new feature which is available in Ruby on Rails 4. Routing Concern is a wonderful concept of re-using routing definitions. I just love how Rails try to reckon on DRY principle. I’ve worked in a project where routes.rb has about 50 lines of definition while the project was progress not more than 20%. There were many similar routing definitions in different scopes (namespaces). Routing Concerns will now enable us write less codes to define application routes.

I’m not going to show how to use it as Rails guide already did the job. I’m simply copying the codes over here.


I’ve also found this blog shared on facebook by one of my friend

Passenger – Run Rails app in development mode

When you run Rails app using Phusion Passenger (modrails), by default, it runs in ‘production’ mode. However, if you want to run it in ‘development’ mode for any reason, you need to add a directive in your apache/nginx configuration.


RailsEnv development

Continue reading →

Handlebars helpers

In an application we are using Handlebars templating. However, we needed many custom helpers to do what we wanted to achieve. Here are some of those helpers. It may help someone who is looking for achieving similar things. Examples are included.


Character limit with trailing characters (optional).

Amistad – Facebook like friendship management in Ruby on Rails

Today I’ve decided to write something about the Ruby Gems that I used. So I’ve started this series (Ruby Gems). This is the first post of the series. I will try to write regularly.

When I was working on my pet project MovieMates, I needed to implement a friend relationship. Initially, I was going to do it myself. However, later I thought I should, at least, google a bit to check if there is already some similar solutions. Then I found Amistad.

The current version of Amistad supports ActiveRecord 3.0.x, Mongoid 3.0.x and MongoMapper 0.12.0.


Add following line to your Gemfile

gem 'amistad'

Continue reading →

Replace Rails confirm dialog with Bootboxjs

Today, I wanted to use Bootboxjs wonderful alert, confirm box in a project. Though it’s pretty easy to use separately, I, later, wanted to replace Rails default confirm dialog box with the same of bootboxjs. Rails default confirmation box is shown by its built in unobtrusive UJS library. At first, lets see how that works.

link_to 'Destroy', @post, :confirm => 'Are you sure?',:method => :delete, :class => 'btn btn-mini btn-danger'

This will generate markups that looks like:

<a class="btn btn-mini btn-danger" href="/posts/999" rel="nofollow" data-confirm="Are you sure?" data-method="delete">Destroy</a>

Now, when you click the Destroy link, it will first ask you with message “Are you sure?”. If you press “Ok” only then it will proceed. So, you get this confirmation box free :).

However, we are seeing this same confirm dialog for years after years and it’s too boring. I wanted to replace this using bootboxjs sexy dialog boxes that is based on Twitter Bootstrap. I’m already using twitter bootstrap for my project so I’m actually not adding that much overhead. You can include it easily using Twitter Bootstrap Rails gem.

When I wanted to override Rails confirm box, I found the a Gist that worked nicely out of the box.

Continue reading →