Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

I want to send mail from a website contact page.

I wrote the code, but after sending the email, it does not redirect to a thank you page.

I can't find what the error is. Please I need help.

What I have tried:

PHP
<pre><?php
    $mail_to = 'myemail@gmail.com'; // specify your email here

    // Assigning data from the $_POST array to variables
    $name = $_POST['w3lName'];
    $mail_from = $_POST['w3lSender'];
    $phone = $_POST['w3lPhone'];
    $msubject = $_POST['w3lSubject'];
    $message = $_POST['w3lMessage'];

    // Construct email subject
    $subject = 'Message from visitor ' . $name;

    // Construct email body
    $body_message = 'From: ' . $name . "\r\n";
    $body_message .= 'E-mail: ' . $mail_from . "\r\n";
    $body_message .= 'Phone: ' . $phone . "\r\n";
    $body_message .= 'Subject: ' . $msubject . "\r\n";
    $body_message .= 'Message: ' . $message;

    // Construct email headers
    $headers = 'From: ' . $mail_from . "\r\n";
    $headers .= 'Reply-To: ' . $mail_from . "\r\n";

    // Send mail
    $mail_sent = mail($mail_to, $subject, $body_message, $headers);

    // redirect to success page 
    if ($success){
        header("Location:https://www.bbc.com"); //Redirect to url if form submitted
    }
    else{
    echo "Error";
    }

?>
Posted
Updated 18-Sep-20 0:56am
Comments
[no name] 18-Sep-20 6:35am    
Have you ever been able to redirect anybody anywhere or is this your first attempt?
namo77 18-Sep-20 7:03am    
First attempt

1 solution

You are checking the wrong variable after sending the mail.
PHP
$mail_sent = mail($mail_to, $subject, $body_message, $headers);

// redirect to success page 
if ($success){ // you should be checking $mail_sent
    header("Location:https://www.bbc.com"); //Redirect to url if form submitted
}
else{
echo "Error";
}
 
Share this answer
 
Comments
namo77 18-Sep-20 7:12am    
Thank you! It works perfectly now.
🙏🏽

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900