Это комбинация нескольких скриптов и немного чтения. Я не добавил никакой обработки формы или тому подобного, но она позволяет использовать опцию для прикрепления нескольких файлов через одну кнопку ввода. Надеюсь, что это полезно для кого-то. Я уверен, что это нарушает все виды стандартов. Я знаю, что это работает в Chrome 31 и IE10.
Редактировать: работая с этим небольшим сценарием, я добавил форматирование HTML для сообщения и замену сообщения "спасибо".
<?php
if(isset($_POST['Submit'])) {
$email_to = "";
$email_subject = "";
$thankyou = "thanks.html";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
function died($error) {
echo "Sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
$requester_name = $_POST['requester_name']; // required
$requester_email = $_POST['requester_email']; // required
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message = "<html><body> \r\n";
$email_message .= "<table style=\"border: 1px #777 solid; font-family: Arial; font-size: 13px;\" cellpadding=\"7\"> \r\n";
$email_message .= "<tr><td style=\"background: #444; color:#fff;\"><strong>New Hire Form</strong></td><td style=\"background: #444; color:#fff;\">Requirements</td></tr>" . "\n";
$email_message .= "<tr><td style=\"background: #ccc;\"><strong>Requester Name: </strong></td><td style=\"background: #ddd;\">" .clean_string($requester_name). "</td></tr>" . "\n";
$email_message .= "<tr><td style=\"background: #ccc;\"><strong>Requester Email: </strong></td><td style=\"background: #ddd;\">".clean_string($requester_email). "</td></tr>" . "\n";
$email_message .= "</table> \r\n";
$email_message .= "</body></html>";
// multipart boundary
$email_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" . $email_message . "\n\n";
for($i=0;$i<count($_FILES['attachfile']['name']);$i++)
{
if($_FILES['attachfile']['name'][$i] != "")
{
//here you will get all files selected by user.
$name = ($_FILES['attachfile']['name'][$i]);
$tmp_name = ($_FILES['attachfile']['tmp_name'][$i]);
$type = ($_FILES['attachfile']['type'][$i]);
$size = ($_FILES['attachfile']['size'][$i]);
echo count($_Files['attachfile']) ;
echo $_FILES['attachfile']['name'][$i] ;
echo $_FILES['attachfile']['tmp_name'][$i] ;
echo $_FILES['attachfile']['type'][$i] ;
// Read the file content into a variable
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
// Close the file
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$headers .= 'From: '.$email_sender."\r\n". // Mail will be sent from your Admin ID
'Reply-To: '.$Email."\r\n" . // Reply to Sender Email
'X-Mailer: PHP/' . phpversion();
// headers for attachment
$headers .= "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\"";
@mail($email_to, $email_subject, $email_message, $headers);
?>
<script>location.replace('<?php echo $thankyou;?>')</script>
<?php
}
die();
?>