• Customer Success

How Userfox Sends & Tracks Emails In Their Ruby App Using The Mailgun API

Mailgun Team
5 min read

Today’s post is written by Peter Clark, CEO of userfox ( now adroll ), 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.

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.

RestClient.post 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( OpenSSL::Digest::Digest.new('sha256'), api_key, '%s%s' % [timestamp, token]) end class PostsController < ApplicationController def api_key "key-6mlhwmhnnqvvpocy17ji7qor3bihh566" end # GET /posts def index @posts = Post.all respond_to do |format| format.html # index.html.erb end end # 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 = Post.new(:name => "Name", :title => "Title", :content => "#{user} clicked on #{link}").save end end render :text => "OK" end end

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.

Last updated on September 16, 2020

  • Related posts
  • Recent posts
  • Top posts
View all

Always be in the know and grab free email resources!

No spam, ever. Only musings and writings from the Mailgun team.

By sending this form, I agree that Mailgun may contact me and process my data in accordance with its Privacy Policy.

sign up
It's easy to get started. And it's free.
See what you can accomplish with the world's best email delivery platform.
Sign up for Free