Sending of files attachem

The essence of the mechanism of sending of files consists in the text message that the letter will consist of several parts which borders are designated by the unique signature. She also allows you to put some files to the letter and to not mix their contents during data transfer.


I have written a class (more precisely if to be fair is a standard decision of a similar problem. You can find the same class on php.spb.ru. I did not begin to copy it  without changes, and have a little processed creatively.


So an initial code of a class with comments:



<?

class multi_mail

{

var $from; // the Sender

var $to; // the Addressee

var $headers; // Heading of the message

var $body; // the Body of the message



function multi_mail () // the Designer of a class

// We spend initialization of variables

{

$this-> from = " ";

$this-> to = " ";

$this-> body = " ";

$this-> headers = array ();

$this-> subject = " ";

}


// The file is attached

function attach_file ($file_name = " ", // the Name of a file

$file_content, // Contents of a file

$encoding_type = "application/octet-stream"

// Type of the coding of the data.

)

{

$this-> headers [] = array (// It is written down the heading information.

"name" => $file_name,

"content" => $file_content,

"encode" => $encoding_type

);

}


function build_letter ($header)

// We build the Part of the letter, whether it be attachennyj a file or a plain text

{

$letter = $header ["content"];

if ($header ["encode"]! = "text/plain"):

$letter = chunk_split (base64_encode ($letter));

$encoding = "base64";

else:

$encoding = $header ["encode"];

endif;

return " content-type: ". $header ["encode"].

($header ["name"]? ".; name = " ". $header ["name"] "." ":" ")

. " rncontent-transfer-encoding:

$encodingrnrn$lettern ";

}


function set_multipart_mail () // we Collect the letter from isolated parts

{

$boundary = ' b '.md5 (uniqid (time));

// We create the unique number serving indetifikatorom for chati of the letter


$multipart = " content-type: multipart/mixed;

boundary = $boundarynnthis is a mime encoded letterrnrn - $boundary ";

for ($step = sizeof ($this-> headers)-1; $step> =0; $step-)

{

$multipart. = "rn." $this-> build_letter ($this-> headers [$step]). " - $boundary ";

// We insert contents a boundary labels

}

return $multipart. = " - rn ";

}


function get_full_message ()

// We insert a body of the letter (a text stuffing) and all files

// On an output{exit} it is received full pismo (one big line:)))

{

$mime = " ";

if (! empty ($this-> from)):

$mime. = " from: ". $this-> from. "rn";

endif;

if (! empty ($this-> body)):

$this-> attach_file (" ", $this-> body, "text/plain");

$mime. = " mime-version: 1.0rn ". $this-> set_multipart_mail ();

endif;


return $mime;

}



function send_mail () // Actually posyl letters

{

$mime = $this-> get_full_message (false);

mail ($this-> to, $this-> subject, " ", $mime);

}

}

?>