PHP: Send PDF or other binary file with SendLetter()

<?php
 
/************* Settings  Begin ****************/
 
$filename = 'test.doc';  // file to be posted; contents need to conform to requirements (address in proper address area)
$username = '********';
$password = '********';
$description = 'Sending a letter using PHP';
$mode = 'Default';  // Default, Production, or Development.
 
/************* Settings  End ******************/
 
 
// Open File
if( !($fp = fopen($filename, "r"))){
	// Error opening file
	// Handle error however appropriate for your script
	echo "Error opening file";
	exit;
}
 
// Read data from the file into $data
$data = "";
while (!feof($fp)) $data .= fread($fp,1024);
fclose($fp);
 
$soapclient = new SoapClient('https://api.postalmethods.com/2009-02-26/PostalWS.asmx?WSDL');
$result = $soapclient->SendLetter(array('Username'    => $username,
                                     'Password'       => $password,
                                     'MyDescription'  => $description,  // free-form description for your records
                                     'FileExtension'  => end(explode(".", $filename)),  // make sure the extension reflects the file type
                                     'FileBinaryData' => $data,  // PHP5 does base64_encoding implicitly
                                     'WorkMode'       => $mode)); 
 
 
//print_r($result);
 
$status_code = $result->SendLetterResult;  // $status_code is the (positive) transaction ID if successful, or a (negative) error number if unsuccessful
if ($status_code > 0){
	print "Message submitted successfully with transaction ID <b>$status_code</b>";
} else {
	print "Message submission failed on error <a href=\"http://www.postalmethods.com/resources/reference/status-codes\">$status_code</a>";
}
?>

Continue to: PHP: Retrieve The PDF Document Of The Letter Or Postcard With GetPDF() ›

Send Letters
Securely + Reliably
Send letters securely and reliably from any business application. Use for invoices, quotes, and other important business communications. Enjoy special web-form to postal features and instant notifications.
Simple Process
Easy as 1-2-3
Experience the ease of our streamlined process - developed for efficiency! You submit your document by email or API. We automatically print, collate, insert, and stamp your letter. Your letter is delivered via standard postal service.
Pay As You Go
No Fuss. No Obligations.
Register for free with no obligations! Evaluate the service as long as you need. Once you are ready, set up our user-friendly pay-as-you option to use our service. No fuss. No on-going commitment. Just plain easy!