Sending upload file with email notify

Oct 26, 2010 Author: LinuxAdmin

Sending email notify with stored content wants a good knowledge of email formats. On the first place in the basic stuff is "Content-type": needs to declare the content as "multipart/mixed".

After that the parts for the text and the upload file need to be within the defined limits. Every limit starts with begins with two dashes, followed by unique number. That number cannot attend in email part with the notiy.

It is useful to code the uploaded files with the function base64_encode() for safe sending. To split the sended files we can use the finction chunk_split().

This code will be used for upload_file.php that we write in the previous example PHP UPLOAD FORM .


$to = $_POST ['to'];

$from = $_POST ['from'];

$subject = $_POST ['subject'];

$comment = $_POST ['comment'];

$pr = $_FILES ['pr'];

$pr_pyt = $_FILES ['pr'] ['tmp_name'];

$pr_name = $_FILES ['pr'] ['name'];

$pr_size = $_FILES ['pr'] ['size'];

$pr_type = $_FILES ['pr'] ['type'];

//open, read and close of the file

$fp = fopen ( $pr_pyt, "rb" );

$file = fread ( $fp, $pr_size );

$fclose ( $fp );

//creating a series of border

$var = md5 ( time ());

$gr = "==Multipart_boundary_x {$var}x";

// coding of the information for safe Receiving
//and splitting the filename to 76 characters

$file = chunk_split (base64_encode ($file));

//define header

$head = "From: $from\n";

$head .= "MIME-Version: 1.0\n";

$head .= "Content-Type: multipart/mixed;
          charset=\"iso-8859-1"\n ";

$head .= "boundary=\"{gr}\"";

//Define of the subject

$subj = "This is the subject from
         some parts of MIME format \n\n";

$subj .= "-- {$gr}\n";

$subj .= "Content-Type: text/plain;

$subj .= "Content-Transfer-Encoding:

$subj .= "comment\n\n";

$subj .= "--{gr}\n";


$subj .= "Content-type; {pr_type};\n";

$subj .= "name=\"{pr_name}\"\n";

$subj .= "Content-Desposition:

$subj .= "filename=\"{pr_name\"\n";

$subj .= "Content-Transfer-Encoding:

$subj .= "$file\n\n";

$subj .= "--{$gr}";


$send = mail ($to, $subject, $subj, $head);
 if ($send)
 echo "The message has been send";


views 9801
  1. Add New Comment

February 26,2011 at 20:22:23
Waste coding