note 52215 deleted from function.base64-encode by nlopess
| From: | nlopess@php.net | Date: | Sat, 01 Sep 2007 16:26:50 +0000 |
| Subject: | note 52215 deleted from function.base64-encode by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-130047@lists.php.net to get a copy of this message | ||
Note Submitter: RRWH.com
----
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");
?>