Hi All,

I am new to PHP but I have to do a coursework: developing PHP email application.
I am having problems with the attachment part. The code seems to be working but displays the attachment in the message body in a weird format:

//message body starts

This is a multi-part message in MIME format.

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


Content-Type: application/msword;
Content-Disposition: attachment;
Content-Transfer-Encoding: base64



//message ends

Could anyone help me, please?

This is my code:

    /* ------------------------------------------------------
    mm564_lib.php script
     /* ------------------------------------------------------

    /* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
    $email = $HTTP_POST_VARS['recipient'];
    $subject = $HTTP_POST_VARS['subject'];
    $message = $HTTP_POST_VARS['messagebox'];

    // Generate a random string to be used as the boundary marker

    // Building message headers
    $headers = "From: $from\r\n" .
               "MIME-Version: 1.0\r\n" .
               "Content-Type: multipart/mixed;\r\n" .

    // Starting the message body
    $message = "From: " . $_POST['name'] . "\r\n";   
    $message = "This is a multi-part message in MIME format.\n\n" .
               "--{$mime_boundary}\n" .
               "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
               "Content-Transfer-Encoding: 7bit\n\n" .
               $message . "\n\n";

    // Processing the uploaded files
    foreach($_FILES as $userfile)
          // store the file information to variables for easier access
          $tmp_name = $userfile['tmp_name'];
          $type = $userfile['type'];
          $name = $userfile['name'];
          $size = $userfile['size'];

          // if the upload succeded, the file will exist
          if (file_exists($tmp_name))

             // check to make sure that it is an uploaded file and not a system file

                // open the file for a binary read
                $file = fopen($tmp_name,'rb');

                // read the file content into a variable
                $data = fread($file,filesize($tmp_name));

                // close the file

                // now we encode it and split it into acceptable length lines
                $data = chunk_split(base64_encode($data));

             // Inserting a boundary to indicate that the attachment is started
             $message .= "--{$mime_boundary}\n" .
                "Content-Type: {$type};\n" .
                " name=\"{$name}\"\n" .
                "Content-Disposition: attachment;\n" .
                " filename=\"{$fileatt_name}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data . "\n\n";

    // Closing mime boundary that indicates the last of the message

    /* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) 
      echo "<h4>Invalid email address</h4>";
      echo "<a href='javascript:history.back(1);'>Back</a>";
    } elseif ($subject == "") {
      echo "<h4>No subject</h4>";
      echo "<a href='javascript:history.back(1);'>Back</a>";

    /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
    elseif (mail($email,$subject,$message, $headers)) 
      echo "<h4>Thank you for sending email</h4>";
    } else {
      echo "<h4>Can't send email to $email</h4>";


From a quick look, this jumps out.

$email = $HTTP_POST_VARS['recipient'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['messagebox'];

$HTTP_POST_VARS has been replaced as far as I know. Use $_POST

I was just looking into this for work I'm doing. Small world.

Hi tinymark,

thank for the reply. I tried your solution, it works but I still get the attachment encoded in the body part.

Do you mean the code for an encoded exe file is showing up? I'm a little confused.
Do you have a form that submits files to be uploaded and then emailed somewhere?

I have an HTML form with sender, subject, message parts plus the attachment part.
When I send an email to my email address I receive it, I can see that the file is attached to the mail but the attachment appears on the message as an encoded component as it is shown above (see opening thread).

I'm closing in on it. I know its the way you are setting up your message. Take everything out that is variable and hard-code it first. Does that work? I'm missing too many pieces of the puzzle (assets to test with).

When I aim it back towards what I'm doing with attachment and emails the code splits like crazy from yours.

Have a look at http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php. You will notice the differences.

I have the crazy nonsense not showing up and my code gives me an attachment. I lose it when I'm picking up things from the form. I'm looking at how I'm serialized the data that I'm getting. All those lovely %40 for @ things.