You are here:

PHP5/Mail with attachment problems

Advertisement


Question
Hi. Can you help me with a bit of code for the mail function? My problem is that I can't seem to attach files correctly. Here is part of the code: $to="";
$from="R.-SM";
$subject="Cerere site";
$message="Nume: $_POST[nume]";
$headers="From: $from";
$semi_rand=md5(time());
$mime_boundary="==Multipart_Boundary_x{$semi_rand}x";
$headers.="\r\nMIME-Version: 1.0\r\n"."Content-Type: multipart/mixed;\r\n"."boundary=\"$mime_boundary\"";
$message=\"$mime_boundary\"\r\n"."Content-Type: text/plain; charser=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n".$message."\r\n\r\n";
$message.="--\"$mime_boundary\"\r\n";
$data=chunk_split(base64_encode(file_get_contents("site".$timp.".zip")));
$message.="Content-Type: application/octet-stream;\r\n"."name=\"site".$timp.".zip\"\r\n"."Content-Disposition: attachment;\r\n"."filename=\"site".$timp.".zip\"\r\n"."Content-Transfer-Encoding: base64\r\n\r\n".$data."\r\n\r\n";
$message.="--\"$mime_boundary\"\r\n";
(mail($to, $subject, $message, $headers)
More specifically, my problem is: the original attachment is sent as part of the message (is sent as text) and the entire message is sent as an attachment. I think it has something to do with the boundaries not being set properly, but I can't seem to figure out what the problem is. Any help will be very much appreciated. Bye.

Answer
Here's part of a mail function I've used successfully in the past, when I compare it to yours it looks like there should be more than one boundary when an attachment is involved:

     // Construct the message
     $headers.= "MIME-Version: 1.0\n";
     if ($attachments){
        $headers.= "Content-Type: multipart/mixed;\n\tboundary=\"----=_NextPart_000_THE_1ST_B0UNDARY\"\n";
     } else {
        $headers.= "Content-Type: multipart/alternative;\n\tboundary=\"----=_NextPart_000_THE_1ST_B0UNDARY\"\n";
     }
     $headers.= "X-Priority: $priority\n";
     $headers.= "X-Mailer: 1 2 Mime Mail v0.3\n";
     $headers.= "\n";
     $headers.= "This is a multi-part message in MIME format.\n";
     $headers.= "\n";
     $headers.= "------=_NextPart_000_THE_1ST_B0UNDARY\n";
     if ($attachments){
        $headers.= "Content-Type: multipart/alternative;\n\tboundary=\"----=_NextPart_001_THE_2ND_B0UNDARY\"\n";
        $headers.= "\n";
        $headers.= "------=_NextPart_001_THE_2ND_B0UNDARY\n";
     }
     $headers.= "Content-Transfer-Encoding: quoted-printable\n";
     $headers.= "\n";
     $headers.= wordwrap($text,76)."\n";
     if ($attachments){
        $headers.= "------=_NextPart_001_THE_2ND_B0UNDARY\n";
     } else {
        $headers.= "------=_NextPart_000_THE_1ST_B0UNDARY\n";
     }
     $headers.= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
     $headers.= "Content-Transfer-Encoding: base64\n\n";
     $headers.= "\n";
     $headers.= chunk_split(base64_encode($html))."\n";
     $headers.= "\n";

     // Attach the attachments to this mail
     if($attachments){
        $headers.= "------=_NextPart_001_THE_2ND_B0UNDARY--\n";
        $headers.= "\n";
        $attachments = is_array($attachments) ? $attachments : array($attachments);
        foreach($attachments as $atm){
           if (file_exists($atm)){
              $atmname = basename($atm);
              $afd= fopen ($atm, "r");
              $atmcontent = fread($afd,filesize($atm));
              fclose ($afd);

              $headers.= "------=_NextPart_000_THE_1ST_B0UNDARY\n";
              $headers.= "Content-Type: application/octetstream;\n\tname=\"$atmname\"\n";
              $headers.= "Content-Transfer-Encoding: base64\n";
              $headers.= "Content-Disposition: attachment;\n\tfilename=\"$atmname\"\n";
              $headers.= "\n";
              $headers.= chunk_split(base64_encode($atmcontent))."\n";
           }
        }
     }

     // End of the message
     $headers.= "------=_NextPart_000_THE_1ST_B0UNDARY--\n";

     // Actually send the mail
     if (mail($to,$subject,"",$headers,"-f $from")) {
        //echo "mail sent";
        return true;
     } else {
        //echo "mail not sent";
        return false;
     }

PHP5

All Answers


Answers by Expert:


Ask Experts

Volunteer


Robert Davis

Expertise

My focus in PHP has been calculations, reporting, database manipulation (MySQL), automated scripting, screen scraping, and tracking systems. A brief professional background is posted at http://businesscatalyst.info.

Experience

I've been programming in many languages for over 15 years, but the last several years I've focused on php. I've posted several tricks I've learned at goodfeelingplace.com.

Education/Credentials
I have a bachelors degree and a masters degree, both in electrical engineering from Arizona State University.

©2012 About.com, a part of The New York Times Company. All rights reserved.