在 @knwang 老师的指导下,最近给 Mailgun 写了一篇博客,原文在这里 - http://blog.mailgun.net/post/45366504974/building-a-data-driven-approach-to-keeping-users
无耻小请求:小菜鸟第一次写这样的技术博文,居然上了 Hacker News, 各位大大们帮忙在 HN 上投个票吧。
This blog was written by Roy Young. Roy is a student from Tealeaf Academy.
Do you remember every email address you use with every one of your online accounts? You know, the accounts you use with your spam email address, versus your "real" email, versus your work email? Probably not. And neither do your users. That is why it makes sense to periodically confirm that you have the best contact information on file for your customers. You could do this every month, or once a year, or take a data driven approach that lets you focus only on the customers that will most likely find your notices to update their contact information helpful, rather than annoying.
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.
This post is built around the following workflow:
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 a app.
To send monthly invoices to your users, you'll need to write code to:
Let's look at each of these in turn.
Deploying customized invoce emails using Mailgun
Sending emails via Mailgun simply requires an account. For the purposes of this tutorial, I'll you've already done that. 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.
class MailgunGateway
include Rails.application.routes.url_helpers
def send_batch_message(users)
RestClient.post(messaging_api_end_point,
from: "Mailgun Demo <billing@#{ENV["mailgun_domain_name"]}>",
to: users.map(&:email).join(", "),
subject: "Monthly Billing Info",
html: billing_info_text,
:"h:Reply-To" => "billing@#{ENV["mailgun_domain_name"]}",
:"recipient-variables" => recipient_variables(billing_recipients)
)
end
private
def api_key
@api_key ||= ENV["mailgun_api_key"]
end
def messaging_api_end_point
@messaging_api_end_piont ||= "https://api:#{api_key}@api.mailgun.net/v2/#{ENV["mailgun_domain_name"]}/messages"
end
def billing_recipients
@users ||= User.where(locked: false)
end
def recipient_variables(recipients)
vars = recipients.map do |recipient|
"\"#{recipient.email}\": {\"name\":\"#{recipient.fullname}\"}"
end
"{#{vars.join(', ')}}"
end
def billing_info_text
<<-EMAIL
<html><body>
Hi %recipient.name%,
<p>
Your bill for the current month is now available, please click on
<br/>
#{billing_url}
<br/>
to see details.
</p>
<p>Reply to this email directly</p>
</body></html>
EMAIL
end
end
In the send_batch_message
method, we also 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.
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/scheduer.rake
.
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|
MailgunGateway.new.send_batch_message(group)
end
end
end
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 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 shcema, 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 Maigun Batch Sending. Beacuse the maximum number of recipients allowed per API call is 1,000, we use find_in_batches
to send billing emails to Mailgun in batches of 1000 recipients.
This is what the Scheduler setting look like in Heroku:
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 "http://mailgun-demo.herokuapp.com/api/bounced_mails"
When the app receives the notification from Mailgun, 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 email address.
class Api::BouncedMailsController < ApplicationController
skip_before_filter :verify_authenticity_token
def create
if verify(ENV["mailgun_api_key"], params[:token], params[:timestamp], params[:signature])
user = User.find_by_email(params[:recipient])
if user && params[:event] == "bounced"
user.lock!
end
head(200)
end
end
private
def verify(api_key, token, timestamp, signature)
signature == OpenSSL::HMAC.hexdigest(
OpenSSL::Digest::Digest.new('sha256'),
api_key,
'%s%s' % [timestamp, token])
end
end
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
…
def lock!
self.locked = true
save(validate: false)
end
end
verity
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
def new
redirect_to home_path if current_user
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
if user.locked
flash[:error] = "Your Email Address is invalid, please update it."
redirect_to edit_user_path(current_user)
else
redirect_to home_path
end
else
flash[:error] = "Incorrect email or password. Please try again."
redirect_to sign_in_path
end
end
def destroy
session[:user_id] = nil
redirect_to root_path
end
end
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.