dongqiao6730 2016-06-05 23:08
浏览 39
已采纳

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

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:

  1. <html>
  2. <body>
  3. <!-- Contact form -->
  4. <form autocomplete="on" action='./contact.php' method='post'>
  5. <div class="form-horizontal contact-form" role="form">
  6. <fieldset>
  7. <legend>Contact</legend>
  8. <div class="r1 row form-group">
  9. <div class="c1 col-sm-6 col-md-6" style="float:left;">
  10. <input class="form-control1" id="fname" name="fname" type="text" placeholder="First Name *" required autocomplete="on">
  11. </div>
  12. <div class="c2 col-sm-6 col-md-6" >
  13. <input class="form-control1" id="lname" name="lname" type="text" placeholder="Last Name *" required autocomplete="on">
  14. </div>
  15. </div>
  16. <div class="r2 row form-group">
  17. <div class="c1 col-sm-6 col-md-6">
  18. <input class="form-control2" id="email" name="email" type="email" placeholder="Email *" required autocomplete="on">
  19. </div>
  20. <div class="c2 col-sm-6 col-md-6">
  21. <input class="form-control3" id="phone" name="phone" type="tel" placeholder="Phone" autocomplete="on">
  22. </div>
  23. </div>
  24. <div class="r4 row form-group">
  25. <div class="c1 col-md-12">
  26. <input class="form-control4" id="subject" name="subject" type="text" placeholder="Subject *" required>
  27. </div>
  28. </div>
  29. <div class="r5 row form-group">
  30. <div class="c1 col-md-12">
  31. <textarea class="form-control5" id="message" name="message" rows="10" placeholder="Message *" required></textarea>
  32. </div>
  33. </div>
  34. <div class="r6 row form-group" style="text-align:center; margin-right:16%">
  35. <div class="c1 col-md-12" >
  36. <button class="btn btn-lg btn-danger submit-form-contact" type="submit">Send Message</button>
  37. </div>
  38. </div>
  39. </fieldset>
  40. </div>
  41. </form>
  42. <!-- End Contact form -->
  43. </body>
  44. </html>

My contact.php file is as given below:

  1. <?php
  2. if ($_POST) {
  3. $to_email = "info@salefee.com"; //Recipient email, Replace with own email here
  4. //check if its an ajax request, exit if not
  5. if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  6. $output = json_encode(array( //create JSON data
  7. 'type'=>'error',
  8. 'text' => 'Sorry Request must be Ajax POST.'
  9. ));
  10. die($output); //exit script outputting json data
  11. }
  12. //Sanitize input data using PHP filter_var().
  13. $user_fname = filter_var($_POST["fname"], FILTER_SANITIZE_STRING);
  14. $user_lname = filter_var($_POST["lname"], FILTER_SANITIZE_STRING);
  15. $user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
  16. $user_phone = filter_var($_POST["phone"], FILTER_SANITIZE_NUMBER_INT);
  17. //$user_company = filter_var($_POST["user_company"], FILTER_SANITIZE_STRING);
  18. //$user_website = filter_var($_POST["user_website"], FILTER_SANITIZE_STRING);
  19. $subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
  20. $message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
  21. //additional php validation
  22. if (strlen($user_fname) < 3) { // If length is less than 3 it will output JSON error.
  23. $output = json_encode(array('type'=>'error', 'text' => 'First Name is too short or empty.'));
  24. die($output);
  25. }
  26. if (strlen($user_lname) < 3) { // If length is less than 3 it will output JSON error.
  27. $output = json_encode(array('type'=>'error', 'text' => 'Last Name is too short or empty.'));
  28. die($output);
  29. }
  30. if (!filter_var($user_email, FILTER_VALIDATE_EMAIL)) { //email validation
  31. $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email.'));
  32. die($output);
  33. }
  34. if (!filter_var($user_phone, FILTER_SANITIZE_NUMBER_FLOAT)) { //check for valid numbers in phone number field
  35. $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number.'));
  36. die($output);
  37. }
  38. if (strlen($subject) < 1) { //check emtpy subject
  39. $output = json_encode(array('type'=>'error', 'text' => 'Subject is required.'));
  40. die($output);
  41. }
  42. if (strlen($message) < 3) { //check emtpy message
  43. $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
  44. die($output);
  45. }
  46. //email body
  47. $message_body = "Name : ".$user_fname." ".$user_lname."
  48. Email : ".$user_email."
  49. Phone : ".$user_phone."
  50. Company: ".$user_company."
  51. Website: ".$user_website."
  52. ".$message;
  53. //proceed with PHP email.
  54. $headers = 'From: '.$user_fname.''."
  55. ".
  56. 'Reply-To: '.$user_email.''."
  57. ".
  58. 'X-Mailer: PHP/'.phpversion();
  59. $send_mail = mail($to_email, $subject, $message_body, $headers);
  60. if (!$send_mail)
  61. {
  62. //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
  63. $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
  64. die($output);
  65. } else {
  66. $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_fname.', thank you for your email.'));
  67. die($output);
  68. }
  69. }
  70. ?>

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-05 23: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

    1. //check if its an ajax request, exit if not
    2. if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    3. $output = json_encode(array( //create JSON data
    4. 'type'=>'error',
    5. 'text' => 'Sorry Request must be Ajax POST.'
    6. ));
    7. die($output); //exit script outputting json data
    8. }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部