|
Here
comes the meat and potatoes. We set our Content-Type to image/jpg
along with the name of the file being sent, pretty straightforward
here, image/jpg tells the client we're sending this as a jpg
file and it has the name regal_004.jpg. We then insert a blank
line and here comes some tricky stuff.
We
run 2 functions on our $file string. Base64_encode and chunk_split.
Base64_encode basically just transforms our string of file
information into something that a mail client can more easily
read or as PHP.net puts it "This encoding is designed to make
binary data survive transport through transport layers that
are not 8-bit clean, such as mail bodies.".
We
then call chunk_split on that base64 encoded string. If you
were to var_dump the base64 string you'd see it go on forever
and ever, what chunking does is break it up after so many
characters so its easier for mail agents to read it. It defaults
to 76 characters so after every 76 characters its going to
break the line by adding \r\n to it. That's basically all
it does. So instead of a string like:
Skjdf89fg98dfg98ad898989h98dgr98dfgu98dfug98dfug8d998u9df98udfg98udfg98udfg98udf9g8udf9g8udfg9df8gudf9g8u9duf
We get
Asdlfkjsadlfkjsakldfj
Ase9435r90r90fgg98
34098340984949484
40930938503948588
Try
it yourself
$string = "sdaflk;jasdfl;kjsadfl;jasljsdflkjsdf893u4tr89u8sduf89us98dfu98sduf98u8fu89sudf9usdfsdu89sdufsdf8fus89fusdfu8";
$new_string = chunk_split($string);
var_dump($new_string);
|