0

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>

  • Use error_reporting (E_ALL); on top to display existing errors – Michael Jun 05 '18 at 06:18
  • @Michael not displaying any errors just refreshing the page – user9131066 Jun 05 '18 at 06:24
  • Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – CBroe Jun 05 '18 at 07:27

3 Answers3

0

use this

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

If you are doing it from your local server, you will have to setup your mail properly in ini then only you will be able to send mail from your local machine.

shahburhan
  • 224
  • 1
  • 12
0

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>';
Michael
  • 556
  • 2
  • 8