【问题标题】:contact form is blank; contact form reply goes to wrong address联系表格为空白;联系表格回复到错误的地址
【发布时间】:2016-05-03 08:48:32
【问题描述】:

我正在尝试制作一个无验证码、阻止垃圾邮件的联系表单。从用户的角度来看,它似乎可以正常工作,因为在单击发送按钮后会出现感谢页面。

问题 #1:收到的电子邮件是空白的,不包含已发送邮件的任何内容。在这里找到了一个听起来相似的帖子,但提供的建议并不适用:Contact form problem - I do receive messages, but no contents (blank page)

问题 #2:回复消息也显示在同一个收件箱中,而不是发送到表单用户的地址——在测试时,它是我自己的。

HTML:

<form method="post" action="submit.php">
 <p>Name:<br>
 <input type="text" name="Name" id="Name" /></p>

 <p>Phone:<br>
 <input type="text" name="Phone" id="Phone" /></p>

 <p>Email:<br>
 <input type="text" name="Email" id="Email" /></p>

 <p class="antispam">Leave this empty:<br />
 <input name="url" /></p>

 <p style="color:#06C;">Forward to:<br>
 <input type="text" name="Forwardto" id="Forwardto" /></p>

 <p>Message:<br />
 <textarea name="Message" rows="20" cols="20" id="Message"></textarea></p>

 <p><input type="submit" value="Send" class="submit-button" /></p>
</form>

PHP(删除了我的实际地址):

<?php

if(isset($_POST['url']) && $_POST['url'] == ''){

    $youremail = '----@----.com';

    $body = "This is the form that was just submitted:
    Name:  $_POST[name]
    Phone:  $_POST[phone]
    E-Mail: $_POST[email]
    Forward to: $_POST[forwardto]
    Message: $_POST[message]";

    if( $_POST['email'] && !preg_match( "/[\r\n]/", $_POST['email']) ) {
      $headers = "From: $_POST[email]";
    } else {
      $headers = "From: $youremail";
    }

    mail($youremail, 'Contact Form', $body, $headers );

}

?>

相关的 CSS:

<style type="text/css">
.antispam { display:none;}
</style>

是上面代码的问题。 . .还是可能发生其他事情?

【问题讨论】:

  • 您是否尝试过使用$_POST['Email'] 而不是$_POST['email'] - 大写“E”?
  • 你也应该打开错误报告,你会收到 php 通知你使用的 $_POST 索引是无效的
  • php 数组键区分大小写。您的所有 $_POST 键必须与表单中 name 的大小写匹配。您正在提交EmailPhone 等...但正在尝试访问emailphone 等...

标签: php forms contact-form


【解决方案1】:

当字段名称为 email(小“e”)时,您尝试访问 $_POST['email'] - 请尝试改用 $_POST['Email']

第二个问题 - mail() 函数的第一个参数是您希望将其发送到的电子邮件,即$_POST['Email']。现在您正尝试将其发送到硬编码的$youremail

【讨论】:

  • 错误的不仅仅是电子邮件变量。它们都会被错误地大小写。
  • 感谢大家的回复。这些更正解决了这两个问题。
  • 如果我帮助了你,请接受并支持我的回答。有关错误报告,请查看 here
  • @Mateusz:我之前确实尝试过,但它不会让我这样做,因为我没有最低要求的 15 声望。
【解决方案2】:

首先你需要关闭引用 $body = "这是刚刚提交的表单:"; 我说使用 $_REQUEST 而不是 $_POST 因为这个变量是一个超全局数组,用于收集使用 GET 和 POST 方法发送的数据。 我已经尝试过了,它有效 if (isset($_REQUEST['email'])) 在此处输入代码`{

      //Email information
      $admin_email = "myemail.co.uk";
      $subject = "This is the form that was just submitted";
      $email = $_REQUEST['email'];
      $comment = $_REQUEST['comment'];
      $full_name = $_REQUEST['full_name'];

      //send email
      mail($admin_email, "$subject", "message:" . $comment, "From:" . $email,     `enter code here`enter code here`$full_name);

       //Email response if needed
      echo "Thank you for contacting us!";
       }

      //if "email" variable is not filled out, display the form
       else  
        {
        enter code here`enter code here`?>
        enter code here`<p>Email us at <i>website name.com</i></p>
        enter code here`<?php
        enter code here`}
        enter code here`die();
        enter code here`?>

【讨论】:

    猜你喜欢
    • 2013-11-19
    • 2013-10-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2019-09-04
    • 1970-01-01
    相关资源
    最近更新 更多