Perl Fax – Send Binary Files Using SendFax

Download the Perl fax code samples.

This example shows how to submit a binary file to the fax Web service. This can be done using SendFax with Perl.

The SendFax method indicates if the submission succeeded. The return value of a successful submission is the transaction ID (positive number) of the fax in the system. Other return values can indicate a failure (for more details, see Web Service Return Codes).

This Perl fax script sends a binary fax from Perl and indicates the success/failure of the submission:


#/**************** Settings begin **************/
my $username  = ''; # Enter your Interfax username here
my $password  = ''; # Enter your Interfax password here
# Enter the destination fax number here in the format:
# +[country code][area code][fax number] for example: +1212555487
my $faxNumber = ''; 
my $filename  = 'sample.pdf';
my $filetype  = 'pdf';
my $NS = 'https://www.interfax.cc';
#/**************** Settings end ****************/


#  Read file as binary in 1K blocks
my ($file, $data);
open FILE, $filename;
while (read FILE, $data, 1024 != 0) {
    $file .= $data;
}
close FILE;
$file = encode_base64($file);

my $client = SOAP::Lite
    ->uri($NS)
    ->on_action( sub { join '/', $NS, $_[1] } ) 
    ->proxy('https://ws.interfax.net/dfs.asmx?wsdl');
    
my $result = $client
    ->call(SOAP::Data->name('Sendfax')->attr({xmlns => $NS}) =>
                    SOAP::Data->name('Username')->value($username)->type(''),
            SOAP::Data->name('Password')->value($password)->type(''),
            SOAP::Data->name('FaxNumber')->value($faxNumber)->type(''),
            SOAP::Data->name('FileData')->value($file)->type(''),
            SOAP::Data->name('FileType')->value($filetype)->type('')
    );


if ( $result->fault ) {
    print $result->faultstring . "\n";
} else {
    if( $result->valueof('//SendfaxResult') > 0 ) {
        print "Success. TransactionID=" . $result->valueof('//SendfaxResult') . "\n";
    } else {
        print "Error, return code=" . $result->valueof('//SendfaxResult') . "\n";
    }
}