简体   繁体   中英

Simple PHP Contact Form: Submit Button Not Sending Emails

I am using a contact form on a site and it is not sending the email, please can anyone give me advise where this code is wrong?
This form was copied from another site that worked, so I cannot understand where it fails. I've checked the syntax and everything seems ok.

<?php 
/*
Template Name: Contact Page
*/

get_header();


$contact_show = true;

//If the form is submitted  
if(isset($_POST['submit'])) {  

 //Check to make sure that the name field is not empty  
 if(trim($_POST['contactname']) == '') {  
     $hasError = true;  
 } else {  
    $name = trim($_POST['contactname']);  
 }  
 //Check to make sure that the subject field is not empty  
if(trim($_POST['subject']) == '') {  
     $hasError = true;  
 } else {  
     $subject = trim($_POST['subject']);  
 }  

//Check to make sure sure that a valid email address is submitted  
if(trim($_POST['email']) == '')  {  
     $hasError = true;  
 } else if (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i",        trim($_POST['email']))) {  
     $hasError = true;
 } else {  
     $email = trim($_POST['email']);  
 }  

//Check to make sure comments were entered  
 if(trim($_POST['message']) == '') {  
     $hasError = true;  
} else {  
    if(function_exists('stripslashes')) {  
         $comments = stripslashes(trim($_POST['message']));  
   } else {  
        $comments = trim($_POST['message']);  
    }  
 }  

//If there is no error, send the email  
 if(!isset($hasError)) {  
    $contact_show = false;
    $emailTo = get_option('of_email_address'); 
    $body = "Name: $name \r\nEmail: $email \r\nSubject: $subject \r\nComments:\r\n $comments";          
    $headers = 'From: <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;  

    mail($emailTo, $subject, $body, $headers);  
    $emailSent = true;  
 }  
}  
?> 
<div id="top">
<h1><?php the_title(); ?></h1>
</div>
</div>
<div id="main_content">
<div id="contact">
<?php while ( have_posts() ) : the_post(); ?>
<?php endwhile; // end of the loop. ?>
<?php if ($contact_show == true) { ?>
<?php } ?> 


        <?php if(isset($hasError)) { //If errors are found ?>
            <div class="redbox">
                <p><?php _e('Please check if you\'ve filled all the fields with valid information. Thank you.') ?></p>
            </div>
        <?php } ?>

        <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
            <div class="greenbox">
                <p>
                    <?php _e('Your message has been sent!') ?><br />
                    <?php echo $name; _e(' thank you for emailing us! We will get in touch with you soon.'); ?>
                </p>
            </div>
        <?php } ?> 
        <form action="" method="post" id="contactForm"><!-- start contact form -->
            <fieldset class="contact-fieldset">
                <ul>
                    <li>
                        <label for="contactname"><?php _e('Name:') ?></label>
                        <input type="text" name="contactname" id="name" class="contact-input requiredContact" />
                    </li>
                    <li>
                        <label for="email"><?php _e('Email:') ?></label>
                        <input type="text" id="email" name="email" class="contact-input     requiredContact cEmail" />
                    </li>     
                    <li>
                        <label for="subject"><?php _e('Subject:') ?></label>
                        <input type="text" name="subject" id="subject" class="contact-input requiredContact" />
                    </li>
                    <li>
                        <label for="message"><?php _e('Message:') ?></label>
                        <textarea rows="6" style="overflow: hidden;" cols="40" id="message" name="message" class="contact-textarea 

requiredContact"></textarea>
                    </li>
                    <li>
                        <input type="submit" value="<?php _e('send message'); ?>"     name="submit" class="contact-submit" 

/>
                    </li>                      
                </ul>
            </fieldset>
        </form></div>



</div>

If you coppied it from another site that did work, that suggests the PHP code works and that it's in fact your server setup that is wrong as that is what you have changed (and not the PHP code, which i assume shows no errors if it was previously working). Ensure these settings are correct:

http://php.net/manual/en/mail.configuration.php

I check your coding, in that there are no errors and the reason for not working is the function calling may be.

In your coding there are several function calls are present. Check that all the functions are present and if any errors in that functions.

Again, PHP looks good; check your php.ini file and ensure that mail functions are configured to either give PHP a path to sendmail or an SMTP connection; also, be sure that your firewall settings are not blocking port 25 (or your configuration's SMTP relay port)

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