Sending email with attachment

I have the following script to send a PDF attachment based on one from Sitepoint. It works when I put my email address in but my client gets the attachment included as part of the email. Presumably it doesn’t detect the boundary or something. Can anyone see if I’ve missed anything please? Ta


    if ( isset($_POST['email']) && isset($_POST['Submit']) ) {

      $email = $_POST['email'];
      $fname = $_POST['fname'];
      $lname = $_POST['lname'];

      if ( !empty($email) ) {

        $fileatt = getcwd()."/docs/recipe.pdf";
	    if ( !file_exists($fileatt) ) die('no recipe');

        $to      = $email;
        $from    = "Name <enquiries@domain.net>";
        $subject = "Recipe of the month";
        $message = "Here is your recipe of the month.\
\
" .
          "We hope you enjoy this.\
\
" .
          "http://www.domain.net\
";
        $headers = "From: {$from}\
" .
          "X-Mailer: PHP/".phpversion()."\
";

        $fileatt_type = "pdf";
        $fileatt_name = "recipe.pdf";

        $file = fopen($fileatt, 'rb');
        $data = fread($file, filesize($fileatt));
        fclose($file);

        $mime_boundary = md5(time());
  
        $headers .= "MIME-Version: 1.0\
" .
          "Content-Type: multipart/mixed; boundary=\\"{$mime_boundary}\\"";

        $message = "This is a multi-part message in MIME format.\
\
" .
          "--{$mime_boundary}\
" .
          "Content-Type: text/plain; charset=\\"iso-8859-1\\"\
" .
          "Content-Transfer-Encoding: 7bit\
\
" .
          $message . "\
\
";

        $data = chunk_split(base64_encode($data));

        $message .= "--{$mime_boundary}\
" .
          "Content-Type: {$fileatt_type}; name=\\"{$fileatt_name}\\"\
" .
           "Content-Transfer-Encoding: base64\
\
" .
           $data . "\
--{$mime_boundary}--\
";

        if ( @mail($to, $subject, $message, $headers) ) {
          echo "<p>Thank you for signing up for our free monthly recipe.\
" .
            "Your first recipe has been sent to you at {$email}. If it does not arrive please " .
            "check your spam folder first and if is not there please contact us to " .
            "let us know.</p>\
" .
            "<p>We will send further recipes on a regular - roughly monthly - basis.</p>\
";

          # update the list of downloaders
          $fh = fopen("adlists/emails.csv", "a+");
          $record = $fname.",".$fname." ".$lname.",".$email."\
";
          fwrite($fh, $record);
          fclose($fh);

        } else {
          echo "<p>The email could not be sent. Sorry, please try again later.</p>";
        }

      } else {
        echo "<p class=\\"error\\">You have not entered your email address!</p>\
" .
          "<p><a href=\\"test.php\\">Return.</a>";
      }
    }

When sending attachments, I send across a Content-Disposition line as well as the Content-Type one.

$TotalEmail .= “Content-Disposition: attachment; filename=\”" . $Filename . “.pdf\”

";

Thanks TimIgoe. I’ll try that. I take it that goes in the section before the data. Other than that is it important the order?

in my case, I’ve got it after Content-Transfer-Encoding: base64 and before the data.

Cool, thanks Tim