javascript - 联系表单显示成功消息但不发送电子邮件

标签 javascript email sendmail php

我为我网站的联系页面创建了以下 php 脚本。该脚本的问题在于,当用户填写并单击提交按钮时,它会显示成功消息,但电子邮件未发送。
有人可以检查一下并告诉我哪里出了问题吗?

<?php 
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP

//If the form is submitted
if(isset($_POST['submitted'])) {

// require a name from user
if(trim($_POST['contactName']) === '') {
    $nameError =  'Forgot your name!'; 
    $hasError = true;
} else {
    $name = trim($_POST['contactName']);
}

// need valid email
if(trim($_POST['email']) === '')  {
    $emailError = 'Forgot to enter in your e-mail address.';
    $hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
    $emailError = 'You entered an invalid email address.';
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

// we need at least some content
if(trim($_POST['comments']) === '') {
    $commentError = 'You forgot to enter a message!';
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['comments']));
    } else {
        $comments = trim($_POST['comments']);
    }
}

// upon no failure errors let's email now!
if(!isset($hasError)) {

    $emailTo = 'info@example.com';
    $subject = 'Submitted message from '.$name;
    $sendCopy = trim($_POST['sendCopy']);
    $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
    $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);

    // set our boolean completion value to TRUE
    $emailSent = true;
}
}
?>

<!-- @begin contact -->
<div id="contact" class="section">
    <div class="container content">

        <?php if(isset($emailSent) && $emailSent == true) { ?>
            <p class="info">Your email was sent. Sir!</p>
        <?php } else { ?>



            <div id="contact-form">
                <?php if(isset($hasError) || isset($captchaError) ) { ?>
                    <p class="alert">Error submitting the form</p>
                <?php } ?>

                <form id="contact-us" action="contacts.php" method="post">
            <div class="columns two contact_label alpha">
                Your Name 
            </div>
            <div class="columns five contact_input omega">
                <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="txt requiredField" placeholder="Name:" />
                        <?php if($nameError != '') { ?>
                            <br /><span class="error"><?php echo $nameError;?></span> 
                        <?php } ?>
                    </div>

                    <div class="clear"><!-- ClearFix --></div>
            <div class="columns two contact_label alpha">
                E-Mail Address
            </div>
            <div class="columns five contact_input omega">
                <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="txt requiredField email" placeholder="Email:" />
                        <?php if($emailError != '') { ?>
                            <br /><span class="error"><?php echo $emailError;?></span>
                        <?php } ?>
                    </div>
            <div class="clear"><!-- ClearFix --></div>
                <div class="columns two contact_label alpha">
                Your Message
            </div>
            <div class="columns five contact_input omega">
                <textarea name="comments" id="commentsText" cols="45" rows="5" class="txtarea requiredField" placeholder="Message:"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
                        <?php if($commentError != '') { ?>
                            <br /><span class="error"><?php echo $commentError;?></span> 
                        <?php } ?>
                    </div>
            <div class="clear"><!-- ClearFix --></div>

                <div class="columns five contact_button alpha omega offset-by-two">
                <input type="submit" name="submitted" id="submitted" value="Send Email" />
            </div>
            <div class="clear"><!-- ClearFix --></div> 


                </form>         

             </div>
        <?php } ?>
    </div>
</div><!-- End #contact -->

最佳答案

您需要删除该行:

$emailSent = true;

并将邮件发送行更改为

$emailSent = mail($emailTo, $subject, $body, $headers);

如果电子邮件被接受发送,这会将 $emailSent 设置为 true。 PHP Mail

关于javascript - 联系表单显示成功消息但不发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15045770/

相关文章:

显示空白回复的 PHP 邮件表单

email - 以编程方式从 Linux 服务器发送电子邮件

python - ConnectionRefusedError at/password-reset/[WinError 10061] 由于目标机器主动拒绝而无法建立连接

Javascript 日期测试工具

php - 如何在预定时间用php发送邮件?

javascript - 获取选择中的光标位置 - 相对于文档

email - spamassassin 停止转发垃圾邮件

iphone - MFMailComposeViewController 发件人地址问题

javascript - Freshdesk - 超时后初始化

javascript - 克隆后更改了原始对象