note 52215 added to function.base64-encode

From: Date: Sun, 24 Apr 2005 17:40:22 +0000
Subject: note 52215 added to function.base64-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-88499@lists.php.net to get a copy of this message
I had the need to send a Multi-part mime message. I spent a lot of time trying to correctly receive it. I finally worked out what I needed to do, and rather than keep it to myself - thought I would share it. I needed to send a text message and an attached file via email rather than providing a direct download link to a file and this is my solution. The main parts I had problem with was to correctly format the boundary in the header - then also in the body of the message. <?php $tempfile = '/full/path/to/file.zip'; $thisfile = 'file.zip'; // Encode the file ready to send it off $handle = fopen($tempfile,'rb'); $file_content = fread($handle,filesize($tempfile)); fclose($handle); $encoded = chunk_split(base64_encode($file_content)); // create the email and send it off $subject = "File you requested from RRWH.com"; $from = "scripts@rrwh.com"; $headers = 'MIME-Version: 1.0' . "\n"; $headers .= 'Content-Type: multipart/mixed; boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n"; $message = ' This is a multi-part message in MIME format. ------=_NextPart_001_0011_1234ABCD.4321FDAC Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hello We have attached for you the PHP script that you requested from http://rrwh.com/scripts.php as a zip file. Regards ------=_NextPart_001_0011_1234ABCD.4321FDAC Content-Type: application/octet-stream; name="'; $message .= "$thisfile"; $message .= '" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="'; $message .= "$thisfile"; $message .= '" '; $message .= "$encoded"; $message .= ' ------=_NextPart_001_0011_1234ABCD.4321FDAC-- '; // now send the email mail($email, $subject, $message, $headers, "-f$from"); ?> ---- Manual Page -- http://www.php.net/manual/en/function.base64-encode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52215 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52215&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52215&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52215&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52215&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52215&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52215&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88499) next »