doucan9079 2016-12-30 00:00
浏览 23
已采纳

使用PHP正确编写电子邮件表单? [关闭]

So, two problems:

1.) My email form is not working, on submit, it advises "web page can't be displayed".

2.) The email form, when properly coded, did not send an email.

I'm trying to get my email form to properly work. It was working, (however was not sending email) then I rearranged some things (added subject line in the form). Now, I can't seem to figure out what went wonky once I added a subject line. Any suggestions?

https://jsfiddle.net/ebxam743/1/

My PHP form is in the CSS section of JSFIDDLE.

Contact Form HTML

<div class="col-md-8">
       <form name="contactform" method="post" action="send_form_email.php">

          <div class="row contact-row">
            <div class="col-md-6 contact-name">

              <input type="text" name="first_name" placeholder="Name">
            </div>

            <div class="col-md-6 contact-email">

              <input type="text" name="email" placeholder="E-mail*">
            </div>
          </div>

          <input type="text" name="subject" placeholder="Subject*">

          <textarea name="comments" placeholder="Message"></textarea>
          <input type="submit" class="btn btn-lg btn-color btn-submit" value="Send Message">
          </div>
        </div>
      </form>
       <!-- end col -->

PHP (can't get it to format properly)...

    <?php

if(isset($_POST['email'])) {



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "you@yourdomain.com";

    $email_subject = "Your email subject line";





    function died($error) {

        // your error code can go here

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please <a href="contact.html">go back</a> and fix these errors.<br /><br />";

        die();

    }



    // validation expected data exists

    if(!isset($_POST['first_name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['subject']) ||

        !isset($_POST['comments'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');       

    }



    $first_name = $_POST['first_name']; // required

    $email_from = $_POST['email']; // required

    $telephone = $_POST['subject']; // not required

    $comments = $_POST['comments']; // required



    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'The Name you entered does not appear to be valid.<br />';


  }

  if(strlen($comments) < 2) {

    $error_message .= 'The Message you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Form details below.

";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "First Name: ".clean_string($first_name)."
";

    $email_message .= "Email: ".clean_string($email_from)."
";

    $email_message .= "Subject: ".clean_string($subject)."
";

    $email_message .= "Comments: ".clean_string($comments)."
";





// create email headers

$headers = 'From: '.$email_from."
".

'Reply-To: '.$email_from."
" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->


 <center><img src="img/rebelliouslogob.png" 

             Thank you for contacting us. We will be in touch with you very soon.</center>

    <a href="contact.html"><h1>Go Back</h1></a>



<?php

}

?>
  • 写回答

1条回答 默认 最新

  • doz59484 2016-12-30 00:09
    关注

    Okay, so I found 2 errors.

    First error

    This line:

    echo "Please <a href="contact.html">go back</a> and fix these errors.<br /><br />";
    

    Should be changed to:

    echo "Please <a href=\"contact.html\">go back</a> and fix these errors.<br /><br />";
    

    Because the quote symbol is breaking the string, so if you need to use the quote symbol inside a string you need to escape it by adding a backslash or create strings using single quotes.

    Second error

    You are referring to a variable called $subject that doesn't exist.

    So instead of using a variable called $telephone which contains the subject data, change the name of it to $subject (you never use the variable $telephone, so it shouldn't affect you).

    This line:

    $telephone = $_POST['subject']; // not required
    

    Should be changed to:

    $subject = $_POST['subject']; // not required
    

    EDIT: Tested the code on my server after the two changes I mentioned above, and I received an email as expected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名