Perl: Send A Letter With SendLetterV2()

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, for method SendLetterV2 you must use a document where the recipient's address already appears so it will 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.
  3. Run your script. It will start, encode your document as Base64 and send a letter to the PostalMethods SendLetterV2 Web Service method. If the response is a positive number, you will be able to see your letter in the PostalMethods Control Panel.
  4. 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 = 'Sending a letter using Perl';
#  When using this method your file must include the address block in
# it's proper location for the envelope type you wish to use.
# Please see the list of supported file types for more information
# (http://www.postalmethods.com/supported)
my $MyFile = 'MyFile.pdf';
my $MyWorkMode = 'Default';
 
 
#  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/PostalWS.asmx');
 
 
my $result =  $soap->SendLetterV2( 
			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')
			);
 
unless ($result->fault) {
	my $statusCode = $result->result;
	if($statusCode > 0){
	    print "Message submitted successfully with transaction ID " . $statusCode . "\n";
	} else {
	    #
	    # A negative value means an error occurred.
	    # See the PostalMethods Status Codes: http://www.postalmethods.com/statuscodes#webservice.
	    #
	    print "Message submission failed on error " . $statusCode . "\n";
	}
} else {
	print join ', ', 
	$result->faultcode, 
	$result->faultstring, 
	$result->faultdetail;
}
 
exit 1;