<?php
if($_POST['submit']=="Kirim")
{
     $penerima=$_POST['penerima'];
     $subyek=$_POST['subyek'];
     $pesan=$_POST['pesan'];
     $pesan=wordwrap($pesan,70);
     $pengirim="no-reply@sdatelemetry.com";
     $nama_pengirim="UNIT HIDROLOGI BBWS CILIWUNG CISADANE";
    
    //mengirim file lampiran ke server
    $tmp_file=$_FILES['lampiran']['tmp_name']; 
    $file_name=$_FILES['lampiran']['name']; 
    if(!empty($tmp_file)){
      $move=move_uploaded_file($tmp_file,$file_name);
      
      //mendapatkan isi file yg sudah dikirim di server hosting
      $fp = fopen($file_name,"rb");
         $data = fread($fp,filesize($file_name));
         fclose($fp);
      
      //encode data, for safe transit
      $konten= chunk_split(base64_encode($data));
      $name=basename($file_name);
      
      //boundary 
      $uid = md5(time()); 
      
      //headers for attachment 
      $headers = "From:  $nama_pengirim <$pengirim>\r\n";
      $headers .= "MIME-Version: 1.0\r\n";
      $headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    
      //preparing attachment
      if(!empty($file_name) > 0){
         if(is_file($file_name)){
            $messages = "--".$uid."\n";
            $messages .= "Content-Type: text/html; charset=\"UTF-8\"\r\n";
            $messages .= "Content-Transfer-Encoding: 7bit\n".$pesan."\r\n"; 
            
            $messages .= "--".$uid."\r\n";
            $messages .= "Content-Type: application/octet-stream; name=\"".$name."\"\r\n";
            $messages .= "Content-Transfer-Encoding: base64\r\n";
            $messages .= "Content-Description: ".$name."\r\n";
            $messages .= "Content-Disposition: attachment;\r\n";
            $messages .= "filename=\"".$name."\"\r\n\r\n".$konten."\r\n\r\n";
         
            $messages .= "--".$uid."\r\n";
            ini_set('sendmail_from',$pengirim);
            $return_path = "-".$pengirim;
            $kirim=mail($penerima,$subyek,$messages,$headers,$return_path);
            if($kirim==true){
              //menghapus file attachment di server
              if(file_exists($file_name)){
                 unlink($file_name);
              }
              echo "Pesan email dengan attachment berhasil dikirim.";
            }else{
              echo "Pesan email dengan attachment gagal dikirim.";
            }
         }
      }
    }
}
?>
<html>
  <style>
     form input{margin:3px;padding:3px 5px;}
     form input[type=text]{width:220px;}
     form input[type=submit], input[type=reset]{cursor:pointer;}
  </style>
  <body>
     <h2>Mengirim pesan email dengan file attachment</h2>
     <form name="form1" action="" enctype='multipart/form-data' method="post">
      Penerima : <input type="text" name="penerima"><br>
      Subyek : <input type="text" name="subyek"><br>
      Pesan : <br>
      <textarea name="pesan" cols="40" rows="6"></textarea><br>
      Lampiran File : <br>
      <input type="file" name="lampiran"><br>
      <input type="submit" name="submit" value="Kirim"> <input type="reset" value="reset">
    </form>
</body>
</html>