title: PHP Mail() attachment error and corrupt files
body: <p>I am trying to process an email in response to a submission.  The email should grab a previously uploaded file and include it as an attachment.  There are two problems occuring right now.</p>
  
  <ol>
  <li>I am receiving error messages on the page when the process runs to send the message.  The errors are: 
  Warning: filesize() [function.filesize]: stat failed for ./example.pdf in /home/avantjob/public_html/portal/profilemenu.php on line 186</li>
  </ol>
  
  <p>Warning: fopen(./example.pdf) [function.fopen]: failed to open stream: No such file or directory in /home/avantjob/public_html/portal/profilemenu.php on line 187</p>
  
  <p>Warning: fread() expects parameter 1 to be resource, boolean given in /home/avantjob/public_html/portal/profilemenu.php on line 188</p>
  
  <p>Warning: fclose() expects parameter 1 to be resource, boolean given in /home/avantjob/public_html/portal/profilemenu.php on line 189</p>
  
  <p>Despite receiving these messages, the email is being sent, and the attachment is included, whoever the file is corrupt/truncated and will not open.</p>
  
  <p>The code is listed below:</p>
  
  <pre><code>//.Class for processing mail
  class AttachmentEmail {
  private $from = 'triangle@avant.jobs';
  private $from_name = 'AVANT Portal';
  private $reply_to = 'triangle@avant.jobs';
  private $to = '';
  private $subject = '';
  private $message = '';
  private $attachment = '';
  private $attachment_filename = '';
  
  public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') 
      {
      $this -&gt; to = $to;
      $this -&gt; subject = $subject;
      $this -&gt; message = $message;
      $this -&gt; attachment = $attachment;
      $this -&gt; attachment_filename = $attachment_filename;
      }
  
  public function getMimeType($file) 
  {
  // MIME types array
  require_once('mimetypes.php');
  $extension = end(explode('.', $file));
  return $mimeTypes[$extension]; // return the array value
  }
  
  public function mail() {
      if (!empty($this -&gt; attachment)) {
          $filename = empty($this -&gt; attachment_filename) ? basename($this -&gt; attachment) : $this -&gt; attachment_filename ;
          $path = dirname($this -&gt; attachment);
          $mailto = $this -&gt; to;
          $from_mail = $this -&gt; from;
          $from_name = $this -&gt; from_name;
          $replyto = $this -&gt; reply_to;
          $subject = $this -&gt; subject;
          $message = $this -&gt; message;
  
          $file = $path.'/'.$filename;
          $mime_type = $this-&gt;getMimeType($file);
          $file_size = filesize($file);
          $handle = fopen($file, "r");
          $content = fread($handle, $file_size);
          fclose($handle);
          $content = chunk_split(base64_encode($content));
          $uid = md5(uniqid(time()));
          $name = basename($file);
          $header = "From: ".$from_name." &lt;".$from_mail."&gt;\r\n";
          $header .= "Reply-To: ".$replyto."\r\n";
          $header .= "MIME-Version: 1.0\r\n";
          $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
          $header .= "This is a multi-part message in MIME format.\r\n";
          $header .= "--".$uid."\r\n";
          $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
          $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
          $header .= $message."\r\n\r\n";
          $header .= "--".$uid."\r\n";
          $header .= "Content-Type: ".mime_type."; name=\"".$filename."\"\r\n";
          $header .= "Content-Transfer-Encoding: base64\r\n";
          $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
          $header .= $content."\r\n\r\n";
          $header .= "--".$uid."--";
  
          if (mail($mailto, $subject, "", $header)) {
              return true;
          } else {
              return false;
          }
      } else {
          $header = "From: ".($this -&gt; from_name)." &lt;".($this -&gt; from)."&gt;\r\n";
          $header .= "Reply-To: ".($this -&gt; reply_to)."\r\n";
  
          if (mail($this -&gt; to, $this -&gt; subject, $this -&gt; message, $header)) {
              return true;
          } else {
              return false;
          }
  
      }
  }
  }
  
  // Display links to each profile sub-program
  
  if (isset($_POST['submit'])) 
      {
      $queryname = "SELECT FNAME, LNAME, MNAME, JOBWANT, PAYWANT, PAYCODE, RESUMEFILE FROM APP WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "'";
      $namedata = mysqli_query($dbc, $queryname);
      $namerow = mysqli_fetch_array($namedata);
      $fname = $namerow['FNAME'];
      $mname = $namerow['MNAME'];
      $lname = $namerow['LNAME'];
      $jobwant = $namerow['JOBWANT'];
      $paywant = $namerow['PAYWANT'];
      $paycode = $namerow['PAYCODE'];
      $resumefile = $namerow['RESUMEFILE'];
      $msg = "This message was sent in response to a completed application on the AVANT Portal. \n".
          " \n".
          "The application information is listed below. \n".
          " \n".
          "Name: $fname $mname $lname \n".
          "Desired Job: $jobwant \n".
          "Pay Rate: $paywant per $paycode \n".
          "Please review this candidate as soon as possible. \n".
          " \n".
          "The resume can be viewed at www.avant.jobs/portal/uploads/" . $resumefile . "\n".
          " \n";
      $subject = "AVANT Portal Application - " . $fname . " " . $lname;
      $sendit = new AttachmentEmail('triangle@avant.jobs', $subject, $msg, $resumefile);
      $sendit -&gt; mail();
  </code></pre>
  
  <p>Any help you can offer would be greatly appreciated.  Thank you.</p>