Emailing Attachments with PHP

I am currently trying to write a php document which will take the input of name and email address and file attachment and then the page will email the details to a specified email address. I have sucessfully created the email and have received it with an attachment, however i am having problems with the attachment, the email doesn't appear to have the attached file, rather it creates a new file and send its, i am assuming i have set up some correct properties to do with sending the email, i am new to php so i am not very familiar with most of the functionallity, i have included the code which i am using, an help would be greatly appreciated.


      $mail_to = "email@address";
      $mail_content = "\n\tFrom: ".$_POST[FName]." ".$_POST[LName]."\n\tEmail: ".$_POST[Email]."\n\t";
      $mail_reply = "From: $_POST[Email]\r\n"."Reply-To: $_POST[Email]\r\n";
      $mail_subject = "Test".$_POST[Position];
      $mail_Attachment = stripslashes($_POST[Attachment]);
      $mail_Attachment_name = substr($mail_Attachment, strchr($mail_Attachment, "/"));    

      $fp = fopen( $mail_Attachment, "r");
      $file = fread( $fp, filesize($fp) );
      $mail_Content_Type = mime_content_type($file);
      Encode The Data For Transition using base64_encode();
      And get a 32-character hexadecimal number
      $file = chunk_split(base64_encode($file));      
      $num = md5( time() );
      Define the main message headers
      $mail_header  = "From:$mail_to\r\n";
      $mail_header .= "MIME-Version: 1.0\r\n";
      $mail_header .= "Content-Type: multipart/mixed; ";
      $mail_header .= "boundary=$num\r\n";
      $mail_header .= "--$num\r\n";
      Define message section
      $mail_header .= "Content-Type: text/plain\r\n";
      $mail_header .= "Content-Transfer-Encoding: 8bit\r\n\n";
      $mail_header .= "$mail_content\r\n";
      $mail_header .= "--$num\n";
      Define the attachment section
      $mail_header .= "Content-Type: $mail_Content_Type ";
      $mail_header .= "name=\"$mail_Attachment_name\"\r\n";
      $mail_header .= "Content-Transfer-Encoding: base64\r\n";
      $mail_header .= "Content-Disposition: attachment; ";
      $mail_header .= "filename=\"$mail_Attachment_name\"\r\n\n";
      $mail_header .= "$file\r\n";
      $mail_header .= "--$num--";
      Send the email
        if( mail($mail_to, $mail_subject, $mail_content, $mail_header)  ){
          $html  = '<br><b>Thank you for contacting</b></b>';
        } else {
          $html = '<br><b>There was an error processing the Form.</b>';    
      fclose( $fp );
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PHP's default mail function has lots of pitfalls especially when dealing with MIME attachments. Use phpmailer ( ) , the standard Mailer Class API for PHP. Its very easy to setup and use.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.