大家晚上好,我写信是因为我无法将使用表单加载的文件附加到电子邮件中。我不明白在将其附加到文件夹之前是否必须保存....这是我的代码,邮件到达,但没有附件。有人告诉我我错在哪里?
$allegato=$_FILES['userfile']['tmp_name'];
$allegato_name=$_FILES['userfile']['name'];
$allegato_tipo=$_FILES['userfile']['type'];
$uploaddir = '/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$headers = 'From: '.$email.'' . "\r\n" .
'Reply-To: pir.stefania@tiscali.it' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
;
if ($_FILES["userfile"]["error"] > 0){
echo "Return Code: " . $_FILES["userfile"]["error"] . "<br>";
}else{
if (file_exists("uploads/" . $_FILES["userfile"]["name"])){
echo $_FILES["userfile"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["userfile"]["tmp_name"],
"uploads/" . $_FILES["userfile"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["userfile"]["name"];
}
}
if(is_uploaded_file($allegato)){
$file = fopen($allegato,'rb');
$data = fread($file, filesize($allegato));
fclose($file);
$data = chunk_split(base64_encode($data));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";
$msg .= "This is a multi-part message in MIME format.\n\n";
$msg .= "--{$mime_boundary}\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= $messaggio . "\n\n";
$msg .= "--{$mime_boundary}\n";
$msg .= "Content-Disposition: attachment;\n";
$msg .= " filename=\"{$allegato_name}\"\n";
$msg .= "Content-Transfer-Encoding: base64\n\n";
$msg .= $data . "\n\n";
$msg .= "--{$mime_boundary}--\n
}else{
$msg = $messaggio;
}
if (mail($destinatario, $oggetto, $msg, $headers)){
echo "<p>Mail inviata con successo!</p>";
}else{
echo "<p>Errore!</p>";
}
/精细脚本/
mail($destinatario, $oggetto, $messaggio, $headers) ;