- Best Practices
Ah, the “users” table.
One could argue that it is one of the most important assets of a start-up. The cynics out there can’t believe that Instagram (a photo sharing app!) was bought for $715 million by Facebook or that Mailbox, a mobile email client for Gmail (and still in beta in many respects), was rumored to be bought for up to $100 million by Dropbox.
This cynical view often results from a myopic focus on just the product or the technology, which makes it easy to dismiss these valuations as absurd. However, what also needs to be considered is the value of all of those users and the growth of that user base. These astronomical valuations are always associated with a product that has a large and fast growing user base. It stands to reason, then, that keeping your user table up to date with correct contact information is a good investment for your business. Of course, the hard part is creating that growth…so you’ll have to pay for that information.
This advice on the “easy part” is free, though. In this post I’ll talk about a good methodology for keeping your users’ email up to date in an age when many people have multiple email addresses and throw-away email addresses for many of their services.
When you send email to an email address that no longer exists, the receiving party (e.g. Gmail or Yahoo) will bounce the email back to you. If you listen to these bounces, your app can react appropriately, creating a great user experience while keeping your customer database up-to-date. This blog outlines a sample application built on Heroku that uses Mailgun to listen to these bounced emails so that users with bad email addresses can continue to receive monthly invoice emails. The same logic could be applied to any of your emails, however, making sure that your users stay engaged and up-to-date with your company via email.
I’ve created a demo site for this article and made the source code available on Github. This demo walks a novice through the entire process of setting up this functionality. Click here if you want to skip directly to integrating bounce web hooks into your app.
To send monthly invoices to your users, you’ll need to write code to:
Deploy customized invoice emails to your hundreds, or thousands or millions of customers.
Actually schedule the deployment of these emails.
Let’s look at each of these in turn.
Deploying customized invoice emails
For the purposes of this tutorial, I’ll assume you already have a Mailgun account. From there, you’ll want to send your customer list to Mailgun, along with any variables that you want dynamically inserted into the emails as a JSON file and the HTML or text body of the email. Here is the code.
Lets look at what we did.
In the sendbatchmessages method, we use Recipient Variables so that we can define variables to customize each email. You’ll notice that we are inserting the user’s first name in the email. You could compose an invoice email that includes details like account number, invoice account, or anything else you like, but I’m keeping things simple.
Sending out automatic monthly billing emails with Heroku Scheduler
Once we have our method for creating and deploying emails via Mailgun, we need to write some code that automatically triggers this method. I’ve chosen to deploy this app on Heroku and am using Heroku Scheduler because it does the job, is a free add-on, and is easy to integrate.
Heroku Scheduler will run a specified Rake task at a specified time. So here we need to build a Rake task to send billing emails using Mailgun in lib/tasks/scheduler.rake.
First, when we send billing invoices to users, we need to find the email addresses of the customer we need to invoice.
In this sample app’s very simple database schema, we have a :locked attribute that is set to True when an email has bounced previously (more on that below). So our query simply pulls all our users where this field is False.
To actually send the email, I’m using Mailgun Batch Sending. Because the maximum number of recipients allowed per API call is 1,000, we use findinbatches to send billing emails to Mailgun in batches of 1000 recipients.
desc "This task is called by the Heroku scheduler add-on"
task :send_billing_info => :environment do
if DateTime.now.mday == 1
User.where(locked: false).find_in_batches(batch_size: 1000) do |group|
Beacuse Heroku Scheduler “FREQUENCY” setting has only three selectors— “daily, hourly, every 10 mins”— we need to check whether the current day is the first day of the month using
if DateTime.now.mday == 1. If it is, the email is deployed. If not, no email.
When an email is bounced, Mailgun will send a POST via the bounce webhook to our app. You need to set the callback url under “Bounces” in the Mailgun Control Panel. In this demo, my callback url is
When the app receives the bounce notification, the app will find the email address in the customer database, and set the user status to locked, so the app will not send billing email to the user until the user update his or her email address.
class Api::BouncedMailsController < ApplicationController
if verify(ENV["mailgun_api_key"], params[:token], params[:timestamp], params[:signature])
user = User.find_by_email(params[:recipient])
if user && params[:event] == "bounced"
def verify(api_key, token, timestamp, signature)
signature == OpenSSL::HMAC.hexdigest(
'%s%s' % [timestamp, token])
The user.lock! is to set user status to locked, so our Rake Task will never send emails to this user. This method is set in User model.
class User < ActiveRecord::Base
self.locked = true
The verify method is to verify the webhook is originating from Mailgun, otherwise any others could send a fake POST to lock the users in your app.
For more details on configuring web hooks you can read the Mailgun documentation – Events/Webhooks.
In the end, we return a 200 header to tell Mailgun that this interaction is successful, otherwise Mailgun will think our server is down and will keep trying to call our webhook.
Once the app knows which users have a bad email address, we need to display a flash screen to the user upon their next login with a prompt to verify their contact information on file.
class SessionsController < ApplicationController
redirect_to home_path if current_user
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
flash[:error] = "Your Email Address is invalid, please update it."
flash[:error] = "Incorrect email or password. Please try again."
session[:user_id] = nil
Once they’ve updated their email address, we set the locked attribute back to false so that they will continue to receive emails.
That’s it. Now, when your users email’s bounce, you can prompt them to update their information, providing a great user experience with the added benefit of keeping your customer data fresh.
Last updated on August 30, 2019