title: How to add images as an attachment to php HTML mail?
body: <p>Is there any issue with my below codes? </p>
  
  <p>I'm trying to add an image as an attachment to HTML mail send it from my form. Users have an option to add images and on click of submit button it will be uploaded and sent. </p>
  
  <p>But an error occurs on click of submit and within seconds it automatically redirects to success page, but no email is sent from the form. If there any mistakes in my code could someone clean the code and point out the mistakes I have done?   </p>
  
  <p>Thanks </p>
  
  <pre><code>require_once "Mail.php";
          $semi_rand = md5(time()); 
          $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
          $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
          $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
          $message .= "--{$mime_boundary}\n";
          // preparing attachments            
              $file = fopen($filename,"rb");
              $data = fread($file,filesize($filename));
              fclose($file);
              $data = chunk_split(base64_encode($data));
              $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" . 
              "Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" . 
              "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
              $message .= "--{$mime_boundary}--\n";
  $Subject = Trim(stripslashes($_POST['Subject']));
  $Name = Trim(stripslashes($_POST['Name'])); 
  $filename = "/uploads/" . $_FILES["file"]["name"];
  $Email = Trim(stripslashes($_POST['Email'])); 
  $EmailMe = 'shmshd12@gmail.com'; 
  $ip = $_SERVER['REMOTE_ADDR'];
  $filename="/uploads/file.jpeg";
  $fname="file.jpeg";
  $Messages = Trim(stripslashes($_POST['Message']));
  $smtp = Mail::factory('smtp', array(
          'host' =&gt; 'host',
          'port' =&gt; '##',
          'auth' =&gt; true,
          'username' =&gt; 'mail@example.com',
          'password' =&gt; '*********',
      ));
  $Body = "&lt;html&gt; html email&lt;/html&gt;";
  $Body1 = "&lt;html&gt; html email&lt;/html&gt;";
  $semi_rand = md5(time()); 
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
  $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
  $headers .= "From: Roberta &lt;request@robertalollobrigida.ga&gt; \r\n";
  $headers .= "Reply-To: &lt; reply@example.com &gt; \r\n";
  $headers .= "Return-Path: user@example.com";
  $headers .= "X-Mailer: PHP \r\n";
          $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
          $message .= "--{$mime_boundary}\n";
          // preparing attachments            
              $file = fopen($filename,"rb");
              $data = fread($file,filesize($filename));
              fclose($file);
              $data = chunk_split(base64_encode($data));
              $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" . 
              "Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" . 
              "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
              $message .= "--{$mime_boundary}--\n";
  
  $success =  mail($Email, $Subject, $message, $headers, "-f " . $from); 
              mail($EmailMe, $Subject,  $message, $ip, $headers, "-f " . $from); 
  
              if (success){
    print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=/thanks\"&gt;";
  }
  else{
    echo "&lt;meta http-equiv=\"refresh\" content=\"0;URL=/error\"&gt;";
  }
  </code></pre>