0

My contact form does not send emails here is my code:

HTML:

<div class="col-md-6">
  <form id="fcf-form-id" class="fcf-form-class" method="post" action="php/form-control.php">
    <div class="row">
      <div class="form-group col-md-6">
        <label for="Name">Twoje imię</label>
        <input type="Name" class="form-control" id="Name">
      </div>
      <div class="form-group col-md-6">
        <label for="Email">Twój telefon</label>
        <input type="Email" class="form-control" id="Email">
      </div>
    </div>
    <div class="form-group">
      <label for="Message">Wiadomość</label>
      <textarea type="Message" class="form-control" id="Message" rows="3"></textarea>
    </div>
    <button type="submit" class="btn font-weight-bold atlas-cta atlas-cta-wide cta-green my-3">Wyślij</button>
  </form>
</div>

PHP:

<?php
if (isset($_POST['Email'])) {
 
    // EDIT THE FOLLOWING TWO LINES:
    $email_to = "...@gmail.com";
    $email_subject = "Automedyk - Wiadomość z formularza";
 
    function problem($error)
    {
        echo "Przykro nam ale wystąpił błąd. ";
        echo "<br><br>";
        echo $error . "<br><br>";
        echo "Prosimy o powrót i poprawę błędów.<br><br>";
        die();
    }
 
    // validation expected data exists
    if (
        !isset($_POST['Name']) ||
        !isset($_POST['Email']) ||
        !isset($_POST['Message'])
    ) {
        problem('Przykro nam ale wystąpił błąd.');
    }
 
    $name = $_POST['Name']; // required
    $email = $_POST['Email']; // required
    $message = $_POST['Message']; // required
 
    $error_message = "";
    $email_exp = "/^[A-Za-z .'-]+$/";
 
    if (!preg_match($email_exp, $email)) {
        $error_message .= 'Nieprawidłowy adres email.<br>';
    }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
 
    if (!preg_match($string_exp, $name)) {
        $error_message .= 'Nieprawidłowe imię.<br>';
    }
 
    if (strlen($message) < 2) {
        $error_message .= 'Nieprawidłowa treść wiadomości.<br>';
    }
 
    if (strlen($error_message) > 0) {
        problem($error_message);
    }
 
    $email_message = "Form details below.\n\n";
 
    function clean_string($string)
    {
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
    }
 
    $email_message .= "Name: " . clean_string($name) . "\n";
    $email_message .= "Email: " . clean_string($email) . "\n";
    $email_message .= "Message: " . clean_string($message) . "\n";
 
    // create email headers
    $headers = 'From: ' . $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
?>

  <!-- INCLUDE YOUR SUCCESS MESSAGE BELOW -->

  Dziękujemy za wiadomość, skontaktujemy się niebawem!

  <?php
}
?>

Have no idea how to fix it and start sending messages to my email

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Welcome to Stack Overflow! Please visit the [help], take the [tour] to see what and [ask]. Do some research - [search SO for answers](https://www.google.com/search?q=php+form+does+not+send+site%3Astackoverflow.com). If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [\[<>\]](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Nov 03 '22 at 13:14
  • Please do not try to bypass the requirements to post code – mplungjan Nov 03 '22 at 13:16
  • Errors in the network console? – mplungjan Nov 03 '22 at 13:17
  • And please confirm, are you just not receiving the email? Or is there an error? – Chris Haas Nov 03 '22 at 13:17
  • You don't seem to have done any investigation into this, or if you have, it isn't mentioned. There's no reports of symptoms or any tracing of the code to see where it starts to go wrong. We don't even know if it actually reaches the mail() command or not, and using @ to suppress error messages really isn't a clever move at all when you're having difficulties. https://www.atatus.com/blog/debugging-in-php/ has a simple guide to debugging with PHP. And https://stackoverflow.com/a/24644450/5947043 discusses all the things which could go wrong when trying to send the email and ensure delivery – ADyson Nov 03 '22 at 13:18
  • 1
    If I had to guess though, I'd say it's probably not even getting past `if (isset($_POST['Email'])) {`, because your ` – ADyson Nov 03 '22 at 13:20
  • id is used for html dom access via javascript, you have to use `name="Email"` in your inputs. – Foobar Nov 03 '22 at 13:20
  • @anyber ...probably the wrong dupe target, the code almost certainly isn't even getting that far - see my comment above. I already linked the OP to the main answer on that page though, should they need it later once they've fixed the HTML form. – ADyson Nov 03 '22 at 13:24
  • 1
    i've added name to each one of those fields, and then form almost works, i get positive result from the form, so php goes futher, but still no message on my mail – n0izeone Nov 03 '22 at 13:29
  • 1
    @ADyson Yeah, I realized that after I closed it. Oops. But it sounds like it might still help after the latest comment. – aynber Nov 03 '22 at 13:31
  • Ok, now you need to start looking at https://stackoverflow.com/a/24644450/5947043 . `$headers = 'From: ' . $email . "\r\n" .` is a red flag to begin with. Attempting to impersonate someone else's email account is a great way to make your message look like spam or spoofing. A mailserver can tell the email didn't come from the server whose domain matches the supplied From address. Use a static From address from your own domain, and if you need to see the email entered on the contact form, you can put it in the email body, and/or in the Reply-To header, if you need to reply to it directly. – ADyson Nov 03 '22 at 13:34
  • I'vchanged it up to : 'From: webmaster@example.com' . "\r\n" . and 'Reply-To: webmaster@example.com' still no luck Maybe my host blocks this method? – n0izeone Nov 03 '22 at 14:07
  • Maybe. You'd have to ask them what they support. Or maybe something else goes wrong. Work your way through the rest of that answer. – ADyson Nov 03 '22 at 14:34
  • Thanks for your help man, post yourself as answer i;ll check u as helpful – n0izeone Nov 03 '22 at 15:49
  • The question is closed as a duplicate of the question I linked you to, so I can't post an answer. And any answer would just be largely telling you to go look at that link anyway! – ADyson Nov 03 '22 at 16:03

0 Answers0