ColdFusion: Send A Letter with SendLetterAndAddress()

Instructions

  1. Make sure your ColdFusion environment is ready and create a new CFM page.
  2. Copy the entire code below and paste the code in the CFM page.
  3. Replace the values of the cfset lines with your details:
    • Replace MyUsername and MyPassword with your PostalMethods user details.
    • Replace MyDescription with your own text to help you identify this letter in your activity log (optional).
    • Replace absolutePathToFile with a path to your test PDF file. Remember, method SendLetterAndAdress will add the recipient's address so it would show through the envelope window. You can use one of our Sample Letter Documents.
    • MyWorkMode determines if your letter will be sent as Production, Development or will use your user's default Work Mode setting. Read more about Production and Development Work Modes.
    • Use the existing postal recipient or replace with another valid postal address. Leave an empty string ("") where no value is needed.
  4. Start your application. It will start, encode your document as Base64 and send a letter to the PostalMethods SendLetterAndAddress Web Service method. If the response is a positive number, you will be able to see your letter in the PostalMethods Control Panel.
    If the response is negative number, check the Web Service Status Codes section.

That's it - you have successfully sent a letter to PostalMethods. Congratulations.

<!--- /************* Settings  Begin ****************/ --->
 
<cfset webserviceUrl = 'https://api.postalmethods.com/2009-02-26/PostalWS.asmx?WSDL'>
<cfset MyUsername = 'MyUsername'>
<cfset MyPassword = 'MyPassword'>
<cfset MyDescription = 'Sending a letter using ColdFusion'>
<!--- file to be posted; contents need to conform to requirements (address in proper address area) --->
<cfset absolutePathToFile = expandPath('MyFile.pdf')>
<cfset MyWorkMode = 'Default'>
<cfset MyAttentionLine1 = 'George Washington'>
<cfset MyAttentionLine2 = ''>
<cfset MyCompany = 'The White House'>
<cfset MyAddress1 = '1600 Pennsylvania Ave NW'>
<cfset MyAddress2 = ''>
<cfset MyCity = 'Washington'>
<cfset MyState = 'DC'>
<cfset MyPostalCode = '20500'>
<cfset MyCountry = 'United States of America'>
 
<!--- /************* Settings  End ******************/ --->
 
 
<cffile action="readBinary" file="#absolutePathToFile#" variable="fileBinaryData">
 
 
<cfinvoke
	webservice="#webserviceUrl#"
	method="SendLetterAndAddress"
	returnVariable="statusCode">
		<cfinvokeargument name="Username" value="#MyUsername#">
		<cfinvokeargument name="Password" value="#MyPassword#">
		<cfinvokeargument name="MyDescription" value="#MyDescription#">
		<cfinvokeargument name="FileExtension" value="#ListLast(absolutePathToFile, '.')#">
		<cfinvokeargument name="FileBinaryData" value="#fileBinaryData#">
		<cfinvokeargument name="WorkMode" value="#MyWorkMode#">
		<cfinvokeargument name="AttentionLine1" value="#MyAttentionLine1#">
		<cfinvokeargument name="AttentionLine2" value="#MyAttentionLine2#">
		<cfinvokeargument name="Company" value="#MyCompany#">
		<cfinvokeargument name="Address1" value="#MyAddress1#">
		<cfinvokeargument name="Address2" value="#MyAddress2#">
		<cfinvokeargument name="City" value="#MyCity#">
		<cfinvokeargument name="State" value="#MyState#">
		<cfinvokeargument name="PostalCode" value="#MyPostalCode#">
		<cfinvokeargument name="Country" value="#MyCountry#">
</cfinvoke>
 
 
<!---
	statusCode is the (positive) Letter ID if successful, or a (negative) error number if unsuccessful.
--->
<cfoutput>
<cfif statusCode gt 0>
	Message submitted successfully with transaction ID <b>#statusCode#</b>
<cfelse>
	Message submission failed on error <a href="http://www.postalmethods.com/resources/reference/status-codes">#statusCode#</a>
</cfif>
</cfoutput>