我一直在尝试为用户创建一个html表单,使用php mail函数发送带附件的邮件。邮件成功发送但没有附件。当我试图检查正在上传的文件时,我似乎没有得到用户选择的文件。以下是我的HTML代码
<form name="message" id="message" method="post" action="mail.php" enctype="multipart/form-data">
<input type="text" class ="text-class" id="name" name="name" placeholder="Name*" required/>
<input type="email" class ="text-class" placeholder="Email*" name="Email" id ="Email" required/>
<textarea class ="text-class" placeholder="Message*" name="Message" id="Message" required></textarea>
<div class="captcha">
<div class="row">
<div class="4u">
<input class ="text-class" type="text" value="2 + 3 is ?" name="question" readonly/>
</div>
<div class="8u">
<input class ="text-class" type="text" placeholder="Answer*" name="captcha" id="captcha" required/>
</div>
</div>
</div>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="attachment" id="attachment"/>
<input type="submit" name="Submit" value="Send" class="submit"/>
</form>
mail.php
<?php
if(isset($_POST['Submit']) && ($_POST['Submit']) == 'Send' )
{
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["Email"]==""||$_POST["Message"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['Email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$message = $_POST['Message'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
if((empty($_POST['attachment'])) || (empty($_FILES['attachment']))){
echo "No attachement!";
}
else{
//file is attached, process it and send via mail
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
mail("mail@test.com", "Mail from my website", $message, $headers);
echo "Your message has been received by us ! Thank you for contacting us";
echo "<script>
alert('Your message has been sent!');
</script>";
}
exit;
}
}
}else{
echo "Not submitted";
}
?>
我收到了“No attachment”消息,因为我似乎没有从html表单中获取所选文件。请有人告诉我哪里出错了?