<form>
            templateId: <input type="text" name="templateId" value="<?php 
if (isset($_REQUEST['templateId'])) {
    echo htmlspecialchars($_REQUEST['templateId']);
}
?>
"><br/>
    <input type="submit">
<input type="hidden" name="go">
</form>
<?php 
include '../infusionsoft.php';
include 'testUtils.php';
if (isset($_REQUEST['go'])) {
    $out = Infusionsoft_APIEmailService::getEmailTemplate($_REQUEST['templateId']);
    var_dump($out);
}
 if ($_GET['object'] == 'Template') {
     echo 'Exporting email templates into files.' . "<br/>";
     $page = 0;
     do {
         echo 'Fetching page ' . $page . '<br/>';
         flush();
         $object_fields = $object->getFields();
         if (in_array('Id', $object_fields)) {
             $results = Infusionsoft_DataService::queryWithOrderBy(new $class_name(), array('Id' => '%'), 'Id', true, 100, $page);
         } else {
             $results = Infusionsoft_DataService::queryWithOrderBy(new $class_name(), array($object_fields[0] => '%'), $object_fields[0], true, 100, $page);
         }
         foreach ($results as $result) {
             if ($result->PieceType == 'Email' && substr($result->PieceTitle, 0, 14) != 'An email draft' && substr($result->PieceTitle, 0, 12) != 'API Template' && $result->Categories != 'Product Fulfillment') {
                 try {
                     $template = Infusionsoft_APIEmailService::getEmailTemplate($result->Id);
                     $file_name = "../exports/" . $_GET['object'] . '_' . $result->Id . '_' . date('Ymd-h.i.s') . '.txt';
                     echo 'Creating file: ' . $file_name . '<br/>';
                     $file = fopen($file_name, 'w');
                     fwrite($file, 'Title: ' . $template['pieceTitle'] . "\n");
                     fwrite($file, 'Categories: ' . $template['categories'] . "\n");
                     fwrite($file, 'From: ' . $template['fromAddress'] . "\n");
                     fwrite($file, 'To: ' . $template['toAddress'] . "\n");
                     fwrite($file, 'Cc: ' . $template['ccAddress'] . "\n");
                     fwrite($file, 'Bcc: ' . $template['bccAddress'] . "\n");
                     fwrite($file, 'Subject: ' . $template['subject'] . "\n\n");
                     fwrite($file, 'Text Body: ' . $template['textBody'] . "\n\n\n");
                     fwrite($file, 'Html Body: ' . $template['htmlBody'] . "\n");
                     fclose($file);
                 } catch (Exception $e) {
                     echo "Could not export template: " . $result->Id . "<br/>";