dongqiao6730 2016-06-06 07:08
浏览 38
已采纳

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

Website source:http://www.salefee.com/

I have made a form on website to send a message to admin after filling the form(using php). My html form is as given below:

<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>

My contact.php file is as given below:

<?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."
Email  : ".$user_email."
Phone  : ".$user_phone."
Company: ".$user_company."
Website: ".$user_website."

".$message;

  //proceed with PHP email.
  $headers = 'From: '.$user_fname.''."
".
  'Reply-To: '.$user_email.''."
".
  '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);
  }
}
?>

The problem is that after clicking submit I am not getting any email.The URL after submitting becomes like http://www.salefee.com/?fname=Test&lname=name&email=test%40email.com&phone=1234567890&subject=testsubject&message=testmessage I want to recieve an email when user click submits button to the mentioned email id. Please help me with this code.

Thanks in advance!!

  • 写回答

1条回答 默认 最新

  • dtoaillwk759656786 2016-06-06 07:28
    关注

    If server wants to save your file, there is problem with your PHP server, probably you have no PHP server installed or cant read .php files or has bade configuration.

    NOTE: First, your form tag must including action link where to redirect after submit and method, sumethimg like this : <form action='./contact.php' method='post'></form>, if this is not filled, your submit form will be submitted in same file.

    Second, if you are not submitting with AJAX you need to remove this part of code in 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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面