Using Lob Templates to Send Tailored Thank You Cards in Rails

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.

Nonprofits and other organizations constantly struggle to increase their donations. Oftentimes this means improving the donation process by making it easier, faster, more transparent, or even more personal. With the tools available today, these sorts of improvements are easier than ever. In this post, we’ll be covering one such way to improve the donation process: tailored thank you cards.


In this tutorial, we’ll be building an app that attempts to improve the donation experience by sending personalized postcards upon receipt of donations. We’ll be using Stripe to take donations and Lob to automatically send tailored “thank you” postcards. Instead of one, generic postcard design, we’ll be using three that highlight the different impacts of each amount.


In this guide, we’ll be using the Ruby on Rails web framework as well as a range of APIs. Therefore, some familiarity with web development, Rails, and APIs is recommended, though don’t worry if you’re not an expert!

Key Tools

You’ll need your API keys for Lob and Stripe handy, which can be found here and here respectively.

Getting Started

Generate Project

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

Install Dependencies

Add the following lines to the end of the Gemfile:

Then, install the above dependencies:

Configure API keys

Next, we’ll want to add our API keys into our project and initialize the Stripe wrapper. We’ll use Figaro to automatically set up a config/application.yml file where we can securely store our API keys. Figaro automatically generates a .gitignore and adds the application.yml file. Run the following command:

Next, add in our API keys to config/application.yml

Next, initialize the Stripe wrapper by creating the file config/initializers/stripe.rb:

With the above configuration all set, we can begin work on our app’s functionality.

Building our Application

Set Up the Donation Model

We’ll begin by creating a donation model to handle the details of each donation:

Next, add in all the necessary fields to the migration Rails automatically generated for us. We’ll want fields to track the Stripe charge as well as donor details such as name and address:

Then run that migration:

Finally, add a send_thankyou_postcard method to the Donation model. We’ll have Rails call this function whenever a donation is created. That way, for every donation we process, we’ll automatically send out a thank you postcard. The function itself will call our DonationWorker, which we’ll implement later on.

Generate the Donation Controller and Modify Views

With our model set up, we’ll next generate the necessary controller and views to handle donation creation:

This should’ve created the following files for us:

  • app/controllers/donations_controller.rb
  • app/views/donations/create.html.erb
  • app/views/donations/new.html.erb

First, modify the new view, adding our donation form and Stripe Checkout:

With this view, we’ll have our simple donation form with Stripe Checkout functionality baked in.

Next, change the create view which will be displayed after a donation’s been accepted:

Finally, edit routes.rb by replacing the automatically generated routes with the following line:

Set Up the Donation Controller

Next, we need to set up the controller for our create view that handles the form data. We’ll use the Stripe wrapper to create a charge and save a new donation object. Our controller should contain the following:

Setting Up the Postcards

Next, we need to set up our templates and DonationWorker to actually send our postcards.

Setting up the Templates

Let’s navigate to Lob’s Dashboard to set up our postcard templates. We’ll be using three different designs corresponding with the amount each user donated. With Lob’s templates feature we can use vastly different designs without worrying about their implementation in our application code.

Let’s go ahead and create the four templates we’ll be using. We’ll have three different front designs and a single back design.

You can find the HTML for each template below:

Note: use the “Preview in Browser” button to check out how each template looks!

Now that we’ve got the templates set up on the dashboard, we need to add their ids to our project. For this, we’ll use Figaro again. While we can still edit our templates, their ids will persist across versions so we can feel safe in adding them to our configuration file.

Grab the template ids and add the following lines to the config/application.yml file. Make sure you’ve got the corresponding ids for each donation amount — we don’t want to be sending the $10 template to users who donated $50!

Generate Donation Worker

Next, we’ll set up our worker to actually send our postcards through Lob. First, we’ll tell Rails to generate a DonationWorker for us:

Then, fill in the app/workers/donation_worker.rb file with a call to the Lob Postcard API:

With the worker all set, we should be ready to get our app up and running!

Run it

Open up two tabs in your terminal in the donations-demo directory and run:

Navigate to localhost:3000/donations/new and create a donation. You should see a new charge created in your Stripe account and a new postcard on your Lob dashboard!

Wrapping Up

With that, we’ve got a functioning application that takes donations and automatically sends thank you postcards!

You can check out Lob’s documentation 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