0

我意识到有很多线程涵盖了这个主题,但我已经看了几个,似乎无法弄清楚我的问题。我有一个联系表格,在我第一次发布时可以使用,从那时起,没有更改代码,它就停止了正常工作。

用户可以填写表格并提交,但表格永远不会作为电子邮件发送(或者至少从未收到过)。下面是我正在使用的代码。对此的任何帮助将不胜感激。另外,不要认为这很重要,但该网站是通过 godaddy 托管的,因为客户已经在那里购买了托管服务。让我知道是否需要进一步澄清。

来自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>

这是文件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']);

}
}
?>
4

1 回答 1

0

所有 mail() 所做的就是在系统的 MTA 中对电子邮件进行排队。检查您的 MTA 日志(例如 var/log/sendmail 等)。

是否命中了 mail() 的行?

 echo 'got here'; exit();

我一直用它来调试。它会很快让您知道代码是否到达特定行。

你应该在发布你的问题之前弄清楚这些东西,因为如果问题出在 mail() 上,那么你的其他代码都与这里的问题无关。

至于修复您的邮件服务器,这不是编程问题。您可以考虑使用 3rd 方 smtp 服务器而不是系统的 MTA。

于 2013-10-17T22:11:04.770 回答