I have a working PHP email form that works about 10% of the time. I need to add authentication to make it work 100% of the time but I'm trying to use PHPMailer (https://github.com/PHPMailer/PHPMailer) and am getting errors and can't get it to work.
My basic working script without authentication:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("","
"),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)) {
echo "Oops! There was a problem with your message. Please check that all fields are filled in.";
exit;
}
$recipient = "myaddress@gmail.co";
$subject = "Enquiry from $name";
$email_content = "Name: $name
";
$email_content .= "Email: $email
";
$email_content .= "Message:
$message
";
$email_headers = "From: Enquiry <myenquiry@gmail.co>";
if (mail($recipient, $subject, $email_content, $email_headers)) {
echo "Thank You! Your message has been sent.";
} else {
echo "Oops! Something went wrong and we couldn't send your message.";
}
}
else {
echo "There was a problem with your submission, please try again.";
}
?>
Any help will be greatly appreciated!
Many thanks.