【问题标题】:My PHP Upload Form Works But No Email Received我的 PHP 上传表单有效,但没有收到电子邮件
【发布时间】:2014-12-26 23:02:57
【问题描述】:

有人可以看看这个非常简单的 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>

更新

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

【问题讨论】:

  • 帮自己一个忙,下载 PHPMailer。它会做你想做的一切,消除你的头痛。
  • 您在测试此代码的地方运行了 SMTP 服务?
  • 是的,它在它要保留的网页上。该网站实际上是一个设计网站,但是网页设计师无法做到,所以它落到了我的身上!
  • @yajakass 你想要一个指向该网站的实时链接吗?
  • 我无法从站点检查 SMTP 是否正在运行。

标签: php html forms file-upload webforms


【解决方案1】:

您的代码存在一些问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-12
    • 2013-04-26
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多