繁体   English   中英

简单的PHP联系人表格:“提交”按钮不发送电子邮件

[英]Simple PHP Contact Form: Submit Button Not Sending Emails

我正在网站上使用联系表格,但它没有发送电子邮件,请有人能告诉我该代码有误吗?
此表单是从另一个有效的站点复制而来的,所以我不知道它在哪里失败。 我检查了语法,一切似乎都正常。

<?php 
/*
Template Name: Contact Page
*/

get_header();


$contact_show = true;

//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)) {  
    $contact_show = false;
    $emailTo = get_option('of_email_address'); 
    $body = "Name: $name \r\nEmail: $email \r\nSubject: $subject \r\nComments:\r\n $comments";          
    $headers = 'From: <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;  

    mail($emailTo, $subject, $body, $headers);  
    $emailSent = true;  
 }  
}  
?> 
<div id="top">
<h1><?php the_title(); ?></h1>
</div>
</div>
<div id="main_content">
<div id="contact">
<?php while ( have_posts() ) : the_post(); ?>
<?php endwhile; // end of the loop. ?>
<?php if ($contact_show == true) { ?>
<?php } ?> 


        <?php if(isset($hasError)) { //If errors are found ?>
            <div class="redbox">
                <p><?php _e('Please check if you\'ve filled all the fields with valid information. Thank you.') ?></p>
            </div>
        <?php } ?>

        <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
            <div class="greenbox">
                <p>
                    <?php _e('Your message has been sent!') ?><br />
                    <?php echo $name; _e(' thank you for emailing us! We will get in touch with you soon.'); ?>
                </p>
            </div>
        <?php } ?> 
        <form action="" method="post" id="contactForm"><!-- start contact form -->
            <fieldset class="contact-fieldset">
                <ul>
                    <li>
                        <label for="contactname"><?php _e('Name:') ?></label>
                        <input type="text" name="contactname" id="name" class="contact-input requiredContact" />
                    </li>
                    <li>
                        <label for="email"><?php _e('Email:') ?></label>
                        <input type="text" id="email" name="email" class="contact-input     requiredContact cEmail" />
                    </li>     
                    <li>
                        <label for="subject"><?php _e('Subject:') ?></label>
                        <input type="text" name="subject" id="subject" class="contact-input requiredContact" />
                    </li>
                    <li>
                        <label for="message"><?php _e('Message:') ?></label>
                        <textarea rows="6" style="overflow: hidden;" cols="40" id="message" name="message" class="contact-textarea 

requiredContact"></textarea>
                    </li>
                    <li>
                        <input type="submit" value="<?php _e('send message'); ?>"     name="submit" class="contact-submit" 

/>
                    </li>                      
                </ul>
            </fieldset>
        </form></div>



</div>

如果您从另一个运行正常的站点复制它,则表明PHP代码可以工作,并且实际上是服务器设置错误,因为那是您所做的更改(而不是PHP代码,我认为如果没有更改,则不会显示任何错误)以前在工作)。 确保这些设置正确:

http://php.net/manual/zh/mail.configuration.php

我检查了您的编码,因为没有错误,无法正常工作的原因可能是函数调用。

在您的编码中,存在多个函数调用。 检查所有功能是否存在以及该功能是否有错误。

同样,PHP看起来不错。 检查您的php.ini文件,并确保已将邮件功能配置为为PHP提供sendmail或SMTP连接的路径; 另外,请确保您的防火墙设置未阻止端口25(或配置的SMTP中继端口)

暂无
暂无

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

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