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:

NFL Schedule said...

Thanks for the share. It was very interesting and informative. Keep posting such a kind of post on your blog.

Smart Framework BD said...

Great..I have been looking for this for so long...at I last after 2 days of frustration..I have reached in your site..& I got my solution.....Thanks...

But you make a mistake in for loop...it will be like this

for($x=0;$x<count($files);$x++) { $file = fopen($files[$x],"rb"); $data = fread($file,filesize($files[$x])); fclose($file); $data = chunk_split(base64_encode($data));

$message.= "Content-Type: {$file_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"; }

ROCKY said...

thank you for the correction :)

Anonymous said...

Thank you so much for this! I had been searching for days on getting the multiple upload to work!! :)

Post a Comment