How userfox sends & tracks emails in their Ruby app using the Mailgun API

Written by Mailgun Team

Categories: Customer Success

3 minute read time

Today’s post is written by Peter Clark, CEO of userfox, an awesome new startup that is changing the way businesses communicate with their customers via email. They just launched a few weeks ago as you may have seen on Hacker News. In today’s post, Peter discusses what userfox does, how they’ve architected their app and a little bit about how they are using Mailgun, including the code to do it! Enjoy.

If you’d like your company tobe featured on the Mailgun blog email michael [at]

Hi, I’m Peter, CEO of userfox. userfox helps companies send better, more effective, welcome emails. Today, most businesses welcome new customers with a singular “welcome to my product!” email.

The problem with that is people are busy. You forget that you signed up for services, you only remember services you need or love, and that generally doesn’t happen the second you register.

So userfox sends a sequence of emails: when you register, a few days later, a week later, later in the month, and so on. The industry calls this a “drip” sequence. These emails contain cool tips and tricks about your product, anecdotes about who your customers are and why they love you – and so on.

We do this really simply. In fact, its much faster to integrate userfox with your application than to send your one crappy welcome email. It’s a no brainer, right? simply paste a singular snippet of javascript into your footer – and voila.

How we architected our app

Framework: Ruby on Rails

Email automation & infrastructure: Mailgun

Database: MongoHQ for MongoDB

Testing: Citrus

How we use Mailgun

We’re an email marketing company. We’re not an infrastructure company, so we rely on Mailgun to do stuff that otherwise we’d have to build. Mailgun delivers all of our customers’ emails. The Analytics API of Mailgun powers the userfox analytics. Why should we re-create the wheel with this stuff, when Mailgun does such a rad job? Besides, have you tried managing an email server? Mailgun does God’s work….

Our basic implementation using Mailgun looks like this. We build the message data together:

data = {:to => email, :from => get_from(trigger), :subject => get_subject(info, email,  
trigger), :text => "#{text}", :html => "#{html}", "h:X-Mailgun-Variables" => "{"id": "" + id + ""}"}

And then queue it into a job via a REST post. Mailgun allows sending via SMTP but we prefer the flexibility and speed that comes with using the API. Our post looks like this. Userfox::Application.config.mailgun_endpoint + '/' + domain + '/messages', data, :Authorization => Userfox::Application.config.mailgun_auth {  
      |response, request, result|
      return response

Mailgun is really powerful when it comes to sending messages. But we also run all our analytics off of Mailgun. When a message is sent we want to know that it was delivered and what happened to it. Was it opened, clicked, reported as spam? Did the user unsubscribe (of course, this would never happen)? All this data can be sent to your app with webhooks. For instance, if you wanted to post to your database when a user clicks on a link you could do this:

require 'openssl'  
def verify(api_key, token, timestamp, signature)  
  return signature == OpenSSL::HMAC.hexdigest(
                          '%s%s' % [timestamp, token])

class PostsController < ApplicationController  
  def api_key

  # GET /posts
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb

  # POST /posts
  def create
    if verify(api_key, params[:token], params[:timestamp], params[:signature])
      user, event = params[:recipient], params[:event]
      if event = 'clicked'
        link = params[:url]
        @post = => "Name", :title => "Title",
                         :content => "#{user} clicked on #{link}").save
    render :text => "OK"

This would tell you that a user has clicked on an email and give you information about who clicked it. You can do this for clicks, opens, unsubscribes, spam complaints, bounces, failures, and deliveries.

If you’ve got a business that needs to communicate with customers, give userfox a try. We’ve done the hard work for you.

Modified on: March 13, 2019

Stay up-to-date with our blog & new email resources

We'll let you know when we add new email resources and blog posts. We promise not to spam you.