0

I have an html form and a php file. The php file has a function which is used to parse the attachment and then the php should send the entire form to an email address with the parsed file. I don't know how to attach the file and ensure that all the information gets send to the email. I am a beginner at this so any help will be appreciated. Here is the html code:

    <form METHOD = "POST" action="Parsing.php" name="form" enctype = "multipart/form-data"> 
<fieldset>
<legend> Enter Information </legend> 
<p>
Plant:
<label for 'Plant_ID'>  </label> <br>
<input type="text" name="Plant_ID"  /> <br>
</p> 

<p>
Date:
<label for 'Datedetails'> </label> <br>
<input type="date" name="Datedetails" placeholder="YYYY-MM-DD" required pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}" /><br>
</p>

<p>
Analysis: 
<label for 'Analysis'> </label> <br>
<input type= "text" name= "Analysis" /> <br>
</p>

<p>
Attachment:
<label for 'file'>  </label> <br>
<input type="file" name="file"  accept = "video/*, image/*, media_type, file_extension" />  <br>
</p>

<input type="submit" value="submit" name = "submit" id="submit"/> <br> 
</fieldset>
</form> 

Here is the php file that I have as of now:

<?php

    $Plant_ID = $_POST['Plant_ID'];
    $Process_Order = $_POST['Process_Order'];
    $Material = $_POST['Material'];
    $Datedetails = $_POST['Datedetails'];
    $Batch_Number = $_POST['Batch_Number'];
    $Processing_Stage = $_POST['Processing_Stage'];
    $Analysis = $_POST['Analysis'];
    $file = $_POST['file'];


function generatePlotForSPA($file, $targetFile) {   //replace $source with $file 
    //code to parse spa files and plot 
    $sourceFile = fopen($file, "rb");

    fseek($sourceFile, 386);
    $targetOffset = current(unpack("v", fread($sourceFile, 2)));
    if($targetOffset > filesize($file)) {
        return false;
    }
    fseek($sourceFile, 390);
    $dataLength = current(unpack("v", fread($sourceFile, 2)));
    if($dataLength + $targetOffset > filesize($file)) {
        return false;
    }

    fseek($sourceFile, $targetOffset);

    $rawData = fread($sourceFile, $dataLength);
    $rawDataOutputPath = $file . "_raw_data";
    $outputFile = fopen($rawDataOutputPath, "w");
    fwrite($outputFile, $rawData);
    fclose($outputFile);
    $gnuScript = "set terminal png size {width},{height};    
        set output '{output}';

        unset key;
        unset border;

    plot '<cat' binary filetype=bin format='%float32' endian=little array=1:0 with lines lt rgb 'black';";

    $targetScript = str_replace("{output}", $targetFile, $gnuScript);
    $targetScript = str_replace("{width}", 500, $targetScript);
    $targetScript = str_replace("{height}", 400, $targetScript);
    $gnuPath = "gnuplot";
    $outputScript = "cat \"" . $rawDataOutputPath . "\" | " . $gnuPath . " -e \"" . $targetScript . "\"";
    exec($outputScript);
    if(!file_exists($targetFile)) {   
        return false;
    }
    return true;
}



if(!isset($_POST['submit'])){
    //This page should not be accessed directly, this is the backhand service 
    echo "error, you need to submit the form";
    }

    //validate 
    if(empty($Plant_ID)|| empty($Process_Order) || empty($Material) || empty($Datedetails) || empty($Batch_Number) || empty($Processing_Stage) || empty($Analysis) || empty($file)){
        echo "All the fields are mandatory, Please provide all the information"
        exit;
    }

    $email_subject = "IR data"; 
    $to = "abc@xyz.com";

    $mail($to, $subject, "Plant ID:" .$Plant_ID, " Material:" .$Material, "Date:" .$Datedetails , "Process Order:" .$Process_Order , "Batch Number:" .$Batch_Number, "Processing Stage:" .$Processing_Stage, "Analysis:" .$Analysis , "File:" .$targetFile);
    echo "The information has been stored" ;


?>

I read up online that Swift Mailer might be something that will help me but I am not sure on how to use that. Thanks a lot in advance!

Meg
  • 9
  • 2
  • 1
    check out [PHPMailer](https://github.com/PHPMailer/PHPMailer) they have an example on their github. – mister martin Dec 08 '16 at 20:31
  • 1
    I strongly recommend using [PHPMailer](https://github.com/PHPMailer/PHPMailer) achiev what you want. As of right now, your code is insecure! If you wonder why, have a look at my answer here: http://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921 You'll be amazed how many security vulnerabilities exist within upload scripts! Your code above hasn't covered most of them. – icecub Dec 08 '16 at 20:33

0 Answers0