Send a Postcard Drip Campaign with Lob Scheduled Sends

NOTE: This example was created on an outdated version of the Lob API. Please see the latest Lob documentation for the most up-to-date information on integrating with Lob.

One of Lob’s key differentiators is the ability to apply digital methodologies to direct mail. It’s easy to set up drip campaigns with email, so once users sign up they will receive a series of communications to keep the lead warm. With Lob, you can use postcards the same way and enjoy even higher ROI. This tutorial will show you how to build a Rails web app to launch a postcard drip campaign.

What are we building?

This tutorial will show you how to build a website that will trigger a 3-postcard drip campaign upon signup. We’ll be sending the first postcard 5 days after sign up, the second postcard 15 days after signup, and the third postcard 30 days afer signup using Lob’s scheduled sends feature.

Key Tools

  • Ruby on Rails – Rails is a popular web application framework running on the Ruby programming language. See installrails.com for an in-depth startup guide.
  • Redis – Redis is a popular in-memory data store that we’ll be using to handle our application’s service workers. See the official quickstart guide for more info.
  • Lob Postcard API – Lob has a whole suite of RESTful APIs for printing and mailing. We will be using the Postcard API in this tutorial.

Before diving into the code, we will need to register for a Lob API key.

Visit the registration page and sign up for an account. You will be given a Test API Key which will give you full access to the API in a sandbox environment. Since you have a test key, all of Lob’s Printing APIs are free to try. When in Test mode, you can create postcards and view them in the dashboard, but nothing will actually be mailed until you use a Live API Key.

Getting Started

Generate Project

First, let’s have Rails start up our project:

Install Dependencies

Add the following lines to the Gemfile:

Then, install the above dependencies:

Set Up the User Model

We’re going to be using Devise, a popular, flexible authentication solution for Rails.

After this, we’ll create a User table and model by utilizing another one of Devise’s generators.

Now that we have Devise set up, we’ll need to add a few fields to the User table to collecting a mailing address.

/db/migrate/xxxxx_add_address_to_user.rb

We also need a model to store postcards we’ve sent to our users.

Now we can run the migrations.

Because a user will have 3 postcards send to them, we’ll want a one-to-many relationship between our users and postcards. So we’ll add the following to the User and Postcard models to define that relationship.

/models/user.rb

/models/postcard.rb

We’ll use an after_create hook to trigger a Sidekiq worker that will receive the user_id, send_date, and discount_percent and send the 3 postcards. We’ll be creating this worker later.

/models/user.rb

Create the Sign Up Page

We’ll need to modify the registrations controller and views created by Devise to accept these relevant fields upon sign up.

/controllers/registrations_controller.rb

/views/devise/registrations/new.html.erb

Generate Postcard Worker

Next, we’ll need to create the Sidekiq PostcardWorker we called in the User model earlier.

In our worker, we’re going to initialize the Lob client and lookup the user_id passed in, so we can determine where to send the postcard. We’re going to modify the postcard template found in the lob-templates GitHub repository so that we can pass in the . We’ll pass in the discount_percent as a data parameter that will display on the front and back of the postcard.

Once we successfully complete the request, we’ll store the postcard_id Lob returns along with the user_id and send_date so that we can keep track of which postcards have been sent to customers.

Note that we need to convert the date time stamp to the iso-8601 format for Lob’s send_date parameter. This allows us to set the date the postcard is sent.

/workers/postcard_worker.rb

Sanity Check

Let’s add a home page and some quick routing logic so we can see a preview of what we have so far. Running the command below, we’ll have a empty controller and view generated for us.

And we’ll make a very simple view.

/views/home/index.html.erb

We’re going to mount Sidekiq’s web interface at /sidekiq and add some authentication logic. If a user is authenticated, they’ll be able to see the home page. If not, they’ll be directed to the registration form.

/config/routes.rb

Now if we start up Rails and Sidekiq…

`

We should get something like the image below. Signing up will now trigger 3 postcard to be send out within the next 30 days.

Wrapping Up

Now you have a fully functional drip campaign triggered off a user sign up using Lob’s postcard API!

You can check out Lob’s documentation and scheduling mailings guide for more information. If you have any additional questions, don’t hesitate to leave a comment below or contact us directly. We’re always happy to help!

Get the latest post delivered right to your inbox

Featured Resources