0

I am wondering why this mail function will not execute when it's inside my funtion which I call when the user submits the form

function checkSpam() {
    if (strpos($_POST['strasse'], 'spamexample') !== false)  {
        mail($tofake,$subject,$body,$headers);
        echo "fake";
    }
    else {
        mail($to,$subject,$body,$headers);
        echo "real";
    }
}

if (isset($_POST["senden"])) {
    checkSpam ();
}

?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • As soon as I place mail($to,$subject,$body,$headers); outside the function, it will send the mail. If it stays within only the echo part works when I submit my form – Marty222 Feb 25 '22 at 05:49
  • 2
    it might because of the variable scopes they are not available inside the function but outside – Meeran Khan Feb 25 '22 at 05:51
  • so i was right :D – Meeran Khan Feb 25 '22 at 05:55
  • Well, where do you think `$tofake`,`$subject`,`$body`,`$headers` and `$to` are going to come from, within your function? Remember that variables you declare outside a function are not available inside the function (and vice-versa) - you'd need to pass them in as parameters to the checkSpam() function. If you didn't know that, now would be a really good time to go and research the topic of "variable scope" in PHP. Start by reading the [relevant bit of the manual](https://www.php.net/manual/en/language.variables.scope.php). – ADyson Feb 25 '22 at 09:48

0 Answers0