Web Form to Fax with PHP

25 Nov 2016 This code sample is deprecated and in process of being removed. Check out our new code libraries and usage documentation on InterFAX @ Github.

This sample demonstrates how to fax from a very basic web form.

This sample uses method SendfaxEx_2.

There are three parts to this example:

  1. A web form
  2. A web form processing script. For consistency with the web form, name this form.php
  3. A fax template which get populated by content entered into the form. For consistency with the processing script, name this template.html. The template is HTML-based making it very easy to change and to extend with CSS.

How it works

The web form is a regular HTML form that collects faxing-related fields, such as the recipient name, fax number, and the fax message. Once submitted it calls the web form processing script through the “action” value of the form.

The web form processing script does several things:

  • Holds all settings, such as your InterFAX username and password.
  • Accepts values from the web form. This sample only shows a few fields being processed, but you can add as many fields as you like.
  • Performs error checking on submitted values. This sample only shows very basic error checking, but you can add as much as you like.
  • Opens a template file and populates it with values submitted in the form.
  • Submits the populated template file to InterFAX for faxing.
  • Shows the submission result (i.e., not the fax transmission result, for which there are other samples in this section)

The template is used by the form processing script. Every field in the template needs to be replaced by actual field values collected by the processing script.

The code

The web form




    
        InterFAX Form-to-Fax Example
        
            label, input, textarea, select {    display: block;    width: 180px; float: left;    margin-bottom: 10px;}
            label { text-align: right; width: 95px; padding-right: 20px; font-weight: bold; font-size: 83%;}
            br { clear: left;}
            #subbut { margin-top: 10px; border: thin solid black; position: relative; left: 115px; font-size: 90%;}
            .required { color: #a00;}
            #note { width: 180px; color: #a00; margin-left: 115px; font-size: 83%;}
        
    
    
        Send a fax
        
            
* Recipient Name: * Fax Number: * Message:

*required entries

The web form processing script





InterFAX Form-to-Fax Example Result


<?php /**************** Settings begin **************/ $username = ; // Insert your InterFAX username here $password = ; // Insert your InterFAX password here $filename = ‘template.html’; // The HTML template location in your filesystem $filetype = ‘HTML’; // File format; supported types are listed at // https://www.interfax.net/en/help/supported_file_types $postponetime = ‘2001-12-31T00:00:00-00:00’; // don’t postpone $retries = ‘3’; $csid = ‘AA CSID’; $pageheader = ‘To: {To} From: {From} Pages: {TotalPages}’; $subject = ‘Anything goes’; $replyemail = ; $page_size = ‘A4’; $page_orientation = ‘Portrait’; $high_resolution = FALSE; $fine_rendering = TRUE; $page = “https://www.example.com/form.html”; // URL from which the form may be posted /**************** Settings end ****************/ $errcount = 0; // only allow posting of the form from a defined URL if ($page <> $_SERVER[‘HTTP_REFERER’]){ echo “Invalid referrer”; die; } // import form information $name = $_POST[‘name’]; $faxnumber = $_POST[‘faxnumber’]; $message = $_POST[‘message’]; /* Simple form validation check to see if recipient name, fax number and message were entered */ // if no recipient name entered print an error if (empty($name)){ print “Error: Recipient name is required “; $errcount++; } // if no fax number entered print an error if (empty($faxnumber)){ print “Error: Fax number is required “; $errcount++; } // if no message entered print an error if (empty($message)){ print “Error: Message is required.”; $errcount++; } // end validation // if the form is valid if(!$errcount){ // open template file if( !($fp = fopen($filename, “r”))){ // Error opening file echo “Error opening file”; exit; } // read data from template file into $template $template = “”; while (!feof($fp)) $template .= fread($fp,1024); fclose($fp); // replace placeholders in template with collected data $template = str_replace(‘{name}’, $name, $template); $template = str_replace(‘{faxnumber}’, $faxnumber, $template); $template = str_replace(‘{message}’, $message, $template); //fax the form contents //echo $template; // uncomment to debug template $client = new SoapClient(“https://ws.interfax.net/dfs.asmx?WSDL”); // load all Web Service parameters $params->Username = $username; $params->Password = $password; $params->FaxNumbers = $faxnumber; $params->Contacts = $name; $params->FilesData = $template; $params->FileTypes = $filetype; $params->FileSizes = strlen($template) ; $params->Postpone = $postponetime; $params->RetriesToPerform = $retries; $params->CSID = $csid; $params->PageHeader = $pageheader; $params->JobID = ; $params->Subject = $subject; $params->ReplyAddress = $replyemail; $params->PageSize = $page_size; $params->PageOrientation = $page_orientation; $params->IsHighResolution = $high_resolution; $params->IsFineRendering = $fine_rendering; // submit document for faxing $result = $client->SendfaxEx_2($params); // capture return value $ret = $result->SendfaxEx_2Result; if($ret > 0){ // fax submission succeeded echo ‘Fax submitted successfully. Transaction ID: ‘ . $ret; } else { // fax submission failed echo ‘Fax submission failed. Error message: . $ret . ; } } // end valid form handling ?>



The fax template



    
        
        body, p, table, tr, td, th { font-family: 'Garamond'; }
        
    
    
        Fax Message
        
To: {name}
Fax: {faxnumber}

Message:


{message}