4 Replies Latest reply on May 25, 2010 10:27 AM by Nancy OShea

    Help for email with attachment.


      I am trying to do a code for sending emails with an attachment. I have tried many things but I am still getting error codes. I am new to this and clueless on what to do at this point. I have downloaded xampp and a mail server trying to figure this out. I cannot find the php.ini file anywhere on my pc. I did find a php.ini.h file somewhere on my pc. I am working on a pc that has windows vista hp.


      Here's the code I have. The file is saved in xammp/htdocs.




      if(isset($_POST) && !empty($_POST)) {


      if(!empty($_FILES['attachment']['name'])) {


      $file_name = $_FILES['attachment']['name'];

      $temp_name = $_FILES['attachment']['tmp_name'];

      $file_type = $_FILES['attachment']['type'];


      $base = basename($file_name);

      $extension = substr($base, strlen($base)-4, strlen($base));


      $allowed_extensions = array(".doc","docx",".pdf",".png",".jpg");


      if(in_array($extension,$allowed_extensions)) {


      $from = $_POST['email'];

      $to = "myemail@here.com";

      $subject = "Test with attachment";

      $message = "This is a test";


      $file = $temp_name;

      $content = chunk_split(base64_encode(file_get_contents($file)));

      $uid = md5(uniqid(time()));


      $header = "From: ".$from."\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 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: ".$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";


      if (mail($to, $subject, "", $header)) {

      echo "Success";


      } else {

      echo "Failed";





      } else {

      echo "File type not allowed";






      What am I doing wrong? Do I have the file in the right location? Should there be more than one file to get this action to happen? All help is appreciated.