0

The following code is the action for a contact form. The form is supposed to send mail, the mail doesn't send and the headers don't work. Instead, I get 'contact/contactForm' which is the action.

<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    if(isset($_POST['submit'])){
        if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
            $firstname =$_POST['firstname'];
            $surname =$_POST['surname'];
            $email =$_POST['email'];
            $tel =$_POST['tel'];
            $service =$_POST['service'];
            $message =$_POST['message'];
        
            $mailTo = myEmail;
    
            $headers = "From: personalEmail;";
            $txt = "You have received an e-mail from ". $firstname . " " . $surname . ".\r\n" .$message . ".\r\n Contact Number:" . $tel . ".\r\n Email: " . $email;
        
            $secretKey = #;
            $responseKey = $_POST['g-recaptcha-response'];
            $userIP = $_SERVER['REMOTE_ADDR'];
            $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
        
            $response = file_get_contents($url);
            $response = json_decode($response);
            
            if($response->success){
                // echo $mailTo . " ," . $service . " ," . $txt . " ," . $headers;
                mail($mailTo, $service, $txt, $headers);
                header("Location: ../contact/?message_sent", 303);
            }
            else{
                //Failed
                header("Location: ../contact/?catchpha", 303);
            }
        }
        else{
            header("Location: ../contact/?message_failed", 303);
        };
    };
    header("Location: ../contact/?form_failed", 303);
    die(); // Stops crawlers
?>  

The mailTo, personalEmail, and secretKey are just replacements for sensitive information.

  • 1
    Do you have a mail server installed? `mail()` does not send email, it passes it to a mail server which has to be configured in the `php.ini` – RiggsFolly Jun 06 '22 at 18:51
  • 2
    `303` is the wrong parameter number for that usage, should be third. – user3783243 Jun 06 '22 at 18:56
  • I don't think the issue is coming from this form, because there's nothing here that would output `'contact/contactForm'`. I'm guessing it might be related to your htaccess or whatever is processing the request before it hits this page. You can test it by doing `die('I hit the page!');` at the very top to see if you get that message. – aynber Jun 06 '22 at 19:04
  • 2
    What does "*I get 'contact/contactForm'*" mean, exactly? What does your form look like? Have you tried any debugging at all, to see if your code is running? As already pointed out, the `header()` syntax you are using is incorrect, so this might be down to simple typos; adding some debugging like `echo`s or logging to see if your code is even running will get you a bit further towards the answer. – Don't Panic Jun 06 '22 at 19:09
  • 2
    @aynber You we're right! I had changed the htaccess to change the URI, which effected path to call the page. The die('I hit the page!') was a nice debug tool... Thanks a lot :) – DKWebservices Jun 07 '22 at 09:48

1 Answers1

-1

header can only be modified before any output has been produced.

Basically, header needs to be called before any other function that can modify the headers.

In your case, a header modification was made by mail function and hence, when header call is reached, the headers have been already modified.

References:

SNikhill
  • 446
  • 2
  • 8
  • Why would `mail` send output? – user3783243 Jun 06 '22 at 18:55
  • You have a point @user3783243. I have edited the answer to reflect better terminology. Basically, I used `output` as to link it to the initial explanation `output has been produced`. – SNikhill Jun 06 '22 at 18:58
  • I don't know of an instance when `mail` would produce any output. A FALSE return won't produce output. Possibly `ini_set('display_errors', 1);` and an undefined index but for that a notice/error should be displayed. – user3783243 Jun 06 '22 at 19:02
  • 2
    Why do you think "*a header modification was made by mail*"? – Don't Panic Jun 06 '22 at 19:06