Multiple document Attached Mail in PHP
Today Discussed Multiple document attached mail in PHP simple way. Normal core php form designed style css used and one or more file upload included form and most important once form tag include enctype="multipart/form-data" is type code and mail code follows .
Multiple document Attached Mail in PHP |
<form class="form-inline" role="form" method="post" enctype="multipart/form-data">
<input type="file" value="" id="file1" name="file1" ">
<input type="file" value="" id="file2" name="file12" ">
</form>
PHP Code:
$folder = "fileupload/";
$temp = explode(".", $_FILES["file1"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
$db_path = "$folder" . $newfilename;
$listtype = array(
'.doc' => 'application/msword',
'.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'.rtf' => 'application/rtf',
'.pdf' => 'application/pdf');
if (is_uploaded_file($_FILES['file1']['tmp_name'])) {
if ($key = array_search($_FILES['file1']['type'], $listtype))
$file = move_uploaded_file($_FILES['file1'] ['tmp_name'], "$folder" . $newfilename);
}
else {
// echo "File Type Shosuld Be .Docx or .Pdf or .Rtf Or .Doc";
}
function multi_attach_mail($to, $subject, $message, $senderMail, $senderName, $files) {
$from = $senderName . " <" . $senderMail . ">";
$headers = "From: $from";
// 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 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if (count($files) > 0) {
for ($i = 0; $i < count($files); $i++) {
if (is_file($files[$i])) {
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i], "rb");
$data = @fread($fp, filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"" . basename($files[$i]) . "\"\n" .
"Content-Description: " . basename($files[$i]) . "\n" .
"Content-Disposition: attachment;\n" . " filename=\"" . basename($files[$i]) . "\"; size=" . filesize($files[$i]) . ";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
//send email
$mail = @mail($to, $subject, $message, $headers, $returnpath);
//function return true, if email sent, otherwise return fasle
if ($mail) {
return TRUE;
} else {
return FALSE;
}
}