Perl: Send A Letter With SendLetter()
Instructions
- Make sure your Perl environment is ready and create a new script.
- 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 SendLetter 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.
- Run your script. It will start, encode your document as Base64 and send a letter to the PostalMethods SendLetter 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 = '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/2009-02-26/PostalWS.asmx'); my $result = $soap->SendLetter( 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;

