DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 86 posts at DZone. View Full User Profile

Implement Simple Mail Merge or Mail Merge with Regions in Cloud Using PHP

02.06.2014
| 2266 views |
  • submit to reddit
//Mail Merge using PHP REST

// Build URI to execute mail merge
$strURI = 'http://api.aspose.com/v1.1/words/Sample.docx/executeMailMerge?withRegions=true&mailMergeDataFile=Data.xml';
// Use the following URI if you want to remove EmptyParagraphs,UnusedRegions,UnusedFields and ContainingFields during mail merge
// $strURI = 'http://api.aspose.com/v1.1/words/Sample.docx/executeMailMerge?mailMergeDataFile=Data.xml&cleanup=EmptyParagraphs,UnusedRegions,UnusedFields,ContainingFields';
/**** End Section 1 ****/

/**** Section 2 ****/
$appSID  = "77****-****-****-****-80*********";
$appKey = "****************";

// Sign URI
$signedURI = Sign($strURI, $appSID, $appKey);
/**** End Section 2 ****/

/**** Section 3 ****/
$responseStream = ProcessCommand($signedURI, "POST", "", "");
/**** End Section 3 ****/

/**** Section 4 ****/
$json = json_decode($responseStream);
//build URI to download output DOC
$outputFileName = $json->Document->FileName;
/**** End Section 4 ****/

//Mail Merge using PHP SDK

/**** Section 1 ****/

// Specify product URI Product::$baseProductUri = "http://api.aspose.com/v1.1"; //sepcify App SID AsposeApp::$appSID = "xxxxxxxxxxxxxxxxxxxxxxxx"; //sepcify App Key AsposeApp::$appKey = "xxxxxxxxxxxxxxxxxxxxxxxx";

/**** End Section 1 ****/

try{

/**** Section 2 ****/
AsposeApp::$outPutLocation = getcwd() . "/Output/";   $mainDocumentFile = getcwd() . "/Input/SimpleMerge.doc";   $mainDocument = basename($mainDocumentFile);   $xml = simplexml_load_file(getcwd() . "/Input/SimpleMerge.xml");
/**** End Section 2 ****/

/**** Section 3 ****/
//upload main document   echo "Uploading main document...<br />";   $folder = new Folder();   $folder->uploadFile($mainDocumentFile, "");   echo "Main document uploaded <br />";
/**** End Section 3 ****/

/**** Section 4 ****/
echo "Executing mail merge... <br />";   //create MailMerge object   $doc = new MailMerge();   $result = $doc->executeMailMerge($mainDocument, $xml->asXML());
/**** End Section 4 ****/ }

catch (Exception $e)
  {
    throw new Exception($e->getMessage());
  }

//«TableStart:TableName»«TableEnd:TableName» regions, use the following XML structure to send in the request body.

<?xml version="1.0" encoding="utf-8"?>
<CollectionName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ItemName>
        <field1>Value1</field1>
        <field2>Value2</field2>
    </ItemName>
    <ItemName>
        <field1>Value1</field1>
        <field2>Value2</field2>
    </ItemName>
</CollectionName>
Mail merge allows you to produce document (potentially large numbers of documents) from a single template and a structured data source. The letter may be sent out to many recipients with small changes, such as a change of address or a change in the greeting line. It can also be used to generate business reports, purchase orders, receipts, catalogs, inventories, and invoices etc. Aspose.Words for Cloud Mail Merge allows you to generate documents from a template and XML in any language including .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more. You can use it with any language or platform that supports REST. (Almost all platforms and languages support REST and provide native REST clients to work with REST API). This post covers mail merge in PHP, you can check Aspose.Words for Cloud documentation for other languages. To execute mail merge, you need to upload a template to Aspose for Cloud or any supported third party storage and then send a POST request (passing XML data in the request body) to generate documents based on template and data. The following steps describe the process in detail. Aspose.Words for Cloud supports simple mail merge and mail merge with regions. If you want to insert simple or non-repeating data, for example name, address and code fields on an envelope, or to and from fields in a letter, use simple mail merge (also called mail merge without regions). If you are preparing reports, invoices and purchase orders, or similar documents, and want to insert tables, rows or repeating data, or if you want your documents to dynamically grow based on your input data, use mail merge with regions.