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
boundary="==Multipart_Boundary_x22107cafc856a68ba80ff666963b34edx"

This is a multi-part message in MIME format.

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

From:


--==Multipart_Boundary_x22107cafc856a68ba80ff666963b34edx
Content-Type: application/msword;
name="companies_dissertation.doc"
Content-Disposition: attachment;
filename=""
Content-Transfer-Encoding: base64

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAKgAAAAAAAAAA
EAAALAAAAAEAAAD+////AAAAACkAAAD/////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////s
pcEANUAJBAAA+BK/AAAAAAAAMAAAAAAABgAAqAkAAA4AYmpias8yzzIAAAAAAAAAAAAAAAAAAAAA
AAAOBBYALhAAAK1YAACtWAAAqAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAA
AAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAAAAAAAAAAAAIgAAAAAABoCAAAAAAAAGgIAABoC
AAAAAAAAGgIAAAAAAAAaAgAAAAAAABoCAAAAAAAAGgIAABQAAAAAAAAAAAAAAC4CAAAAAAAAtgUA

...
--==Multipart_Boundary_x22107cafc856a68ba80ff666963b34edx--

//message ends

Could anyone help me, please?
Cheers
cycleFun

This is my code:

    <?php
    /* ------------------------------------------------------
    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
    $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

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

    // 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
             if(is_uploaded_file($tmp_name))
             {

                // 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
                fclose($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
    $message.="--{$mime_boundary}--\n";


    /* 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.