Nodral Posted January 17, 2011 Share Posted January 17, 2011 Hi all I'm trying to send an automated email of a zip file. However it seems to be just reading the files into the body of the mail rather than attaching a single zip archive containing several files. I receive the mails, but I effectively want a blank message body with a zip file attached. Please see code below, any ideas why this is not working. I'm a bit of a newbie and never used this before so any pointers would be good. <?php //Send Zip file as attachment //Set Email and attachment details $to = "me@123.com"; $from = "Visionnet <DO_NOT_REPLY>"; $subject = "Subject"; $message = "Please see attached file"; $fileatt = "wiki_zip.zip"; $fileatt_type = "application/zip"; $fileatt_name = "wiki_zip.zip"; //Read in the attachment $file=fopen($fileatt,'rb'); $data=fread($file,filesize( $fileatt)); fclose($file); //Add the MIME content $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the Headers for file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message = "This is a multipart 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"; //Base64 encode file data $data = chunk_split(base64_encode($data)); //Add attachment to message $message .= "--{mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; //Send message $ok = @mail($to, $subject, $message, $headers, "$from"); if($ok) { echo "mail sent"; } else { echo "mail failed"; } ?> Quote Link to comment Share on other sites More sharing options...
Rifts Posted January 17, 2011 Share Posted January 17, 2011 http://www.geekology.co.za/blog/2009/06/sending-emails-with-attachments-using-php-mail-function/ Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 17, 2011 Share Posted January 17, 2011 i don't like messing with all that email attachments base 64 encoding whatchamacallit stuff. of course, it's nice to know how it works, but I suggest that you use an existing class to handle all the ugly stuff. i use rmail: http://www.phpguru.org/downloads/Rmail/Rmail%20for%20PHP/ There is probably newer, but I can't imagine easier. for instance, here is example code to attach a file: $mail->addAttachment(new fileAttachment('example.zip')); wow! no content disposition mime boundary type whatisthat? Quote Link to comment Share on other sites More sharing options...
Nodral Posted January 17, 2011 Author Share Posted January 17, 2011 Cheers Guys If only I'd known about Rmail about 6 hours ago!!! lol Works a treat!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.