PHP Contact Form keeps getting spam - php

my contact form keeps getting spam everyday. Not sure why. Here is contact-process.php to process a submission:
<?php
// configure
$from = 'webmaster#example.com';
$sendTo = 'Message from <myemail#mydomain.com>';
$subject = "Contact Form: $name";
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$human = intval($_POST['humans']);
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// let's do the sending
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
header('Location: /thank-you.php');
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
//Check if simple anti-bot test is correct
if(!empty($_POST['humans'])) {
// it's spam
} else {
// it's human
}
I have no idea why this is happening, I also set up a honeypot as well. Also the emails are coming in as being sent from hostname, not from the individual email addresses themselves.
Thanks for the help guys.

Related

PHP Failure on Form

Apologies, I'm mostly self taught. I'm using the following code live on https://www.poadvisory.com/contact.html and the contact form appears to be working, ReCaptcha as well, but the email never comes. I've spent countless times on the phone with my hosting provider and they claim is a code issue and not a server issue. Surely it's something obvious I don't see. If anyone can help, I'd really appreciate it. I've searched the current questions but don't believe this is a duplicate question.
<?php
require('recaptcha-master/src/autoload.php');
$sendTo = 'Contact Form < my email goes here>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
$okMessage = 'Contact form successfully submitted. Thank you, We will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
$recaptchaSecret = 'removed';
error_reporting(E_ALL & ~E_NOTICE);
try {
if (!empty($_POST)) {
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
} catch (\Exception $e) {
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
} else {
echo $responseArray['message'];
}

How can I get the name, email and subject in this form become the actual values in the message I receive from the server?

How can I configure the variables $from & $subject on top so that the email the server sends me uses those values instead of static text?
<?php
// configure
$from = 'Demo contact form <demo#domain.com>';
$sendTo = 'Demo contact form <demo#domain.com>'; // Add Your Email
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'subject' => 'Subject', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
I want $from to use the Name value and $subject to use the Subject value.
Use the following:
# checks if the 'name' key has been $_POST'ed
$from = isset($_POST['name'])?$_POST['name']:$from;
# checks if the 'subject' key has been $_POST'ed
$subject = isset($_POST['subject'])?$_POST['subject']:$subject;

bootstrap mail form $from<$email>

I refer to this page.
https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
i want mailheader "name"
but this php code is
<?php
// configure
$from = 'Demo contact form <demo#domain.com>';
$sendTo = 'Demo contact form <demo#domain.com>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
so i tried
$sendTo = 'Demo contact form '; => my mail.
$from = 'Demo contact form '; => $name <'$email'>
but the result is "nobody"
So i tried again.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
=>
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from<$email>,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
But it failed...
what should i do?
i tried
From: $FromName and
$headers = "From: $from_user \r\n". and
$headers .= 'From: "'. $from . '" <' . $Email . '>' . "\r\n"; etc...
This worked for me
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$from = "$name<$email_address>";

PHP Contact Form - make the Reply-To to have the submitter email

I inspected different topics there, but all of them are not working with my code. I need to get the ability to answer direct to submitter email via my email. Here is my code:
<?php
// configure
$from = '<mymail#gmail.com>';
$sendTo = '<mymail#gmail.com>';
$subject = 'New message from PROMO form';
$fields = array('name' => 'Name', 'city' => 'City', 'tel' => 'Tel', 'email' => 'Email', 'message' => 'Message', 'age' => 'Age', 'info' => 'Info', 'checkboxtwo' => 'Checkboxtwo'); // array variable name
$okMessage = 'Спасибо.';
$errorMessage = 'Извините, ошибка.';
$headers = 'From: ' . $fields['email'] . "\r\n" . 'Reply-To: ' . $fields['email'];
try
{
$emailText = "You have new message from online form form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, $headers);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
I will highly appreciate any help. Thank you.
THE ANSWER
Re-write your foreach like this:
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
$fields[$key] = $value; //It set the values on the array
}
}
Then add the headers:
$headers = 'From: ' . $fields['email'] . "\r\n" . 'Reply-To: ' . $fields['email'];
So just send the mail.
mail($sendTo, $subject, $emailText, $headers);
Other answers (ignore them)
Just set the headers with the "Reply-To".
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from;
mail($sendTo, $subject, $emailText, $headers);
You will get any replies to the email that you used to submit the message.
EDIT
Get the desired email by using this:
$sendTo = $fields['email'];
Then you will be able to send the email using this var.

php if(!$variable) not working

Please forgive me if this is a dumb question, as I am new to php. I did some c++ years ago and are dabbling in c# with Unity.
I am trying to get a working html / js / php contact form working with reCaptcha. The form works well if the captcha is checked, and the 'success' message is displayed. If the captch is not checked, I want to display a message and not send the form email. The code snippet below works insofar as the email is NOT sent if the captcha is not ticked, but it does not display the message. Can anyone help?
$okMessage = 'Contact form successfully submitted. Thank you, we will get back to you soon!';
$captchaMessage = 'There was an error while submitting the form. Please check the captcha box.';
$spamMessage = 'There was an error while submitting the form. Spamers not welcome.';
$captcha = $_POST['g-recaptcha-response'];
try
{
//Sanitise Inputs
$emailText = "You have new message from contact form\n=============================\n";
$emailText .= "First Name: " . #trim(stripslashes($_POST['name'])). "\n";
$emailText .= "Last Name: " . #trim(stripslashes($_POST['surname'])). "\n";
$emailText .= "Company: " . #trim(stripslashes($_POST['company'])). "\n";
$emailText .= "Email: " . #trim(stripslashes($_POST['email'])). "\n";
$emailText .= "Message: " . #trim(stripslashes($_POST['message'])). "\n";
if(!$captcha){
$responseArray = array('type' => 'danger', 'message' => $captchaMessage);
exit;
}
$secretKey = "secret_key";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
$responseArray = array('type' => 'danger', 'message' => $spamMessage);
} else {
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
Your exit stop the script. So the message cant be displayed.
if(!$captcha){
$responseArray = array('type' => 'danger', 'message' => $captchaMessage);
}
else
{
$secretKey = "secret_key";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
$responseArray = array('type' => 'danger', 'message' => $spamMessage);
} else {
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
// Here the script continue

Categories