在php中单击“提交”按钮后无法发送电子邮件

p8ekf7hl  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(322)

试图发送一封没有显示任何错误信息或只是刷新它所显示的页面的邮件。
我试图通过电子邮件联系形式,但它不工作。这里是代码。尝试做回声,但它不工作。不显示任何错误以及。
联系方式:

<?php ob_start();
if(isset($_POST['submit'])) {   
$to = "abc@gmail.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$txt = $_POST['comment'];
$headers = "From: " .$email . "\r\n" .
"CC: xyz@gmail.com";    
mail($to,$subject,$txt,$headers);   
header("Location: contact.php");    
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>HMS System</title>       
</head>
<body>      
    <div class="container">         
                <div class="page-header">
                    <h2>Contact Us Form</h2>
                </div>
                <form class="form-horizontal" action="contactus.php" method="post" role="form">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">Name</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="name" placeholder="Insert your Name" id="name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">Email Address</label>
                        <div class="col-sm-8">
                            <input type="email" class="form-control" name="email" placeholder="Email Address" id="email">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="subject" class="col-sm-2 control-label">Subject</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="subject" placeholder="Subject" id="subject">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="comments" class="col-sm-2 control-label">Comment</label>
                        <div class="col-sm-8">
                            <textarea class="form-control" rows="10" name="comment" style="resize:none;"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label"></label>
                        <div class="col-sm-8">
                            <input type="submit" value="Send Message" name="submit_contact" class="btn btn-block btn-danger" id="subject">
                        </div>
                    </div>
                </form>

            </section>

        </article>
    </div>
    <div style="width:50px;height:50px;"></div>

</body>
inb24sb2

inb24sb21#

如果您是从您的本地服务器,您将不得不设置您的邮件正确的ini然后只有您将能够从您的本地机器发送邮件。

cgvd09ve

cgvd09ve2#

用这个

if(isset($_POST['submit_contact']))
aiazj4mn

aiazj4mn3#

更好的方法是将 mail 语句中的函数。邮件正在返回 bool 价值观。开始尽可能简单。

error_reporting (E_ALL);

$to = "mail@whatever.com"
// ... your settings ...

if ( mail ( $to , $subject , $message ) ) {

    // do when ok

} 
else {  

    // do when error

}

验证是否从接收到所有值 $_POST

echo '<pre>';
print_r ($_POST);
echo '</pre>';

相关问题