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.
  3. Replace MyFile.pdf with a path to your test PDF file. Remember, method SendLetterAndAdress() will add the recipient's address so it on the first page of your letter, so it shows through the envelope window. You can use one of our Sample Letter Documents to leave spaces where the address will go.
  4. Replace MyUsername and MyPassword with your PostalMethods user details
  5. Replace all address parameters with real address values. Leave an empty string ("") where no value is needed.
  6. Run your script. It will start 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 through PostalMethods. Congratulations!

#!/usr/bin/perl
use strict;
use warnings;
use SOAP::Lite;
use MIME::Base64;
 
my $MyUsername = 'MyUsername';
my $MyPassword = 'MyPassword';
my $MyDescription = 'MyDescription';
my $MyFile = 'MyFile.pdf';
my $MyAttentionLine1 = 'MyAttentionLine1';
my $MyAttentionLine2 = 'MyAttentionLine2';
my $MyAttentionLine3 = 'MyAttentionLine3';
my $MyAddress1 = 'MyAddress1';
my $MyAddress2 = 'MyAddress2';
my $MyCity = 'MyCity';
my $MyState = 'MyState';
my $MyPostalCode = 'MyPostalCode';
my $MyCountry = 'MyCountry';
 
 
#  Read file as binary in 1K blocks
my ($file, $data);
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/PostalWS.asmx');
 
 
my $statusCode =  $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 => 'AttentionLine1', value => $MyAttentionLine1)->uri('PostalMethods'),
			SOAP::Data->new(name => 'AttentionLine2', value => $MyAttentionLine2)->uri('PostalMethods'),
			SOAP::Data->new(name => 'AttentionLine3', value => $MyAttentionLine3)->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')
			)->result;
 
 
if($statusCode > 0){
  #
  # A positive value means the message was successfully queued for processing.
  # The PostalMethods Letter ID is returned.
  #
  print "Message submitted successfully with transaction ID " . $statusCode . "\n";
}
else{
  #
  # A negative value means an error occurred.
  # See the PostalMethods Status Codes at http://www.postalmethods.com/statuscodes#webservice.
  #
  print "Message submission failed on error " . $statusCode . "\n";
}
 
exit 1;