PHP Classes

How to Integrate a Print-to-Mail PHP API into Your Software to Send PDF Statements via the Post Office - Docusend PHP package blog

Recommend this page to a friend!
  All package blogs All package blogs   Docusend PHP Docusend PHP   Blog Docusend PHP package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Integrate a Pr...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  


Viewers: 125

Last month viewers: 7

Package: Docusend PHP

REST API in PHP for printing, mailing and even emailing documents

Software developers are conscious of the importance of giving more options to users. Many individuals and companies need to deliver printed documents via postal mail to make bills, invoices, etc. available to their customers on paper. Others prefer to send these documents electronically.

DocuSend provides a cloud-based services API that addresses this need by allowing senders to generate documents to be delivered via United States Postal Service. DocuSend takes care of printing and delivering the documents to the post office. And now with the new email feature, you can send a secure PDF link by email, track unopened links, and opt to resend the document via USPS.

Read this article to learn how you can integrate sending emails and printed documents in a PHP application that implements both methods in an all-in-one document-delivery solution.

Loaded Article

Why is the delivery of printed documents by postal mail still important?

Surveys have shown that approximately 70% of U.S. households prefer to receive bills and statements via postal mail rather than as email attachments. For some types of documents, the percentage is even higher, including among those who prefer to pay their bills online.

While we would like to be able to send email for everything, spam and phishing make it a risky method for sending sensitive documents. U.S. mail is more secure and reliable, which is why such a large majority of customers prefer it.

In addition, it is a legal requirement in many states to send some types of communication through the U.S. mail, such as certain financial documents, legal notices, and change-of-service notices. Some of these may even have to be sent certified.

The fact that your customers can't always use email to save postage and time has been a nuisance in the past. The normal approach of printing, folding, stuffing, stamping, and delivering mail to a postal facility is tedious and requires a correctly configured printer and keeping an inventory of stamps, envelopes, paper, etc.

But not anymore. The DocuSend cloud service sends users’ documents directly to a U.S. Postal Service delivery center with the same ease, if not more, than sending an email. And DocuSend provides a REST API that’s easy to work with.

This article shows you how to set up a simple REST API that gives your users the option to send documents via US Mail as easily as sending an email, either with a menu option or with a "Send US Mail" button.

How to Start Sending Print Documents for Postal Mail Delivery Using PHP

To get started with the API, go to this page to create an account with DocuSend. This is a master account. All additional accounts will be associated with this one. This is important to you because DocuSend pays a royalty of 4 cents per mailed envelope to those who integrate with them.

After you have signed up, you will be given credentials to use in your integration. You will also have an engineer assigned to help you, although the process is simple enough that you probably won't need much help, if any.

You can retrieve the most up-to-date wrapper class for the REST API from GitHub. This wrapper handles how to send things to the server and get them back much easier than writing your own.

Once you have added this wrapper to your application, you have a few tasks to perform.

How to Set Up the Parameters to Access the DocuSend API

API requests should be sent to sandbox API endpoint:

Set the right parameters in lib/connection.php

$testServer = "";

$UserEmail = "";

$UserPassword = "Testing";

How to Set Up New Company Clients

If your application is used by many different companies, those companies will most likely want their own account with DocuSend. This is easy to provide.

require_once ('lib/DocusendAPI.php');
require_once ('lib/connection.php');
$api = new DocusendAPI($testServer, $UserEmail, $UserPassword);

echo "<br>Create new company<br>";
$json = printf($api->PostCompany($aParms = array(

             'CompanyName' => 'Mail Technologies Inc',

'Address1' => '10 Vantage Point Drive',

            'Address2' => 'Ste. 1',

            'City' => 'Rochester',

            'State' => 'NY',

            'ZipCode' => '14624',

            'CompanyPhone' => '877-514-3091',

            'Email' => '',

            'FullName' => 'DocuSend API',)));


if ($api->httpcode == 201) {

    $value = json_decode($json, true);

    printf("<br></br>" . $json);

    echo "Create Succesful!";

} else {

    printf("ERROR: HTTPCODE=%d %s", $api->httpcode, $json);


You will use $newLogin and $newPassword to log in as the new company to the API.

How to Upload and Send a Print Document for Delivery  

The DocuSend API allows you or your users to send documents with any of various specifications  added to the file, such as perforated paper, overlay, duplex, color, inserting a flyer, adding a remittance envelope, selecting that it is HIPAA, and even how long the document is to be kept in the portal.

require_once ('lib/DocusendAPI.php');

require_once ('lib/connection.php');

$api = new DocusendAPI($testServer, $UserEmail, $UserPassword);


$results = $api->PostJob("C:\\Testing\\Issues\\Invoice.PDF", 0, 0, 0);// in this line is where you should put the job specifications.

$aJob = json_decode($results, true);if (isset($aJob['ID']))

$jobID = $aJob['ID'];

while ($jobID) {

    $results = $api->GetJob($jobID);

    $aJob = json_decode($results, true);

    if (isset($aJob['ChargeAmount']))



echo "Job Finished " . print_r($aJob, true);

How to Pay for Documents to Be Sent via Postal Mail

require_once ('lib/DocusendAPI.php');

require_once ('lib/connection.php');

$api = new DocusendAPI($testServer, $UserEmail,


$aCreditCard = array(
    'CompanyName' => 'Interpal
    'Address1' => '141 Elm
    'Address2' => 'Suite 01',
    'City' => 'Anytown',
    'State' => 'Alabama',
    'Zipcode' => '14471',
    'Country' => 'CR',
    'Phone' => '5550407',
    'Email' =>
    'CreditCardNumber' =>
    'ExpirationMonth' => '02',
    'ExpirationYear' =>
    'CCV' => '123',
    'FullName' => 'John
    'QuickPay' => 0);
$result= $api->GetPay();
$aPay = json_decode($result, true);
if (!empty($aPay['AmountDue']) &&
$aPay['AmountDue']>0.00 ) {
    $payment = $api->PutPay("Credit
Card", $aPay['AmountDue'], $aCreditCard);


Combine Postal Mailing with Securely Sending Documents by Email

DocuSend offers a service called DocuLink that serves as a bridge between email and postal mail.

 DocuLink allows users to send via email a link to an invoice, letter or other customer document. When clicked, it will send the email recipient to an encrypted portal where they will view the document.

 When the link has been opened, it will notify the sender that the customer has opened the document. And if the link has not been opened within a stipulated number of days, which can be modified by the sender, the document can be automatically sent for USPS delivery.

How to Save Your Customers Money While You Make Money Sending Mail through DocuSend

Setting up the DocuSend REST API will make life much easier for your customers and save them at least 50% over processing their mailings manually.

With the little time it takes to implement the DocuSend API, it can make money for you too as a PHP developer by earning you 4 cents per envelope that your customers have delivered by postal mail.

There are many other features available in the DocuSend API, such as certified mail and foreign mail. If there are any you're looking for that are not listed on our website, talk to us, as we may well be able to address your users' needs.

Watch the videos on the DocuSend site so you can learn more about the benefits of these services, or visit our documentation in

You need to be a registered user or login to post a comment

Login Immediately with your account on:


No comments were submitted yet.

  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   Docusend PHP Docusend PHP   Blog Docusend PHP package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Integrate a Pr...