After sending email page is not loaded properly in Php

49 Views Asked by At

I am using php mail() for sending email. with this function mail is being sent properly. But after sending mail page is not not loaded properly. Actually I want to land on same page after sending email. following is the code that I have written

 if (isset($_POST['submit'])) { // Check if form was submitted

    if (empty($_POST['name']) ||
        empty($_POST['email']) ||
        empty($_POST['phone']) ||
        empty($_POST['subject']) ||
        empty($_POST['message']) ||
        !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        echo "No arguments Provided!";
        return false;
    }

    $name = strip_tags(htmlspecialchars($_POST['name']));
    $email_address = strip_tags(htmlspecialchars($_POST['email']));
    $phone = strip_tags(htmlspecialchars($_POST['phone']));
    $subject = strip_tags(htmlspecialchars($_POST['subject']));
    $message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
    $to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
    $email_subject = "Subjet : $subject";
    $email_body = "You have received a new message from your website contact form.\n\n" . "Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
    $headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
    $headers .= "Reply-To: $email_address";
    mail($to, $email_subject, $email_body, $headers);
    header("Refresh:0");
    return true;
   
    // exit();

}

this is contact us page enter image description here

after sending email this page is not loaded completely only header is loaded enter image description here

1

There are 1 best solutions below

0
On

It is probably because the mail sending fails.

You can see the error it is returning by adding the following on top of the PHP file:

ini_set('display_errors', 1); 

Also, it is advisable to use PHPMailer, because the mail() function doesn't always work.