基于php和html的电子邮件联系人表单不起作用

基于php和html的电子邮件联系人表单不起作用,php,email,contact-form,Php,Email,Contact Form,我正在用templateforest的模板创建一个网站。我有一个HTML联系人表单,集成在网站底部,它与提交过程的PHP文件相关 下面是HTML部分的代码: <!--contact section start--> <div class="section section-contact" id="contact"> <div class="container"> <div class="row"> &

我正在用templateforest的模板创建一个网站。我有一个HTML联系人表单,集成在网站底部,它与提交过程的PHP文件相关

下面是HTML部分的代码:

<!--contact section start-->
<div class="section section-contact" id="contact">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <!--tittle start-->
                <div class="tittle">
                    <h1>
                        <a name="contactar"><span>10</span></a>
                        Contactar</h1>
                    <h2>
                        <span> Si deseas ponerte en contacto conmigo por favor, rellena el siguiente formulario o si lo prefieres utiliza los datos de contacto facilitados al pie de esta página.</span>
                    </h2>
                </div>
                <!--tittle end-->
                <div class="contact-row">
                    <form role="form" class="form-horizontal">
                        <div class="row form-group">
                            <div class="col-sm-4 col-xs-12">
                                <input type="text" name="name" placeholder="Name" class="form-control">
                            </div>
                            <div class="col-sm-4 col-xs-12">
                                <input type="text" name="email" placeholder="Email" class="form-control">
                            </div>
                            <div class="col-sm-4 col-xs-12">
                                <input type="text" name="subject" placeholder="Subject" class="form-control">
                            </div>
                        </div>
                        <div class="row form-group">
                            <div class="col-xs-12">
                                <textarea name="comments" placeholder="Comments" class="form-control" rows="10" cols="30" id="msg" name=""></textarea>
                            </div>
                        </div>
                        <div class="row form-group text-center">
                            <div class="col-xs-12">
                                <button type="submit" id="submit" class="view-btn">Contactar con Patricia</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<!--contact section end-->

10
接触器
如果是有利于承包商的合同,则应在合同日期前提供合同便利设施。
帕特里夏接触器
这是sendmail.php文件中的代码:

<?php
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['comments']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $mailTest = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $mailTest, $val ) ) {
      exit;
    }
  }

$headers = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Reply-To: ' . $_POST["email"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

  mail( "info@example.com", $_POST['subject'], $_POST['comments'], $headers );
  //  Replace with your email 
}
?>

显然,我已经更换了“info@example.com“使用有效的电子邮件。。。但它不起作用

我一直在寻找类似的代码,但我不是很擅长php,所以我找不到解决方案

当我在网站上按下“提交”按钮时,它只会重新加载整个页面。。。就这些。。。没有错误消息,但也没有发送电子邮件

服务器的php版本是5.4

2015年5月13日编辑:

这是我所做的更改后的PHP文件代码,正如建议的那样:

<?php
//this is to activate error messages
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['comments']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $mailTest = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $mailTest, $val ) ) {
      exit;
    }
  }

$headers = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Reply-To: ' . $_POST["email"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail( "info@example.com", $subject, $comment, $headers )){
   echo "Mail sent, it's out of your hands now and it did its job.";
}

else{
   echo "There was an error, check your logs.";
}
  //  Replace with your email 
}
?>

看到你发布的代码,我提交了以下内容并摘自我的评论

  • 如果未显式使用POST方法,则表单默认为GET方法
您使用的是POST数组,因此将表单设置为POST方法

<form method="post" role="form" class="form-horizontal">
      ^^^^^^^^^^^^^
旁注:错误报告只应在暂存阶段进行,而不应在生产阶段进行


脚注:

<?php
//this is to activate error messages
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['comments']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $mailTest = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $mailTest, $val ) ) {
      exit;
    }
  }

$subject = $_POST['subject'];
$comment = $_POST['comments'];

$headers = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Reply-To: ' . $_POST["email"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail( "info@example.com", $subject, $comment, $headers )){
   echo "Mail sent, it's out of your hands now and it did its job.";
}

else{
   echo "There was an error, check your logs.";
}
  //  Replace with your email 
}
?>
您不应该像这样传递POST数组:

