Emailing Attachments with PHP

Posted on 2005-04-01
Medium Priority
Last Modified: 2008-02-01
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 );
Question by:jdwan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 32

Accepted Solution

ldbkutty earned 2000 total points
ID: 13686091
PHP's default mail function has lots of pitfalls especially when dealing with MIME attachments. Use phpmailer ( http://phpmailer.sourceforge.net ) , the standard Mailer Class API for PHP. Its very easy to setup and use.

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question