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?

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 class="col-md-6 contact-email">

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

          <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">
       <!-- end col -->

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


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


    $email_to = "";

    $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 />";



    // 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) {



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



  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论

相关推荐 更多相似问题