填写表单时向管理员发送电子邮件

时间:2016-06-06 07:08:24

标签: php html forms phpmailer email-integration

网站来源:http://www.salefee.com/

我在网站上制作了一个表单,在填写表单后使用php向管理员发送消息。 我的html表格如下:

<html>
<body>
 <!-- Contact form -->
              <form autocomplete="on" action='./contact.php' method='post'>
              <div class="form-horizontal contact-form" role="form">
                <fieldset>
                  <legend>Contact</legend>
                  <div class="r1 row form-group">
                    <div class="c1 col-sm-6 col-md-6" style="float:left;">
                      <input class="form-control1" id="fname" name="fname" type="text" placeholder="First Name *" required autocomplete="on">
                    </div>
                    <div class="c2 col-sm-6 col-md-6" >
                      <input class="form-control1" id="lname" name="lname" type="text" placeholder="Last Name *" required autocomplete="on">
                    </div>
                  </div>
                  <div class="r2 row form-group">
                    <div class="c1 col-sm-6 col-md-6">
                      <input class="form-control2" id="email" name="email" type="email" placeholder="Email *" required autocomplete="on">
                    </div>
                    <div class="c2 col-sm-6 col-md-6">
                      <input class="form-control3" id="phone" name="phone" type="tel" placeholder="Phone" autocomplete="on">
                    </div>
                  </div>

                  <div class="r4 row form-group">
                    <div class="c1 col-md-12">
                      <input class="form-control4" id="subject" name="subject" type="text" placeholder="Subject *" required>
                    </div>
                  </div>
                  <div class="r5 row form-group">
                    <div class="c1 col-md-12">
                      <textarea class="form-control5" id="message" name="message" rows="10" placeholder="Message *" required></textarea>
                    </div>
                  </div>
                  <div class="r6 row form-group" style="text-align:center; margin-right:16%">
                    <div class="c1 col-md-12" >
                      <button class="btn btn-lg btn-danger submit-form-contact" type="submit">Send Message</button>
                    </div>
                  </div>
                </fieldset>
              </div>
              </form>
              <!-- End Contact form -->
</body>
</html>

我的contact.php文件如下:

<?php
if ($_POST) {
  $to_email = "info@salefee.com"; //Recipient email, Replace with own email here

  //check if its an ajax request, exit if not
  if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    $output = json_encode(array( //create JSON data
      'type'=>'error',
      'text' => 'Sorry Request must be Ajax POST.'
    ));
    die($output); //exit script outputting json data
  }

  //Sanitize input data using PHP filter_var().
  $user_fname   = filter_var($_POST["fname"], FILTER_SANITIZE_STRING);
  $user_lname   = filter_var($_POST["lname"], FILTER_SANITIZE_STRING);
  $user_email   = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
  $user_phone   = filter_var($_POST["phone"], FILTER_SANITIZE_NUMBER_INT);
  //$user_company = filter_var($_POST["user_company"], FILTER_SANITIZE_STRING);
  //$user_website = filter_var($_POST["user_website"], FILTER_SANITIZE_STRING);
  $subject      = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
  $message      = filter_var($_POST["message"], FILTER_SANITIZE_STRING);

  //additional php validation
  if (strlen($user_fname) < 3) { // If length is less than 3 it will output JSON error.
    $output = json_encode(array('type'=>'error', 'text' => 'First Name is too short or empty.'));
    die($output);
  }
  if (strlen($user_lname) < 3) { // If length is less than 3 it will output JSON error.
    $output = json_encode(array('type'=>'error', 'text' => 'Last Name is too short or empty.'));
    die($output);
  }
  if (!filter_var($user_email, FILTER_VALIDATE_EMAIL)) { //email validation
    $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email.'));
    die($output);
  }
  if (!filter_var($user_phone, FILTER_SANITIZE_NUMBER_FLOAT)) { //check for valid numbers in phone number field
    $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number.'));
    die($output);
  }
  if (strlen($subject) < 1) { //check emtpy subject
    $output = json_encode(array('type'=>'error', 'text' => 'Subject is required.'));
    die($output);
  }
  if (strlen($message) < 3) { //check emtpy message
    $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
    die($output);
  }

  //email body
  $message_body = "Name   : ".$user_fname." ".$user_lname."\r\nEmail  : ".$user_email."\r\nPhone  : ".$user_phone."\r\nCompany: ".$user_company."\r\nWebsite: ".$user_website."\r\n\r\n".$message;

  //proceed with PHP email.
  $headers = 'From: '.$user_fname.''."\r\n".
  'Reply-To: '.$user_email.''."\r\n".
  'X-Mailer: PHP/'.phpversion();

  $send_mail = mail($to_email, $subject, $message_body, $headers);

  if (!$send_mail)
  {
    //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
    $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
    die($output);
  } else {
    $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_fname.', thank you for your email.'));
    die($output);
  }
}
?>

问题是,点击提交后我没有收到任何电子邮件。提交后的网址变得像http://www.salefee.com/?fname=Test&lname=name&email=test%40email.com&phone=1234567890&subject=testsubject&message=testmessage我希望在用户点击提交按钮时收到电子邮件给所提到的电子邮件ID。请帮我这个代码。

提前致谢!!

1 个答案:

答案 0 :(得分:0)

如果服务器想要保存您的文件,那么您的PHP服务器就会出现问题,可能您没有安装PHP服务器或者无法读取.php文件或者有bade配置。

注意: 首先,您的表单标记必须包含操作链接,在提交和方法后重定向的位置,sumethimg如下:<form action='./contact.php' method='post'></form>,如果未填写,您的提交表单将在同一文件中提交。

其次,如果您没有使用AJAX提交,则需要删除contact.php中的这部分代码

//check if its an ajax request, exit if not
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    $output = json_encode(array( //create JSON data
      'type'=>'error',
      'text' => 'Sorry Request must be Ajax POST.'
    ));
    die($output); //exit script outputting json data
}