简体   繁体   中英

Unable to send an email after clicking on submit button in php

Trying to send an email not displaying any error message or anything just refreshing the page that it.

I am trying an email through contact form but its not working.Here is the code for that. tried by doing echo but it is not working .not displaying any error as well.

contact and contactus:

<?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>

用这个

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

如果您是通过本地服务器执行的,则必须在ini中正确设置邮件,然后只有您才能从本地计算机发送邮件。

A better approach would be to wrap the mail function in a statement. Mail is returning a bool value. And beginn as simple as possible.

error_reporting (E_ALL);

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

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

    // do when ok

} 
else {  

    // do when error

}

To verify that you receive all values from $_POST do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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