title: php - instead of uploading to folder, send as email with attachment of file uploaded
body: <pre><code>&lt;?php
      //if there is post
  if(isset($_POST) &amp;&amp; !empty($_POST)){
          //if there is attachment
          if(!empty($_FILES['attachment']['name'])){
              //store some variables
              $file_name = $_FILES['attachment']['name'];
              $temp_name = $_FILES['attachment']['tmp_name'];
              $file_type = $_FILES['attachment']['type'];
  
              //get the extension of the file
              $base = basename($file_name);
              $extension = substr($base, strlen($base)-4,strlen($base));
  
              //only these file type will be allowed
              $allow_extensions = array(".doc","docx",".pdf",".zip",".png");
  
              //check if file is allowes
              if(in_array($extension,$allow_extensions)){
  
                  //mail essentials
                  $from = $_POST['email'];
                  $to = "sampleemail@gmail.com";
                  $replyto = $to;
                  $subject = "email with attachment";
                  $message = "this is a random message";
  
                  //things you need
                  $file = $temp_name;
                  $content = chunk_split(base64_encode(file_get_contents($file)));
                  $uid = md5(uniqid(time()));
  
                  //standard mail headers
                  $header = "From " . $from . "\r\n";
                  $header .= "Reply-To: " . $replyto . "\r\n";
                  $header .= "MIME-Version: 1.0\r\n";
  
                  //declairing we hav e multiple parts of message like text
                  $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
                  $header .= "This is a multi-part message in MIME format. \r\n";
  
                  //plain text part
                  $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";
  
                  //file attachment
                  $header .= "--".$uid."\r\n";
                  $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
                  $header .= "Content-Transfer-Encoding: base64\r\n";
                  $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
                  $header .= $content. "\r\n\r\n";
  
                  //send mail 
                  if(mail($to, $subject, "", $header)){
                      echo "Mail Sent";
                  }
                  else
                  {
                      echo "Failed";
                  }
  
              }
              else{
                  echo "file type not allowed";
              }
  
  
          }
          else{
              echo "no file posted";
          }
      }
  ?&gt;
  
  
  &lt;html&gt;
      &lt;head&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
      &lt;form method="post" action="index.php" enctype="multipart/form-data"&gt;
          &lt;input type="text" name="email" value="from" /&gt;
          &lt;br&gt;
          &lt;input type="file" name="attachment" /&gt;
          &lt;br&gt;
          &lt;input type="submit" value="Send Mail" /&gt;
      &lt;/form&gt;     
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Can't find the bug to my code, I am not getting the link to my attachment.. T_T please help. I am not good in boundary.. T_T I tried using phpmailer but can't get it to work, is there any document to read on how to set it up? I really just wanted to make a simple form with attach button for applicant to sent their resumes.. </p>