DEV Community

Haseeb A. Basil
Haseeb A. Basil

Posted on • Originally published at Medium on

Tutorial on Sending Postcards, Letters and Checks by Postal Mail Using LOB REST API using PHP - 2022

What is Lob?

Why use Lob?

Services

Why use Lob?

Programmer Friendly

Integration Support

Conclusion

What is Lob?

Lob is a venture backed startup company based in San Francisco, CA and has over 6000 customers including Amazon, Square, and Counsyl.

They provide a cloud based printing and mailing solution. Founded with mission to provide more affordable and simpler service than building an in-house printing solution.

Lob is built for developers to provide them with tools to build powerful applications and help scale physical mailing for their companies.

Lob’s plan is to eliminate the complexities of direct mail and provide you with a more innovative system, where you can easily track your mail, reach your customers without having to deal with hurdles of paper jams or trips to the post office.

Why use Lob?

Lob’s customer base consists of more than 6000 companies, which ranges from fortune 500 hundred companies (e.g. Amazon) to startups and individual developers. If you have Lob integrated in your system, you can focus on your customers instead of having to deal with pains of printing and mailing.

Lob is based on pay as you consume model. So, you can easily send 1 mailing or 1 million at a time.

Physical mail has higher open rates than email. You may not see it now, but more and more startups are now moving towards physical mail as their primary method of marketing.

One of the findings by Epsilon shows that “for every 1,000 existing customers receiving a direct-mail piece, 34 will respond on average. For email, the average response is 0.12%.”

Now if you are going to be adapting physical mail as your marketing method, what better way to go than Lob, a simple, rich and affordable printing/mailing solution?

Services

Lob provides a number of services, to help you grow your business and reach your customers better. The following are the main services provided by Lob.

Postcards

By using the Lob API, you can easily design a fully customizable HTML postcard template. Then you can send the customized postcards for testing the templates or for any specific actions your users take or specific events you define. Or to reactivate relapsed users.

Letters

Send letters to your users, as easily as sending an email campaign. You can generate each email on demand. You can easily customize the letter for events you define or actions your users take.

You can send the letters securely and quickly. You can use Lob API to send each document accurately by ensuring each document is for intended recipient’s eyes only.

Checks

With Lob you can deliver checks, with various customizations to support, logo, invoice information, instructions and memos, and much more to make payments as clear as possible.

Now with sending checks, security is a must have and nobody provides a more secure delivery system than Lob.

With Lob API, each check is sent with a colored background, watermarks, micro-printed borders, thermal ink that disappears when rubbed, and Magnetic Ink Character Recognition (MICR) encoding to facilitate bank processing and reduce errors.

And just as importantly, you can make sure that your checks are delivered, by tracking their progress, throughout the mailing process.

Simple Area Mail

With Lob API, you can easily target entire zip codes with postcards. This service is especially beneficial to startups, to make their presence known to future customers.

With Lob API you can easily get the zip-code based demographics to make your campaign even more specific to point of age, income as well as residential versus business split. And you can do all this without having to pay for any setup fee, you only pay for what you send.

Address Verification

You can easily validate mailing addresses instantly by using Lob API, you can also standardize the addresses to help ensure that your mail reaches the desired recipient. The API uses data directly from USPS to correct the missing information. And on top of all that it is free for all domestic addresses, and very affordable for international addresses.

Couchsurfing, a vibrant travel community uses Lob to automate everything from making sure the user provides deliverable addresses to ending address verification postcards on-demand.

Easy to use

The Lob API is incredibly easy to use, providing you with powerful and scalable mailing solution. Lob have made sending a physical mail as easy as sending an email.

You can easily track all your mails.You can target specific regions and demographics for your campaign. Lob’s API service is very similar to that of Sendgrid or Twilio. The Lob API takes all the pain out of sending mail and it can be integrated with many internal applications.

Programmer Friendly

The Lob API is extremely programmer friendly, it provides pre-built wrappers for multiple programming languages like: Java, PHP, Node, Python, and Ruby. It also provides documentation for all these wrappers, with example code.

The complete documentation is also available for raw API requests. But here we will be discussing PHP wrapper. You can easily install the PHP wrapper using composer or you can get it directly from Github.

Now, to start you must authenticate all of your requests, and to properly authenticate with the Lob API, the API key should be sent in each request. Below is an example for how to initiate the wrapper with authorization:

$apiKey = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc';
    $lob = new \Lob\Lob($apiKey);
Enter fullscreen mode Exit fullscreen mode

As you can see, it is pretty simple and easy, to use the PHP wrapper.

Errors

Below is the list of errors HTTP response code errors and what the mean:

