简体   繁体   中英

Contact Form mailer.php returns a blank page

The contact form was working fine until I tried to add reCAPTCHA. I have managed to make reCAPTCHA appear, have the recaptchalib , and the mailer.php which is the page displaying a blank page. Any ideas what I am doing wrong?

Here is the mailer.php

<?php 
if(isset($_POST['submit'])) {

    // check reCAPTCHA information
    require_once('recaptchalib.php');

    $privatekey = "privatekey";
    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

    // if CAPTCHA is correctly entered!                        
    if ($resp->is_valid) {
        // great success!    
        $myemail = "operations@socialmarketing.com";

        /* Check all form inputs using check_input function */
        $name = $_POST['inputName'];
        $email = $_POST['inputEmail'];
        $subject = $_POST['inputSubject'];
        $message = $_POST['inputMessage'];

        /* Let's prepare the message for the e-mail */

        $subject = "Message From LGBT campaign Contact Form";

        $message = "

        China LGBT Contact Form

        Name: $name
        Email: $email

        Message:
        $message

        ";

        /* Send the message using mail() function */
        mail($myemail, $subject, $message);

        /* Redirect visitor to the thank you page */
        header('Location: successPage.html#contact');

    } else {
       // alert the captcha is not correct

    }
}?>

Here is my HTML page, which I have made a .php page

<div class="marketing">
<div class="intro" id="contact">
  <h1>Contact Us</h1>
  <p>If you would like to stay informed about our progress or would like to help with the campaign, please fill out this form to send us an email.</p>
  <div class="panel-body">
    <form name="contactform" id="contactform" action="mailer.php" class="form-horizontal" role="form"  method="POST">


      <div class="form-group">
        <label class="col-lg-2 control-label">Name</label>
        <div class="col-lg-10">
          <input type="text" class="form-control" name="inputName" placeholder="Your Name">
        </div>
      </div>
      <div class="form-group">
        <label class="col-lg-2 control-label">Email</label>
        <div class="col-lg-10">
          <input type="email" class="form-control" name="inputEmail" placeholder="Your Email">
        </div>
      </div>
      <div class="form-group">
        <label class="col-lg-2 control-label">Message</label>
        <div class="col-lg-10">
          <textarea class="form-control" rows="4" name="inputMessage" placeholder="Your message..."></textarea>
        </div>
      </div>

      <div class="form-group">
        <div class="col-lg-10 col-lg-offset-2">
        <p>Prove you are not a spambot</p>
        <?php require_once('recaptchalib.php');
            $publickey = "publickey";
            echo recaptcha_get_html($publickey);
          ?>

    </div>
  </div>

      <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
          <button type="submit" class="btn btn-primary">Send Message</button>
        </div>
      </div>
    </form>
  </div>
</div>

Any help with this would be hugely appreciated guys.

Thanks a bunch

SOLUTION

<form name="contactform" id="contactform" action="mailer.php" class="form-horizontal" role="form"  method="post">


      <div class="form-group">
        <label class="col-lg-2 control-label">Name</label>
        <div class="col-lg-10">
          <input type="text" class="form-control" name="inputName" placeholder="Your Name">
        </div>
      </div>
      <div class="form-group">
        <label class="col-lg-2 control-label">Email</label>
        <div class="col-lg-10">
          <input type="email" class="form-control" name="inputEmail" placeholder="Your Email">
        </div>
      </div>
      <div class="form-group">
        <label class="col-lg-2 control-label">Message</label>
        <div class="col-lg-10">
          <textarea class="form-control" rows="4" name="inputMessage" placeholder="Your message..."></textarea>
        </div>
      </div>

      <div class="form-group">
        <div class="col-lg-10 col-lg-offset-2">
        <p>Prove you are not a spambot</p>
        <?php require_once('recaptchalib.php');
            $publickey = "6Le0ff0SAAAAAOCeQiOcGUwQEfXERDyNJ";
            echo recaptcha_get_html($publickey);
          ?>

    </div>
  </div>

      <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
          <input type="submit" class="btn btn-primary" value="Send Message" name="submit">
        </div>
      </div>
    </form>

<?php 

 error_reporting(E_ALL);
 ini_set('display_errors', 1);

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

    // check reCAPTCHA information
    require_once('recaptchalib.php');

    $privatekey = "6Le0ff0SAAAALTDn4IkqNSN5F0AU2Ezhvf";
    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

    // if CAPTCHA is correctly entered!                        
    if ($resp->is_valid) {
        // great success!    
        $myemail = "kenm@socialmarketing.com";


        /* Check all form inputs using check_input function */
        $name = $_POST['inputName'];
        $email = $_POST['inputEmail'];
        $subject = "Message From LGBT campaign Contact Form";
        $message = $_POST['inputMessage'];
        $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

        /* Let's prepare the message for the e-mail */

        $message = "

        China LGBT Contact Form

        Name: $name
        Email: $email

        Message:
        $message

        ";

        /* Send the message using mail() function */
        mail($myemail, $subject, $message);

        /* Redirect visitor to the thank you page */
        header('Location: successPage.html#contact');

    } else {

       // alert the captcha is not correct
      echo "captcha did not match!";
      exit;

    }
}?>

Change your button to this:

<input type="submit" class="btn btn-primary" value="Send Message" name="submit">

POST is looking for a named attribute called submit .

which based on your conditional statement, and nothing will execute inside it because of it:

if(isset($_POST['submit'])) {...}

You also don't have a named form element to go with $subject = $_POST['inputSubject'];

Either add one:

Subject:<input type="text" class="form-control" name="inputSubject" placeholder="Subject">

or simply test with:

$subject = "Form submitted";

You should make sure that all fields are filled. If the subject is left empty or any other, you may not receive mail because of it, especially the Email field.

Another reason may be because you do not have proper headers, including a From:

Visit the PHP.net website on mail:

Example From: header from the website:

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

and modifying mail($myemail, $subject, $message);
to mail($myemail, $subject, $message, $headers);

Quoting them:

Note:

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

If you're getting a blank page, it's because something is failing and not showing an error. I'd start by making sure that error reporting is enabled in your PHP script. Add this to the top of mailer.php.

ini_set('display_errors',1); 
error_reporting(E_ALL);

You also need to add some sort of message here.

} else {

    // alert the captcha is not correct
    echo "captcha did not match!";
    exit;

}

Also as mentioned, check your error log.

You can start by changing

<button type="submit" class="btn btn-primary">Send Message</button>

to

<input type="submit" class="btn btn-primary" value="submit">

This should submit your form to mailer.php. From that point on it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM