php – Form to email, file upload difficulties-ThrowExceptions

Exception or error:

I am relatively new to php but learning as I go. I am creating a tenant application form for my business, and have got the form working with basic text, dropdown fields, etc. Now I need to add a file upload to the form that will be sent to my email.

Can anyone please spare the time to add in the file upload as an attachment as I’ve tried a few different code snippets but struggled with it.

the HTML for the form is quite lengthy, I will post it if required but i have added enctype="multipart/form-data" to the form tab, name for inout is file_upload.

Heres my form handler, which may well be a long way round but it works presently

if(isset($_POST['email'])) { 
    $email_to = "REMOVED";
    $email_subject = "TENANT APPLICATION";

    function died($error) {

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";

    $property = $_POST['property'];  
    $title = $_POST['title']; 
    $first_name = $_POST['first_name']; 
    $last_name = $_POST['last_name']; 
    $dateofbirth = $_POST['dateofbirth']; 
    $nationality = $_POST['nationality']; 
    $email_from = $_POST['email']; 
    $telephone = $_POST['telephone'];
    $natins = $_POST['natins'];
    $smoker = $_POST['smoker'];
    $bankrupt = $_POST['bankrupt'];
    $ccj = $_POST['ccj']; 
    $currentaddress = $_POST['currentaddress']; 
    $tenancy = $_POST['tenancy'];
    $timeataddress = $_POST['timeataddress']; 
    $employmentstatus = $_POST['employmentstatus']; 
    $company = $_POST['company']; 
    $positionheld = $_POST['positionheld']; 
    $salary = $_POST['salary']; 
    $timeatemployer = $_POST['timeatemployer']; 
    $selfemployedinfo = $_POST['selfemployedinfo']; 
    $selfemployedincome = $_POST['selfemployedincome'];
    $otheroccupantsover18 = $_POST['otheroccupantsover18'];
    $otheroccupantsunder18 = $_POST['otheroccupantsunder18'];
    $pets = $_POST['pets']; 

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

    $string_exp = "/^[A-Za-z .'-]+$/";

    if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

    if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

    if(strlen($error_message) > 0) {

    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);

    $email_message .= "Property applied for: ".clean_string($property)."\n\n";

       $email_message .= "APPLICANT DETAILS.\n\n";

    $email_message .= "Title: ".clean_string($title)."\n";
    $email_message .= "First name: ".clean_string($first_name)."\n";
    $email_message .= "Last name: ".clean_string($last_name)."\n";
    $email_message .= "Date of birth: ".clean_string($dateofbirth)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "National insurance : ".clean_string($natins)."\n";
    $email_message .= "Smoker : ".clean_string($smoker)."\n";
    $email_message .= "Bankrupt : ".clean_string($bankrupt)."\n";   
    $email_message .= "CCJ's : ".clean_string($ccj)."\n\n";

      $email_message .= "CURRENT ADDRESS DETAILS.\n\n";

    $email_message .= "Current address: ".clean_string($currentaddress)."\n";
    $email_message .= "Type of tenancy: ".clean_string($tenancy)."\n";
    $email_message .= "Time at current address: ".clean_string($timeataddress)."\n\n";

      $email_message .= "PREVIOUS ADDRESS DETAILS.\n\n";

    $email_message .= "Previous address 1: ".clean_string($previousaddressone)."\n";
    $email_message .= "Previous address 2: ".clean_string($previousaddresstwo)."\n\n";

      $email_message .= "EMPLOYMENT.\n\n";

    $email_message .= "Employment status: ".clean_string($employmentstatus)."\n\n";

       $email_message .= "EMPLOYED DETAILS.\n\n";

    $email_message .= "Company: ".clean_string($company)."\n";
    $email_message .= "Position held: ".clean_string($positionheld)."\n";
    $email_message .= "Salary: ".clean_string($salary)."\n";
    $email_message .= "Time with employer: ".clean_string($timeatemployer)."\n\n";

       $email_message .= "SELF EMPLOYED DETAILS.\n\n";

    $email_message .= "Details of self employment: ".clean_string($selfemployedinfo)."\n";
    $email_message .= "Average net income of past 3 years: ".clean_string($selfemployedincome)."\n\n";

       $email_message .= "OTHER OCCUPANTS.\n\n";

    $email_message .= "Other occupants OVER 18: ".clean_string($otheroccupantsover18)."\n";
    $email_message .= "Other occupants UNDER 18: ".clean_string($otheroccupantsunder18)."\n";
    $email_message .= "Details of any pets: ".clean_string($pets)."\n\n";

    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  

    <?php include 'thankyouapplicationform.php';?>



I have successfully got the following code working but this was a code snippet downloaded from somewhere, and i failed to merge the file upload part into my form handler

'''  <?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
Tested working with PHP5.4 and above (including PHP 7 )

require_once './vendor/autoload.php';

use FormGuide\Handlx\FormHandler;

$pp = new FormHandler(); 

$validator = $pp->getValidator();


$pp->sendEmailTo('REMOVED'); // ↠Your email here

echo $pp->process($_POST); '''

Edit Here are the attempts I’ve made so far…

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *