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 MyFile.pdf with a path to your test PDF file. Remember, for method SendLetter() you must use a document where the recipient's address already appears on the first page of the document, in such a way that it shows through the envelope window. You can use one of our Sample Letter Documents.
- Replace MyUsername and MyPassword with your PostalMethods user details
- Run your script. It will send a letter to the 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 = 'MyDescription'; # When using this method your file must include the address block in # its proper location for the envelope type you wish to use. # For supported file types, see http://www.postalmethods.com/supported my $MyFile = 'MyFile.pdf'; # 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->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'), )->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: http://www.postalmethods.com/statuscodes#webservice. # print "Message submission failed on error " . $statusCode . "\n"; } exit 1;
