Link to home
Start Free TrialLog in
Avatar of trman
trman

asked on

PHP form processing, MIME types, email attachments for Entourage (for Mac)

I have created the below code which processes the attached HTML web form, and then sends the data to a recipient. I have tested it using Yahoo Mail, Gmail, Outlook Express (PC) and on Android, and the attachements show up as attachments.  

My problem is that the attachments involved, do not display correctly in the email program, Entourage for Macintosh. Instead, they show up as text inside the message, like this below:

"This is a multi-part message in MIME format.

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

The following person has applied online:

Name: tr
Phone: r
Email: tr
Address: t
Educational Level: r
Additional Info:
How they heard of us:


--==frontier
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document;
name="testhey.docx"
Content-Disposition: attachment;
Content-Transfer-Encoding: base64

UEsDBBQABgAIAAAAIQDd/JU3ZgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAI ooAAC
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAC0
VMtuwjAQvFfqP0S+Vomhh6qqCBz6OLZIpR9g7A1Y9Uv28vr7bg JEVQtBKuUSKVnvzOzsxIPR2pps
CTFp70rWL3osAye90m5Wso/JS37PsoTCKWG8g5JtILHR8PpqMNkESBl1u1SyOWJ44DzJOViRC h/
..."

PHP process code is below, and the apply-now.html (web form) is attached.

Thanks in advance!
<?php


   $to="obsurf@seniorssharing.org";
   $subject="Online Application Received";
   $from = stripslashes($_POST['fullname'])."<".stripslashes($_POST['email']).">";

   // generate a random string to be used as the boundary marker
   //$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
       $mime_boundary="==frontier";

   // now we'll build the message headers
   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";


   $fullname= stripslashes( $_POST['fullname']);
   $phone= stripslashes( $_POST['phone']);
   $address= stripslashes( $_POST['address']);
   $email= stripslashes( $_POST['email']);
   $education= stripslashes( $_POST['education']);
   $additional= stripslashes( $_POST['additional']);
   $reference= stripslashes( $_POST['reference']);
 $message="The following person has applied online: \n\n ";
 $message.="Name: $fullname \n";
 $message.="Phone: $phone \n";
 $message.="Email: $email \n";
 $message.="Address: $address \n";
 $message.="Educational Level: $education \n";
 $message.="Additional Info: $additional \n";
 $message.="How they heard of us: $reference \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";

  
   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 (file_exists($tmp_name)){

         if(is_uploaded_file($tmp_name)){
 	
      
            $file = fopen($tmp_name,'rb');
 	
            // read the file content into a variable
            $data = fread($file,filesize($tmp_name));

          
            fclose($file);
 	
           
            $data = chunk_split(base64_encode($data));
         }
 	
        
         $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";
      }
   }
 
   $message.="--{$mime_boundary}--\n";
   // now we just send the message
   if (@mail($to, $subject, $message, $headers))
         echo '<meta http-equiv="Refresh" content="0; URL=thanks.html">';    
    else
         echo "Failed to send. Please contact us at: 510.658.7003";

?>

Open in new window

apply-now.html
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

The code snippet contains the apply-now form, stripped of all of the non-moving parts.  It has the potential to upload as many as three files.  The action script, process.php, reads the form input controls and the uploaded files, and emails them to obsurf@seniorssharing.org.

I am going to suggest a small change in the application design.  Rather than sending the file attachments via email, save the file attachments on the server and simply send a URL link to the saved file attachments.  Not only will this cut down on the bloat of email traffic (and solve the Entourage issue), but it will enable you to keep the client information even if obsurf@seniorssharing.org loses an email message.

HTH, ~Ray
<form enctype="multipart/form-data" method="post" action="process.php" >
<input type="text" name="fullname" />
<input type="text" name="phone" />
<input type="text" name="education" />
<input type="text" name="email" />
<input type="text" name="address" />
<textarea name="additional"></textarea>
<textarea name="reference"></textarea>
<input type="file" name="file1">
<input type="file" name="file2">
<input type="file" name="file3">
<input type=button value="Apply Now!"
</form>

Open in new window

Avatar of trman
trman

ASKER

I like youir comment, Ray.  Sometimes you down a certain rode for so long you forget there are other paths...I will check with  my associates and see if this solution is acceptable.  Meanwhile, do you have specific code in mind?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial