如何在html联系表单中的提交按钮上配置电子邮件地址

时间:2016-02-23 15:01:33

标签: php html

我无法在这段代码中的提交按钮上配置我的电子邮件地址:

<div id="contact" class="spacer">

<div class="container contactform center">
<h2 class="text-center  wowload fadeInUp">Get in touch with us</h2>
  <div class="row wowload fadeInLeftBig">
      <div class="col-sm-6 col-sm-offset-3 col-xs-12">
        <form class="cmxform" id="commentForm" method="post" action="email.php">
        <fieldset>
        <input type="text" placeholder="Subject" id="csubject" name="subject" minlength="2" type="text" required>
        <input type="text" placeholder="Email" id="cemail" type="email" name="email" required>
        <textarea rows="5" placeholder="Message" id="ccomment" name="comment" required></textarea>
        <input class="submit  btn btn-primary" type="submit" value="Submit">
        </fieldset>
    </form>
      </div>
  </div>

我尝试将其链接到php页面(email.php),但它说服务器错误。我不知道该怎么办。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

要发送电子邮件,您需要mail()函数http://php.net/manual/en/function.mail.php

使用您的代码,您需要将提交命名为name="submit"然后在action.php文件中,您必须编写类似这样的内容(或在您拥有此代码的同一个php中):

if(isset($_POST["submit"]))
{
//Remember to do input validations
$subject = $_POST["subject"];
$from = $_POST["email"];
$comment = $_POST["comment"];
$body = "User with email $from send the next comment: $comment";


//then here you set your email

$to = "myemail@email.com"; //change this
mail($to,$subject,$body);
}

这只是为了解释一些基本用法并设置你需要的变量,我建议你也阅读PHPmailer类