简体   繁体   中英

PHP Contact form not being sent to email

I realize there are lots of threads covering this topic, but I've looked at several and can't seem to figure out my issue. I have a contact form that worked when I first put it up, and since then, with no changes to the code, has stopped working correctly.

A user can fill out the form and submit it, but the form is never sent as an email (or at least it's never received.) Below is the code I am working with. Any help with this would be very much appreciated. Also, don't think this would matter, but the website is hosted through godaddy because that is where the client had already purchased their hosting. Let me know if I need to clarify anything further.

Code from contact.php

<div id="contactForm" class="clearfix">
            <?php
                //init variables
                $cf = array();
                $sr = false;

                if(isset($_SESSION['cf_returndata'])){
                    $cf = $_SESSION['cf_returndata'];
                    $sr = true;
                }
            ?>
            <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
                <li id="info">There were some problems with your form submission:</li>
                <?php 
                if(isset($cf['errors']) && count($cf['errors']) > 0) :
                    foreach($cf['errors'] as $error) :
                ?>
                <li><?php echo $error ?></li>
                <?php
                    endforeach;
                endif;
                ?>
            </ul>
            <p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for your message! We will get back to you ASAP!</p>
        <form action="contact-process.php" method="post" id="contaxForm">
            <div class="rowElem clearfix">
                <label for="name">Name:<em class="warning">*</em></label>
                <input id="name" name="name" class="input-text" type="text" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" placeholder="John Doe" autofocus required>                    
            </div>     

            <div class="rowElem clearfix">
                <label for="email">Email:<em class="warning">*</em></label>
                <input id="email" name="email" class="input-text" type="email" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" placeholder="joe@email.com" required>
            </div>       

            <div class="rowElem clearfix">
                <label for="subject">Subject: </label>
                <select name="subject">
                    <option value="General Inquiry">General Inquiry</option>
                    <option value="Reviews">Review</option>
                    <option value="Wholesale">Wholesale</option>
                    <option value="Other">Other</option>
                </select>
            </div>

            <div class="rowElem clearfix">
                <label for="message">Message:</label>
                <textarea class="large" rows="5" id="message" name="message" class="input-text" type="text" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?>" placeholder=""></textarea>
            </div>
            <div class="rowElem">
                <label> &nbsp; </label>
                <input class="submitBtn" type="submit" value="Submit!" />
            </div>
        </form>
        <?php unset($_SESSION['cf_returndata']); ?> <!--this allows the form to be reset when leaving or refreshing page--> 
    </div>

And here is the code from the file contact-process.php

<?php  
if( isset($_POST) ){  

//form validation vars  
$formok = true;  
$errors = array();  

//submission data  
$ipaddress = $_SERVER['REMOTE_ADDR'];  
$date = date('d/m/Y');  
$time = date('H:i:s');

//form data
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
// $device = $_POST['device'];
$message = $_POST['message'];

//form validation to go here....

//validate name is not empty
if(empty($name)){
    $formok = false;
    $errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
    $formok = false;
    $errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $formok = false;
    $errors[] = "You have not entered a valid email address";
}
//validate message is not empty
if(empty($message)){
    $formok = false;
    $errors[] = "You have not entered a message model";
}

//send email if all is ok
if($formok){        
    $headers  = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers = "From: email@yahoo.com" . "\r\n";
    $headers .= "Reply-To: $email" . "\r\n";
    $to = "email@website.com";
    $email_subject .= "New Submission from your website";

    $emailbody = "<p>You have recieved a new message from the contact form on your website.</p>
                  <p><strong>Name: </strong> {$name} </p>
                  <p><strong>Email Address: </strong> {$email} </p>
                  <p><strong>Subject: </strong> {$subject} </p>
                  <p><strong>Message: </strong> {$message} </p>
                  <p>This message was sent on {$date} at {$time}</p>";

    mail($to,$email_subject,$emailbody,$headers);       
}

//what we need to return back to our form
$returndata = array(
    'posted_form_data' => array(
        'name' => $name,
        'email' => $email,
        'message' => $message
    ),
    'form_ok' => $formok,
    'errors' => $errors
);

//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){

    //set session variables
    session_start();
    $_SESSION['cf_returndata'] = $returndata;

    //redirect back to form
    header('location: ' . $_SERVER['HTTP_REFERER']);

}
}
?>

All mail() does is queue the email in the system's MTA. Check your MTA log (eg. var/log/sendmail, etc..).

Is the line with mail() being hit?

 echo 'got here'; exit();

I use this to debug all the time. It will quickly let you know if the code is reaching a particular line.

You should figure this stuff out before posting your question, because if the problem is with mail(), none of your other code is related to the problem here.

As for fixing your mail server, that's not a programming question. You can look into using a 3rd party smtp server instead of your system's MTA.

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