PHP联系表单失败

时间:2012-09-11 12:29:31

标签: php html forms

我写了一个php联系表单,无法弄清楚它为什么不发送电子邮件。表单提交正常,但不发送实际的电子邮件。

以下是我的PHP代码。

<?php  
 //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)) {  
        $emailTo = 'xyz@xyz.com';  
        $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";  
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;  

        mail($emailTo, $subject, $body, $headers);  
       $emailSent = true;  
 }  
}  
?> 

以下HTML代码是联系表单HTML:

 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="contactform">
                <fieldset class="contact-fieldset">
                    <h2>Send us a message</h2>
                    <ul>
                        <li>
                            <label for="contactname">Your Name:</label>
                            <div class="ctinput-bg"><input type="text" name="contactname" id="contactname" value="" class="contact-input required" /></div>
                        </li>
                        <li>
                            <label for="email">Email:</label>
                            <div class="ctinput-bg"><input type="text" id="email" name="email" class="contact-input required email" /></div>
                        </li>     
                        <li>
                            <label for="subject">Subject:</label>
                            <div class="ctinput-bg"><input type="text" name="subject" id="subject" class="contact-input required" /></div>
                        </li>
                        <li>
                            <label for="message">Your message:</label>
                            <div class="cttxtarea-bg"><textarea rows="6" cols="40" id="message" name="message" class="contact-textarea required"></textarea></div>
                        </li>
                        <li>
                            <div class="form-button contact-submit">
                                <span><input type="submit" value="&nbsp;&nbsp;send message&nbsp;&nbsp;" name="submit" /></span>
                            </div>
                        </li>                      
                    </ul>
                </fieldset>
            </form>

4 个答案:

答案 0 :(得分:1)

除了其他人提供的解决方案之外,我强烈建议您使用SwiftMailer库。

我必须创建一个自动电子邮件功能,手动设置标题等是一个噩梦。 SwiftMailer为我节省了大量时间,请参阅here以获取示例。

答案 1 :(得分:0)

听起来您的mail功能设置不正确。检查您的php.ini文件,确保您具有正确的SMTP服务器设置。

此外,与原始问题无关,但我不确定使用<ul>标记表单在语义上是正确的。一组字段实际上不是一个未排序的列表,我更喜欢使用语义正确的<table>甚至一系列<div>

答案 2 :(得分:0)

我看到两件事:

1-你的代码顶部的$ hasError = false的声明(让shure成为excist)

2 - 生成标题:

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

希望这可以解决问题,

答案 3 :(得分:0)

您可以尝试检查邮件是否实际发送:

if (mail($to, $subject, $body)) {
   echo("<p>Message successfully sent!</p>");
} else {
   echo("<p>Message delivery failed...</p>");
}

并且确保您还可以对标头执行var_dump以查看它们是否正确:

var_dump($headers);

这些东西不会解决问题,但肯定有助于调试它。