Wednesday, September 21, 2011

Multiple Attachment php script--Attach any file type like images, pdf, word document, excel, txt etc



Multiple file attachment in single script.
Just include the files to be sent in array and send it.
the script is working all fine.
you can edit the script and make some changes if only one file is attached.




// Read POST request params into global vars
// FILL IN YOUR EMAIL
$fileatt="";
$fileatt1="";
$fileatt = $_FILES['attachment']['tmp_name'];
$fileatt_type = $_FILES['attachment']['type'];
$fileatt_name = $_FILES['attachment']['name'];

$fileatt1 = $_FILES['attachment2']['tmp_name'];
$fileatt_type1 = $_FILES['attachment2']['type'];
$fileatt_name1 = $_FILES['attachment2']['name'];


$to="rakeshroy02@gmail.com";
$files = array("$fileatt","$fileatt1");
$filenames = array("$fileatt_name","$fileatt_name1");

// email fields: to, from, subject, and so on
$from = "rakeshroy02@gmail.com";
$subject="File attachment";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

// multipart boundary
$message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x $file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {$fileatt_type};\n". " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}

$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
$errorMsg="Your email has been sent, we will respond shortly.";
} else {
$errorMsg ="Mail was not sent.Please try again.";
}


Enjoy the script.

4 comments:

Post a Comment