title: How to Send a form in an email with an attachment using PHP
body: <p>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: </p>
  
  <pre><code>    &lt;form METHOD = "POST" action="Parsing.php" name="form" enctype = "multipart/form-data"&gt; 
  &lt;fieldset&gt;
  &lt;legend&gt; Enter Information &lt;/legend&gt; 
  &lt;p&gt;
  Plant:
  &lt;label for 'Plant_ID'&gt;  &lt;/label&gt; &lt;br&gt;
  &lt;input type="text" name="Plant_ID"  /&gt; &lt;br&gt;
  &lt;/p&gt; 
  
  &lt;p&gt;
  Date:
  &lt;label for 'Datedetails'&gt; &lt;/label&gt; &lt;br&gt;
  &lt;input type="date" name="Datedetails" placeholder="YYYY-MM-DD" required pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}" /&gt;&lt;br&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
  Analysis: 
  &lt;label for 'Analysis'&gt; &lt;/label&gt; &lt;br&gt;
  &lt;input type= "text" name= "Analysis" /&gt; &lt;br&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
  Attachment:
  &lt;label for 'file'&gt;  &lt;/label&gt; &lt;br&gt;
  &lt;input type="file" name="file"  accept = "video/*, image/*, media_type, file_extension" /&gt;  &lt;br&gt;
  &lt;/p&gt;
  
  &lt;input type="submit" value="submit" name = "submit" id="submit"/&gt; &lt;br&gt; 
  &lt;/fieldset&gt;
  &lt;/form&gt; 
  </code></pre>
  
  <p>Here is the php file that I have as of now: </p>
  
  <pre><code>&lt;?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 &gt; filesize($file)) {
          return false;
      }
      fseek($sourceFile, 390);
      $dataLength = current(unpack("v", fread($sourceFile, 2)));
      if($dataLength + $targetOffset &gt; 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 '&lt;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" ;
  
  
  ?&gt;
  </code></pre>
  
  <p>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! </p>