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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化