我的PHP上传表单工作,但没有收到电子邮件


My PHP Upload Form Works But No Email Received

有人能看看这个极其简单的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>

更新

表格正在工作,但只收到封面说明,为什么其他字段没有通过电子邮件发送?

您的代码有一些问题:

  • 您将错误的参数传递给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"