有人可以看看这个非常简单的php文件上传表格并告诉我我做错了什么,它在网站上工作正常,甚至收到确认消息。但是,我的电子邮件地址没有收到任何电子邮件。这是我第一次尝试使用PHP表单。
<?php
if(isset($_POST['submit']))
{
//The form has been submitted, prep a nice thank you message
$output = '<h1>Your Application Has Been Received</h1>';
//Set the form flag to no display (cheap way!)
$flags = 'style="display:none;"';
//Deal with the email
$to = 'myemail.com';
$subject = 'Application';
$message = strip_tags($_POST['message']);
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: myemail.com\r\nReply-To: myemail.com";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $name, $email, $headers);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>
<body>
<?php echo $output; ?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<label for="name">Name</label><input type="name" name="name" id="name"></p>
<p>
<label for="phone">Telephone</label> <input type="phone" name="phone" id="phone"></p>
<p>
<label for="email">Email</label> <input type="email" name="email" id="email"></p>
<p>
<label for="message">Cover Note</label> <textarea name="message" id="message" cols="20" rows="8"> </textarea></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>
更新
表单正在运行,但只收到Cover Note,为什么其他字段不通过电子邮件发送?
答案 0 :(得分:0)
您的代码存在一些问题:
您将错误的参数传递给mail()
。这一行:
mail($to, $subject, $name, $email, $headers);
应该是:
mail($to, $subject, $message, $headers);
您没有存储所有表单字段:
您需要从$_POST
检索所有表单字段并将其合并到您的邮件中。也许是这些方面的事情:
$message = "Name: " . $_POST['name'] . "\n";
$message .= "Phone: " . $_POST['phone'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Message:\n\n" . strip_tags($_POST['message']);
就个人而言,我会根据白名单检查文件类型,并仅对未知类型使用application/octet-stream
。例如:
$type = $_FILES['file']['type'];
if (!in_array ($type, array ('text/plain', 'image/png', 'audio/wav'))) {
$type = 'application/octet-stream';
}
然后在邮件正文中设置Content-Type: $type; name="$filename"
。