-1

sinds a couple of days my form has stopped working. I'm not really familiar with PHP and i have no idea how to get it working again.

I'm hosting my site on one.com , it has been working fine for about a year now, and suddenly it stopped working? I've been searching the internet and i'm finding all kinds of answers 'you have to update your php code...' but i'm not familiar with PHP so please help :D

The form.php :

<?php
/* Set e-mail recipient */
$myemail  = "info@kongweb.be";
/* Check all form inputs using check_input function */
$naam = check_input($_POST['naam']);
$mail = check_input($_POST['mail']);
$bericht = check_input($_POST['bericht']);
/* Let's prepare the message for the e-mail */
$message = "Hallo!
Wij ontvingen een contactformulier met de volgende gegevens:
Naam: $naam
E-mailadres: $mail
Message: $bericht
Einde van dit bericht.
";
/* Send the message using mail() function */
$headers = "From: info@kongweb.be";
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:bedankt.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}
function show_error($myError)
{
?>
    <html>
    <body>
    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>
    </body>
    </html>
<?php
exit();
}
?>

And the code for the contact form on the index.html :

<!-- contact begin -->
    <div id="contact" class="container-fluid text-center tekst2">
        <h1>Contact</h1>
    <form name="contact" method="post" action="https://kongweb.be/form.php" onsubmit="return validateForm()" >
    <input type="hidden" name="subject" value="contact">

    <div class="row slideanim spatieform">
       <div class="col-sm-12">
            <input type="text" name="naam" size="30" id="naam" required placeholder="Name">
       </div>
    </div>
    <div class="row slideanim spatieform">
        <div class="col-sm-12">
            <input type="text" name="mail" size="30" id="mail" required placeholder="Email">
        </div>
    </div>

    <div class="row slideanim spatieform">
        <div class="col-sm-12">
     <textarea name="bericht" id="bericht" cols="30" rows="8" required placeholder="Message"></textarea>
    </div>
    </div>

    <div class="row slideanim spatieform">
        <div class="col-sm-12">
    <input type="submit" name="submit" value="Send it" style="background-color: #F3E600">
    </div>
    </div>
    </form>

    </div>

<!-- contact einde -->
  • There are many reasons why mail might not be delivered, most not related to your code. You need to start tracking down the problem from mail logs and bounced messages (if any). This is not straightforward. The linked question can get you started, but unless you can show that the problem exists in your code, there's little more we can do. – Tangentially Perpendicular Jun 09 '22 at 20:07

1 Answers1

-1

This will escape your input a bit and give you some error messages for debug. Unless you are sending the email to yourself you need a $to variable and you were missing a $subject , which is also required or will not work. This is a very simple way to do this. There are lots of other like looping through the $_POST for instance. I also suggest reading https://www.php.net/manual/en/function.mail.php

Example Fix to your question:

// you can make sure its set and escape it also. Must be cleaned when user input.

if(isset('submit')){

$naam = mysqli_real_escape_string($con, $_POST['naam']);
if(!isset($coops) || empty($coops)){
  $json_array['error'] = true;
  $json_array['message'] = "Coops not set.";
  $json = json_encode($json_array);
  echo($json);
  exit;
} 
$mail = mysqli_real_escape_string($con, $_POST['mail']);
if(!isset($mail) || empty($mail)){
  $json_array['error'] = true;
  $json_array['message'] = "mail not set.";
  $json = json_encode($json_array);
  echo($json);
  exit;
} 
  
$bericht = mysqli_real_escape_string($con, $_POST['bericht']);
if(!isset($bericht) || empty($bericht)){
  $json_array['error'] = true;
  $json_array['message'] = "bericht not set.";
  $json = json_encode($json_array);
  echo($json);
  exit;
}

$headers = "From: info@kongweb.be";
$myemail  = "info@kongweb.be"; 
$subject = "Your subject"; // was missing and must have
$message = "Hallo!
Wij ontvingen een contactformulier met de volgende gegevens:
Naam: $naam
E-mailadres: $mail
Message: $bericht
Einde van dit bericht.";
mail($myemail, $subject, $message); 
}
header('Location:bedankt.html');
exit();

are you sending the email to yourself. Otherwise you need a $to in place of you $myemail but without $subject being intialized it would never work. Redirect visitor to the thank you page

to Mail address to which you want to send mail Required subject Subject of the mail Required message Message to be sent with the mail. Each line of the message should be separated with a LF (\n). Lines should not be larger than 70 characters. Required

  • This assumes that the OP has opened a connection to MySQL, but there's no mention of that. Your calls to `mysqli_real_escape_string()`don't 'clean' anything. They escape certain characters for use with MySQL, and not necessarily appropriately for use in `mail()`. In this regard they're almost as bad a the OPs calls to `htmlspecialchars()`. Frankly, I doubt this will work any better than the OPs effort. – Tangentially Perpendicular Jun 09 '22 at 23:07
  • Why did you have to assume anything? And having made an unwarranted assumption, you go on to use an inappropriate function call to escape the incoming data, when escaping it may not be required. I see nothing here to suggest that this fixed the OPs problem. – Tangentially Perpendicular Jun 10 '22 at 01:34
  • If you run the code I provided with thier html they provided. The mail is sent. The user said ""Email not sending anymore form.php". I provided a answer to their questions. – HaleySocial Jun 10 '22 at 01:57