0

Hello fellow Stackoverflow members! I wrote this send form in PHP for my personal website, here's the code I wrote:

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

    $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"]);

    if(empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        header("HTTP/1.1 400 Bad Request");
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    $recipient = "montana@getprowl.com";

    $subject = "General questions for you";

    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message: \n$message\n";

    $email_headers = "From: $name <$email>";

    if(mail($recipient, $subject, $email_content, $email_headers)) {
        header("HTTP/1.1 200 OK");
        echo "Your message was sent successfully!";
    } else {
        header("HTTP/1.0 500 Internal Server Error");
        echo "Your message wasn\'t sent, please try again.";
    }

} else {
    header("HTTP/1.1 403 Forbidden");
    echo "There was a ploblem with your submission, please try again.";
}

When you enter information, I'll get the error that I wrote to popup which is Your message wasn't sent, please try again. which is indicative of a HTTP 500 error, I'm thinking this could be solved via adding something in ".htacces" but honestly not sure what to put in there, have tried a couple of things but to avail. Thank you guys, any suggestions help.

Montana
  • 53
  • 1
  • 10

1 Answers1

0

Mail function will not work in your local server. You need to configure the mail server to send emails using local server. Instead of that you can use gmail SMTP server to send emails.

You can follow this example to send emails using gmail SMTP.

https://www.formget.com/send-email-via-gmail-smtp-server-in-php/

SRK
  • 3,476
  • 1
  • 9
  • 23
  • Thank you for the suggestion Smit, tried some things suggested, but they did not work. – Montana Mar 19 '18 at 06:12
  • You must be missing something as gmail SMTP needs a setup to send emails, make sure you setup it correctly. @Montana – SRK Mar 19 '18 at 06:13