Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 702
  • Last Modified:

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 );
1 Solution
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now