简体   繁体   English

PHP联系人表格未发送到电子邮件

[英]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. 另外,不要紧要紧,但是该网站是通过godaddy托管的,因为客户已经在这里购买了托管服务。 Let me know if I need to clarify anything further. 让我知道是否需要进一步澄清。

Code from contact.php 来自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 这是来自文件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. 所有mail()所做的工作都是将电子邮件放入系统的MTA中。 Check your MTA log (eg. var/log/sendmail, etc..). 检查您的MTA日志(例如var / log / sendmail等)。

Is the line with mail() being hit? 与mail()的行是否被击中?

 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. 您应该在发布问题之前先弄清楚这些问题,因为如果问题出在mail()上,那么这里的其他代码都与问题无关。

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. 您可以考虑使用第三方的smtp服务器而不是系统的MTA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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