0

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 = 'myemail@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - 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: myemail@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@gmail.com.
    $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

Rob
  • 14,746
  • 28
  • 47
  • 65
  • 1
    This looks like partial code. Otherwise why have `return false` and `return true` inside if conditions? Would seem this is a larger function or method. Perhaps show all your code since you dont know where the bug resides. Partial code only shows where you yourself think the problem resides. – GetSet Jan 31 '21 at 04:49
  • Are you really using the Unix program `sendmail` which you tagged this as? – Rob Jan 31 '21 at 11:56

1 Answers1

1

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.

Example person
  • 3,198
  • 3
  • 18
  • 45