Tuesday, November 27, 2018

Ability to add message with file attachments using PHP

Note: This code is applicable to the PHP Toolkit prior to 2012.2.

Below is a sample scripts that will add a message with file attachment in the Support Case
using PHP add web services operation:

     $imgPath = 'C:\Documents and Settings\hguerrero\My Documents\HelloWorld.doc;
     $imgContents = file_get_contents($imgPath); //php built in function that will extract the contents of the file
     $Message = new nsComplexObject('Message');
     $MediaItem = new nsComplexObject('MessageMediaItemList');

     $nsFile = new nsComplexObject('File');

     $nsFileFields = array(
                                       'folder' => new nsRecordRef(array('internalId' => '-10')),
                                       'name' => 'HelloWorld.doc',
                                       'fileType' => '_ WORD,
                                       'attachFrom' => '_computer',
                                       'content' => $imgContents);

     $nsFile->setFields($nsFileFields);
     $MediaItem->setFields(array('mediaItem' => $nsFile));
     $nsFields = array('author' => new nsRecordRef(array('internalId' => '1','type' => 'customer')),
                                  'recipient' => new nsRecordRef(array('internalId' => '384','type' => 'contact')),
                                  'messageDate' => '2012-03-23T00:50:39.301Z',
                                  'subject' => "Case 1446161",
                                  'message' => "Message 101",
                                  'emailed' => "true",
                                  'activity' => new nsRecordRef(array('internalId' => '413','type' => 'supportCase')),
                                  'mediaItemList'=>$MediaItem
                                  );

     $Message->setFields($nsFields);
     $addResponse = $myNSclient->add($Message);


     if ($addResponse->isSuccess) {
          echo "Add Successful";
     }
     else {
          echo "Add Failed";
     }


Note: You can attach a file of up to 10 MB only.

No comments:

Post a Comment