html – My php form stopped working. It was working fine before,-ThrowExceptions

Exception or error:

Here is my HTML:

<form method="POST"  action="php/enigma.php" class="input-focus-label-highlight input-label-focus-color-FFFFFF">
    <span class="form-input-label input-focus-label font-family-1 color-666" data-pb-editable-text=""><span style="color:#ffffff;">Name:<br>​​​​​​​</span><br></span>
    <div>
        <div class="form-input-field"><input class="font-family-1 rel-color-464646 rel-border-color-E7E7E7" type="text"  id="name"  name="name" placeholder="" required style="
            margin-bottom: 20px;"></div>
        </div>
    <span class="form-input-label input-focus-label font-family-1 color-666" data-pb-editable-text=""><span style="color:#ffffff;">Email Address:<br>​​​​​​​</span><br></span>
    <div>
        <div class="form-input-field"><input class="font-family-1 rel-color-464646 rel-border-color-E7E7E7" type="email"  id="email"  name="email" placeholder="" required></div>
        <div class="form-submit-button"><button class="background-color-FFFFFF background-color-hover-ECF8FF" type="submit" name="submit"><i class="fa fa-arrow-right color-FFFFFF" style="color: rgb(0, 0, 0);"></i></button></div>
    </div>
</form>

Here is my PHP:

    <?php

    $email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);

    if(
    empty($email))   {
        echo "Please fill in the required (*) fields";
        exit;
    }

    $headers = "MIME-Version: 1.0\r\n";
    $headers = "Content-type: text/plain; charset=utf-8\r\n";
    $headers = "From: ". $email ."" . "\r\n";
    'Reply-To: ". $email ."' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


    mail( 'email@email.com', 'New Email from  Website' ,
    "Email: $email \r\n", "Name: $name \r\n", $headers );

     header('Location:https://google.com');

    ?>

My form was working perfectly fine. I added the “Name” input field and it just stopped working. I removed the name input field again to test if it will work but it does not work anymore.

The header function executes correctly. The mail function seems to be the issue. I tried with a server and a localhost and still no luck.

How to solve:

You’re passing Name as a header to mail() instead of part of the body, and sending the $headers as “additional parameters”. To illustrate, let’s put each parameter on its own line:

mail(
  'email@email.com',
  'New Email from  Website' ,
  "Email: $email \r\n",
  "Name: $name \r\n",
  $headers
);

It looks like you meant to combine the “Email” and “Name” strings into the body:

mail(
  'email@email.com',
  'New Email from  Website' ,
  "Email: $email \r\nName: $name \r\n",
  $headers
);

Leave a Reply

Your email address will not be published. Required fields are marked *