0

I purchased a 'template' website. I'm having 1 issue with the Contact/Form Submission page. When I press send, I do not receive an email at the intended email address. I'm not sure if this is an issue with the code (I'm guessing not, I would think this highly rated template would have had something like this correct),

index.html

<!--Contact-->
<div class="cr-contact-section section pt-150 pb-70">
    <div class="container">

        <!--Section Title-->
        <div class="row">
            <div class="cr-section-title text-center col-xs-12 mb-100">
                <h3>Get IN Touch</h3>
                <p>Feel free to send us a message! Let's grow your business together!.</p>
            </div>
        </div>

        <div class="row">

            <!--Contact Form-->
            <div class="col-md-10 col-md-offset-1 col-xs-12">

                <div class="cr-contact-form">

                    <form id="contact-form" action="mail.php" method="post">
                        <input type="text" placeholder="Name" name="name">
                        <input type="email" placeholder="Email" name="email">
                        <textarea placeholder="Message" name="message"></textarea>
                        <input type="submit" value="submit">
                    </form>

                    <p class="form-messege"></p>

                </div>

mail.php

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "post") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    $recipient = "sales@31creative.net";

    // Set the email subject.
    $subject = "New contact from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $email_content, $email_headers)) {
        // 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 and 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.";
}

?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Mr.X
  • 1
  • 1
    Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – El_Vanja Apr 14 '21 at 21:34
  • **NOTE** the PHP `mail()` function does not send mail, it passes mail to a real mail server, which you have to have installed and configured into the `php.ini` file for it all to work – RiggsFolly Apr 14 '21 at 21:37

0 Answers0