Perl: Send A Letter with SendLetterAndAddress()

Instructions

  1. Make sure your Perl environment is ready and create a new script.
  2. Copy the entire code below and paste the code in the Perl script.
    • 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 MyFileExtension with the extension of the document you are using for this test.
    • Replace MyFile 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.
  3. 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.

#!/usr/bin/perl
use strict;
use warnings;
use SOAP::Lite;
use MIME::Base64;
 
my $MyUsername = 'MyUsername';
my $MyPassword = 'MyPassword';
my $MyDescription = 'Sending a letter using Perl';
my $MyFile = 'MyFile.pdf';
my $MyWorkMode = 'Default';
my $MyAttentionLine1 = 'George Washington';
my $MyAttentionLine2 = '';
my $MyCompany = 'The White House';
my $MyAddress1 = '1600 Pennsylvania Ave NW';
my $MyAddress2 = '';
my $MyCity = 'Washington';
my $MyState = 'DC';
my $MyPostalCode = '20500';
my $MyCountry = 'United States of America';
 
 
#  Read file as binary in 1K blocks
my ($file, $data, $n);
open FILE, $MyFile;
while (read FILE, $data, 1024 != 0) {
    $file .= $data;
}
close FILE;
$file = encode_base64($file);
 
 
my $soap = SOAP::Lite
    -> uri('PostalMethods')
    -> on_action( sub { join '/', 'PostalMethods', $_[1] } )
    -> proxy('https://api.postalmethods.com/2009-02-26/PostalWS.asmx');
 
 
my $result =  $soap->SendLetterAndAddress( 
			SOAP::Data->new(name => 'Username', value => $MyUsername)->uri('PostalMethods'),
			SOAP::Data->new(name => 'Password', value => $MyPassword)->uri('PostalMethods'),
			SOAP::Data->new(name => 'MyDescription', value => $MyDescription)->uri('PostalMethods'),
			SOAP::Data->new(name => 'FileExtension', value => 'pdf')->uri('PostalMethods'),
			SOAP::Data->new(name => 'FileBinaryData', value => $file)->uri('PostalMethods'),
			SOAP::Data->new(name => 'WorkMode', value => $MyWorkMode)->uri('PostalMethods'),
			SOAP::Data->new(name => 'AttentionLine1', value => $MyAttentionLine1)->uri('PostalMethods'),
			SOAP::Data->new(name => 'AttentionLine2', value => $MyAttentionLine2)->uri('PostalMethods'),
			SOAP::Data->new(name => 'Address1', value => $MyAddress1)->uri('PostalMethods'),
			SOAP::Data->new(name => 'Address2', value => $MyAddress2)->uri('PostalMethods'),
			SOAP::Data->new(name => 'City', value => $MyCity)->uri('PostalMethods'),
			SOAP::Data->new(name => 'State', value => $MyState)->uri('PostalMethods'),
			SOAP::Data->new(name => 'PostalCode', value => $MyPostalCode)->uri('PostalMethods'),
			SOAP::Data->new(name => 'Country', value => $MyCountry)->uri('PostalMethods')
			);
 
unless ($result->fault) {
	my $statusCode = $result->result;
	if($statusCode > 0){
	    print "Message submitted successfully with transaction ID " . $statusCode . "\n";
	} else {
	    print "Message submission failed on error " . $statusCode . "\n";
	}
} else {
	print join ', ', 
	$result->faultcode, 
	$result->faultstring, 
	$result->faultdetail;
}
 
exit 1;