<!--contact section start-->
<div class="section section-contact" id="contact">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <!--tittle start-->
                <div class="tittle">
                    <h1>
                        <a name="contactar"><span>10</span></a>
                        Contactar</h1>
                    <h2>
                        <span> Si deseas ponerte en contacto conmigo por favor, rellena el siguiente formulario o si lo prefieres utiliza los datos de contacto facilitados al pie de esta página.</span>
                    </h2>
                </div>
                <!--tittle end-->
                <div class="contact-row">
                    <form role="form" class="form-horizontal" action="sendmail.php">
                        <div class="row form-group">
                            <div class="col-sm-4 col-xs-12">
                                <input type="text" name="name" placeholder="Name" class="form-control">
                            </div>
                            <div class="col-sm-4 col-xs-12">
                                <input type="text" name="email" placeholder="Email" class="form-control">
                            </div>
                            <div class="col-sm-4 col-xs-12">
                                <input type="text" name="subject" placeholder="Subject" class="form-control">
                            </div>
                        </div>
                        <div class="row form-group">
                            <div class="col-xs-12">
                                <textarea name="comments" placeholder="Comments" class="form-control" rows="10" cols="30" id="msg" name=""></textarea>
                            </div>
                        </div>
                        <div class="row form-group text-center">
                            <div class="col-xs-12">
                                <button type="submit" id="submit" class="view-btn">Contactar con Patricia</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
mail( "info@example.com", $_POST['subject'], $_POST['comments'], $headers );
你要让自己接受XSS注射

您应该使用预先分配的变量

$subject = $_POST['subject'];
$comment = $_POST['comments'];
...

mail( "info@example.com", $subject, $comment, $headers );


XSS注入文章:


编辑:

<?php
//this is to activate error messages
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['comments']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $mailTest = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $mailTest, $val ) ) {
      exit;
    }
  }

$subject = $_POST['subject'];
$comment = $_POST['comments'];

$headers = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Reply-To: ' . $_POST["email"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail( "info@example.com", $subject, $comment, $headers )){
   echo "Mail sent, it's out of your hands now and it did its job.";
}

else{
   echo "There was an error, check your logs.";
}
  //  Replace with your email 
}
?>

如果未显式使用POST方法,则默认获取表单。您使用的是POST数组,因此将表单设置为POST方法。除非你有一些JS/Ajax,否则你不会向我们展示其中有一个或POST方法。因此,就其本身而言,我无法将此作为“答案”提交。不知道这是否是您的完整代码。“我在templateforest买了一个模板,但当问作者时,他从未给过我任何答案。”-真糟糕,不是吗?我已经回答了,所以不要太喜欢他们;-)我在并行目录中有几个.js文件/然而,没有任何人似乎与邮件服务有关。。。我是否可以在所有代码中查找任何通用代码,以查找是否有一个?我将在这里发布JS文件的完整代码以提供帮助。这是一个引导程序吗?是的,它是。。。我想这是twitter bootstrapLet的问题,看看OP是否对此做出回应。谢谢你的回答!我是这种纸条的另一个读者,所以我不确定你们在谈论帖子的时候是什么意思。无论如何,我正在添加和替换您提到的代码,我将在几分钟后发布结果。。。。编辑:还是没有运气。。。未显示错误且未收到电子邮件:(@DanielMartinez不客气。现在,此
已丢失
method=“post”
如果不使用它,就好像您使用的是使表单失败的
。这是否意味着通过缺少的POST方法更改GET方法可以解决问题?还是由于.js文件损坏,我仍然会失败?@DanielMartinez请先尝试这样做,添加
method=“POST”
添加到概述的表单标记。它肯定需要一个POST方法。
<?php
//this is to activate error messages
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['comments']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $mailTest = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $mailTest, $val ) ) {
      exit;
    }
  }

$subject = $_POST['subject'];
$comment = $_POST['comments'];

$headers = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Reply-To: ' . $_POST["email"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail( "info@example.com", $subject, $comment, $headers )){
   echo "Mail sent, it's out of your hands now and it did its job.";
}

else{
   echo "There was an error, check your logs.";
}
  //  Replace with your email 
}
?>