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

时间:2013-10-17 22:02:05

标签: php forms email

我意识到有很多线程涉及这个主题,但我看了几个,似乎无法弄清楚我的问题。我有一个联系表单,在我第一次提出时起作用,从那时起,如果没有更改代码,就停止了正常工作。

用户可以填写表单并提交,但表单永远不会作为电子邮件发送(或者至少从未收到过。)以下是我正在使用的代码。任何有关这方面的帮助将非常感激。此外,不要认为这很重要,但网站是通过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']);

}
}
?>

1 个答案:

答案 0 :(得分:0)

所有mail()都会将电子邮件排入系统的MTA中。检查您的MTA日志(例如var / log / sendmail等)。

mail()的行是否被点击?

 echo 'got here'; exit();

我一直用这个来调试。如果代码到达特定行,它会很快通知您。

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

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