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:

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
  • Missing a space there? `data-netlify="true"id="contact-form"` Does Netlify support .php files? I don't think so – brombeer Nov 07 '19 at 12:55
  • Oh I didn't think of netlify not supporting php... can I use JS instead? – Sparlarva Nov 07 '19 at 12:56
  • https://docs.netlify.com/forms/setup/#html-forms might help – brombeer Nov 07 '19 at 12:56
  • echo doesn't output to the browser's console, it outputs HTML (or text, or JS code or whatever you like) in the body of the response, which is then rendered in the main browser window. Anyway...please don't ask multiple questions about the same thing. https://stackoverflow.com/questions/58747684/no-email-being-sent-when-using-php-form – ADyson Nov 07 '19 at 13:33
  • Possible duplicate of [No email being sent when using PHP Form](https://stackoverflow.com/questions/58747684/no-email-being-sent-when-using-php-form) – ADyson Nov 07 '19 at 13:34

0 Answers0