0

I am trying to use a simple php form to submit emails. As extra information I am hosting on netlify.

The problem is, nothing is echoing in the browser/console and no email is being received and i dont have an error message anywhere, all I get is a message from netlify saying this - after i click 'submit' : enter image description here

The code:

            <form name="captureClientContactPage" data-netlify="true"id="contact-form" class="contact__form mt-5" method="post" action="mail.php">
         <!-- form message -->
            <div class="row">
                <div class="col-12">
                    <div class="alert alert-success contact__msg" style="display: none" role="alert">
                        Your message was sent successfully.
                    </div>
                </div>
            </div>
            <!-- end message -->
            <div class="form-row">
                <div class="col-lg-12">
                     <div class="form-group-2 mb-4">
                        <textarea name="message" class="form-control" rows="6" placeholder="Your Message"></textarea>
                    </div>

                    <div class="text-center">
                        <button class="btn btn-main" name="submit" type="submit">Send Message</button>
                    </div>
                </div>
            </div>
        </form>

Then this is the php file mail.php:

<?php

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        # FIX: Replace this email with recipient email
        $mail_to = "myemail@myemail.com";

        # Sender Data
        // $subject = trim($_POST["subject"]);
        $name = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["name"])));
        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
        $message = trim($_POST["message"]);

        if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($subject) OR empty($message)) {
            # Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo "Please complete the form and try again.";
            exit;
        }

        # Mail Content
        $content = "Name: $name\n";
        $content .= "Email: $email\n\n";
        $content .= "Message:\n$message\n";

        # email headers.
        $headers = "From: $name <$email>";

        # Send the email.
        $success = mail($mail_to,$content, $headers);
        if ($success) {
            # Set a 200 (okay) response code.
            http_response_code(200);
            echo "Thank You! Your message has been sent.";
        } else {
            # Set a 500 (internal server error) response code.
            http_response_code(500);
            echo "Oops! Something went wrong, we couldn't send your message.";
        }

    } else {
        # Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo "There was a problem with your submission, please try again.";
    }

?>

any help would be greatly appreciated. thanks very much.

If you need any more information please do ask,

Dale.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Sparlarva
  • 790
  • 1
  • 8
  • 30
  • None of the code you've posted will output the message shown in your screenshot. Is this even the correct version of the code? Have you omitted something? – ADyson Nov 07 '19 at 13:23
  • That's the message shown by netlify when you use a form. – Sparlarva Nov 07 '19 at 13:25
  • So that overrides your own echo statements then? I don't know how their specific software works or how it would integrate with what you've written. You could always log some data to disk so you can trace which path your own code is taking. I've added the netlify tag to your question in case anyone understands the specifics of that platform. In the meantime, read through the duplicate in case any of that needs correcting as well. At the moment though, we don't even know if your code is actually executing the `mail()` command, never mind anything else. – ADyson Nov 07 '19 at 13:29
  • Actually it seems Netlify is more of a platform than just a webhost where you can add any old code. https://docs.netlify.com/forms/submissions/#form-triggered-functions and https://docs.netlify.com/functions/trigger-on-events/ might be of interest to you...looks like you can use it to execute some code after netlify receives a form that someone has submitted. Looks like you'd have to write the function in NodeJS or Go, rather than PHP. – ADyson Nov 07 '19 at 14:12

0 Answers0