base64_encode



   

I had the need to send a Multi-part mime message. I spent a lot oftime trying to correctly receive it.  I finallyworked 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 emailrather than providing a direct download link to a file and this ismy solution.

The main parts I had problem with was to correctly format theboundary in the header - then also in the body of themessage.

<?php
     
$tempfile
= '/full/path/to/file.zip';
$thisfile ='file.zip';

// Encode the file ready tosend 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 sendit off

$subject = "File you requestedfrom RRWH.com";
$from= "[email protected]";
$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 fromhttp://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 theemail
mail($email,$subject, $message,$headers, "-f$from");

?>



<?
//get the base64 encodedimage
$handle= fopen($tempfile,'rb');
$file_content =fread($handle,filesize($tempfile));
fclose($handle);
$encoded =chunk_split(base64_encode($file_content));

//then echo to browseras:

echo '<imgsrc="data:image/png;base64,'.$encoded.'">';
?>

你可能感兴趣的:(base64_encode)