使用PHP正确编写电子邮件表单?

时间:2016-12-30 00:00:53

标签: php html forms email

所以,有两个问题:

1。)我的电子邮件表单无法正常工作,在提交时,它会建议"网页无法显示"。

2。)如果编码正确,电子邮件表格不会发送电子邮件。

我试图让我的电子邮件表单正常工作。它正在工作,(但是没有发送电子邮件)然后我重新排列了一些东西(在表单中添加了主题行)。现在,一旦我添加了一个主题,我似乎无法弄清楚什么变得难以理解。有什么建议吗?

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

我的PHP表单位于JSFIDDLE的CSS部分。

联系表单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(无法正确格式化)...

    <?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.\n\n";



    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)."\n";

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

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

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





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'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 个答案:

答案 0 :(得分:4)

好的,所以我发现了2个错误。

第一个错误

这一行:

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

应改为:

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

因为引号符号正在破坏字符串,所以如果您需要在字符串中使用引号符号,则需要通过添加反斜杠或使用单引号创建字符串来对其进行转义。

第二次错误

你指的是一个名为$ subject的变量,它不存在。

因此,不要使用包含主题数据的名为$ telephone的变量,而是将其名称更改为$ subject (您永远不会使用变量$ telephone,因此它不会影响您)

这一行:

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

应改为:

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

编辑:在我上面提到的两项更改后,我的服务器上的代码已经过测试,我收到了预期的电子邮件。