In general, 2xx indicates success, 4xx indicate an input error, and 5xx indicates an error on Lob’s end.

Webhooks

Webhooks, is a really good feature to track event based notifications, like whenever an event is fired Lob will immediately send that event to any URL specified for that event, on the Lob dashboard.

But first you have to be sure that you are subscribed to the event in the environment (test or live).

Metadata

When sending a request to Lob’s API, you can attach metadata to it so, you can easily filter the requests out later. Below is an example for how to attach metadata to a request:

$lob->postcards()->create(array(
        'description' => 'Demo Postcard job',
        'metadata[campaign]' => 'NEWYORK2015',
        'to' => 'adr_78c304d54912c502',
        'from' => 'adr_61a0865c8c573139',
        'front' => '<html style="margin: 130px; font-size: 50;">Front HTML for {{name}}</html>',
        'back' => '<html style="margin: 130px; font-size: 50;">Back HTML</html>',
        'data[name]' => 'Harry'
    ));
Enter fullscreen mode Exit fullscreen mode

As you can see, that the above request contains metadata with key “campaign”. Now, to filter out the request with containing specific metadata you will do something like this:

$lob->postcards()->all(array(
        'metadata[campaign]' => 'NEWYORK2015',
        'limit' => 2,
        'offset' => 0
    ));
Enter fullscreen mode Exit fullscreen mode

Usage

Below is a example code to verify mailing addresses using Lob API PHP wrapper:

$lob->addresses()->verify(array(
        'address_line1' => '185 Berry Street',
        'address_city' => 'San Francisco',
        'address_state' => 'CA',
        'address_zip' => '94107'
    ));
Enter fullscreen mode Exit fullscreen mode

Below is an another example of code which shows how you can design postcards using Lob API PHP wrapper:

$lob->postcards()->create(array(
        'description' => 'Demo Postcard job',
        'to' => 'adr_78c304d54912c502',
        'from' => 'adr_61a0865c8c573139',
        'front' => '<html style="padding: 1in; font-size: 50;">Front HTML for {{name}}</html>',
        'back' => '<html style="padding: 1in; font-size: 20;">Back HTML for {{name}}</html>',
        'data[name]' => 'Harry'
    ));
Enter fullscreen mode Exit fullscreen mode

The value for array keys “to” and “from” are address IDs that you can get from the API.

You can get a list of addresses from this simple request:

$lob->addresses()->all(array(
        'limit' => 2,
        'offset' => 1
    ));
Enter fullscreen mode Exit fullscreen mode

The above mentioned request will bring all data related to addresses, including address ID. You can, use metadata in this request to get more specific results.

Lob’s API also provides some other resources for example:

$lob->countries()->all();
Enter fullscreen mode Exit fullscreen mode

By using this request, you can get the list of all countries, which you can use later to add addresses etc..

Now by looking at the above code examples you can clearly see, how easy it really is to use this API.

Track the Mail

With Lob’s API you can track your mail pieces along the way. You can easily monitor your postcards, letters, checks and documents on their way to the intended recipients.

You can keep track using tracking events or by adding unique promo codes, URLs, or phone numbers. You can use webhooks to follow your mail through the mailstream with live tracking events and PDF proofs.

Below is a breakdown of each scan event label and description:

In Transit

The mailpiece is being processed at the entry/origin facility.

In Local Area

The mailpiece is being processed at the destination facility.

Processed for Delivery

The mailpiece has been greenlit for delivery at the recipient’s nearest postal facility. The mailpiece should reach the mailbox within 1 to 2 business days of this scan event.

Re-routed

The mailpiece is re-routed due to recipient change of address, address errors, or USPS relabeling of barcode/ID tag area.

Returned to Sender

The mailpiece is being returned to sender due to barcode, ID tag area, or address errors.

Integration Support

Various wrappers are available for integrating the Lob API into your application including PHP. If you want to use another language and a wrapper is not available for that language, you can create a custom SDK easily for your app.

Lob also offers integration through its integration partners that offer various services of their own. If you have an account on their site, you can integrate those accounts with your account on Lob, using API keys.

Lob also has some partner apps that offer direct mailing solutions powered by Lob. You can get started with these on apps by going to the integration page on Lob’s site.

Conclusion

Lob provides several interesting services that are unusual because they make the bridge between the online world with services that have to be provided in the physical world like printed mailings to be delivered via via postal mail.

I hope this article is a good start for you to create very interesting applications based on services that you have not been aware that could be implemented using REST APIs.

If you liked this article, please share it with other developer colleagues. If you have questions, post a comment below.

Top comments (0)