Contact form keeps sending me blank emails - php

I keep receiving blank form submissions from my contact form. The emails are all coming from "CGI Mailer", and have the form labels, but all the inputs are blank.
The form does work as it should otherwise.
<?php
$userName = $_POST['name'];
$userPhone = $_POST['phone'];
$userEmail = $_POST['email'];
$userMessage = $_POST['text'];
$honeypot = $_POST['firstname'];
$to = "email address";
$subject = "New Email From ";
$body = "Information Submitted:";
$body .= "\r\n Name: " . $userName;
$body .= "\r\n Phone " .$userPhone;
$body .= "\r\n Email: " . $userEmail;
$body .= "\r\n Message: " . $userMessage;
if( ! empty( $honeypot ) ){
return;
}else{
mail($to, $subject, $body);
}
header('Location: thanks.html');
?>
<form method="post" name="myForm" action="sendform.php">
<input name="firstname" type="text" id="firstname" class="hide-robot">
<input id="name" class="form-control" name="name" placeholder="Name..." required /><br />
<input id="phone" class="form-control" name="phone" placeholder="Phone..." required /><br />
<input id="email" class="form-control" name="email" placeholder="E-mail..." required /><br />
<textarea id="text" class="form-control" name="text" placeholder="How can we help you?" style="height:150px;" required></textarea><br />
<input class="btn btn-primary" type="submit" value="submit" id="submit"/><br /><br />
</form>

Related

pseudo captcha with two checkboxes one for humans one for bots

I am trying to create a pseudo captcha to stop spambots spamming my contact form. I have php so that it doens't allow the user to send if the form isn't filled out and instead gives a message. I'm trying to make it so that it doesn't send if the "I am a robot" checkbox is checked as well. Since bots tend to check all checkboxes, I figured this may work. I am pretty new to PHP so go easy on me.
I tried using !empty($robots) which didn't work. I also tried !isset($robots) I messed with a second IF statement.
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$client_message = $_POST['client_message'];
$to = "rtisticpictures#gmail.com";
$subject = "Client Message";
$cmessage = $_POST['client_message'];
$message = $first_name . " " . $last_name . "\n\n Phone:" . $phone . "\n\n email:" . $email . "\n\n" . $cmessage;
if(empty($first_name) || empty($last_name) || empty($phone) || empty($cmessage) || empty($email))
{
echo "Your message can not be sent without all fields filled in, or you have checked that you are a robot. We do not allow bots to send email.";
; // Note this
}
else
{
mail ($to, $subject, $message, "From: " . $first_name . $last_name);
echo "Your message has been Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
HTML FORM
<form action="contact_form.php" method="post" name="contact_form">
<p>First Name: <input name="first_name" type="text" />
Last Name: <input name="last_name" type="text" />
Phone Number: <input name="phone" type="text" placeholder="(999)999-9999" />
E-mail:<input name="email" type="text" placeholder="your#email.com" />
</p>
<p>
<textarea name="client_message" cols="5" rows="3" placeholder="Write your message here."></textarea>
</p>
<div class="6u 12u(2)">
<input type="checkbox" id="human" name="human" unchecked="">
<label for="human">I am not a robot.</label></div>
<div class="6u 12u(2)">
<input type="checkbox" id="robots" name="robots" unchecked="">
<label for="robots">I am a robot.</label>
</div>
<p>
<input type="submit" name="submit" id="submit" value="Send"/>
<input type="reset" name="reset" id="reset" value="Clear"/>
</p>
</form>
I would like it to send only when the 'human' checkbox is selected and the 'robots' checkbox is not selected.
If you want to check if checkbox is checked in PHP you should use isset, in your case:
if(empty($first_name) || empty($last_name) || empty($phone) || empty($cmessage) || empty($email) || !isset($_POST['robots']))
And full code:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$client_message = $_POST['client_message'];
$to = "rtisticpictures#gmail.com";
$subject = "Client Message";
$cmessage = $_POST['client_message'];
$message = $first_name . " " . $last_name . "\n\n Phone:" . $phone . "\n\n email:" . $email . "\n\n" . $cmessage;
if(empty($first_name) || empty($last_name) || empty($phone) || empty($cmessage) || empty($email) || !isset($_POST['robots']))
{
echo "Your message can not be sent without all fields filled in, or you have checked that you are a robot. We do not allow bots to send email.";
; // Note this
}
else
{
mail ($to, $subject, $message, "From: " . $first_name . $last_name);
echo "Your message has been Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
HTML FORM
<form action="contact_form.php" method="post" name="contact_form">
<p>First Name: <input name="first_name" type="text" />
Last Name: <input name="last_name" type="text" />
Phone Number: <input name="phone" type="text" placeholder="(999)999-9999" />
E-mail:<input name="email" type="text" placeholder="your#email.com" />
</p>
<p>
<textarea name="client_message" cols="5" rows="3" placeholder="Write your message here."></textarea>
</p>
<div class="6u 12u(2)">
<input type="checkbox" id="human" name="human" unchecked="">
<label for="human">I am not a robot.</label></div>
<div class="6u 12u(2)">
<input type="checkbox" id="robots" name="robots" unchecked="">
<label for="robots">I am a robot.</label>
</div>
<p>
<input type="submit" name="submit" id="submit" value="Send"/>
<input type="reset" name="reset" id="reset" value="Clear"/>
</p>

php contact form output issues

So I'm trying to make a contact me form for a website and this is my first time making one of these forms. My issue is that for some odd reason my button is not sending anything and the button doesn't want to work.
This is the input form
<div>
<form id="contact-us" method="post" action="contact-form-handler.php">
<input name="name" type="text"class="form-control" placeholder="Your name" required>
<br>
<input name="phone" type="number" class="form-control" placeholder="Phonenumber no symbols or dashes"required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your email" required>
<br>
<textarea name="message" class="form-control" placeholder="write us a message" rows="4" required></textarea>
<br>
<input type="submit" class="form-control-submit" value="SEND">
</form>
</div>
Here is my handler.
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = 'website.com';
$email_subject = 'New message from customer';
$email_body = "name: .$name. \n".
"email: .$visitor_mail.\n".
"phone: .$phone. \n".
"message: .$message. \n";
$to= "email#gmail.com";
$headers= $email_from.;
$headers .= $visitor_email.;
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");
?>
I'm not sure why this isn't working but if anyone could help me that would be amazing.
<div>
<form id="contact-us" method="post" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your name" required>
<br>
<input name="phone" type="number" class="form-control" placeholder="Phonenumber no symbols or dashes" required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your email" required>
<br>
<textarea name="message" class="form-control" placeholder="write us a message" rows="4" required></textarea>
<br>
<input name="submit" type="submit" class="form-control-submit" value="SEND">
</form>
</div>
Handler
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = 'website.com';
$email_subject = 'New message from customer';
$email_body = "name: {$name} \n" .
"email: {$visitor_email}\n" .
"phone: {$phone} \n" .
"message: {$message} \n";
$to = "email#gmail.com";
$headers = $email_from;
$headers .= $visitor_email;
$res = mail($to, $email_subject, $email_body, $headers);
header("Location: index.html");
}
I found a few errors in your code:
$headers= $email_from.; // syntax error - need to remove dot before ;
$headers .= $visitor_email.; // syntax error - need to remove dot before; same here
In your email body, you used $visitor_mail variable, but your is $visitor_email
Also, I've added the name tag to your form and added an additional validation condition.
Please try to use this solution, I hope it will help you. This solution is working fine on my side

Validating email form

What's the best solution to make the name and email fields compulsory in my form?
Can't seem to find a direct solution for my form and no expert.
I'm currently receiving loads of blank emails.
HTML code
<form name="enq" method="post" action="mail/myphpfile.php" onSubmit="return validation();">
<fieldset>
<input type="text" name="name" id="name" value="" class="input-block-level" placeholder="Name" />
<input type="text" name="email" id="email" value="" class="input-block-level" placeholder="Email" />
<textarea rows="11" name="message" id="message" class="input-block-level" placeholder="Comments"></textarea>
<div class="actions">
<input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-info pull-right" title="Click here to submit your message!" />
</div>
</fieldset>
</form>
PHP code
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="name#myemailaddress.co.za";
$subject="Enquiry from my website";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n" . "BCC:email#email1.co.za, email#email2.co.za, email#email3.co.za\r\n";
$message="
Name: $name
<br>
Email-Id:$email
<br>
Message:$query";
if(mail($to,$subject,$message,$headers))
header("Location:../thank-you.html");
else
header("Location:../error.html");
//contact:-email#myemail.co.za
}
?>
Try
<?php
if(isset($_POST['submit']) && !empty($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="name#myemailaddress.co.za";
$subject="Enquiry from my website";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n" . "BCC:email#email1.co.za, email#email2.co.za, email#email3.co.za\r\n";
$message="Name: $name <br>
Email-Id:$emai<br>
Message:$query ";
if(mail($to,$subject,$message,$headers))
header("Location:../thank-you.html");
else
header("Location:../error.html");
//contact:-email#myemail.co.za
}
use html5 required validation like this
<input type="text" name="name" id="name" value="" class="input-block-level" placeholder="Name" required/>
<input type="email" name="name" id="name" value="" class="input-block-level" placeholder="Name" required/>
In HTML5 this should work fine!
<input type="text" name="name" id="name" value="" class="input-block-level" placeholder="Name" required/>
<input type="email" name="emailField" id="name" value="" class="input-block-level" placeholder="Email" required/>
try this!!
if(isset($_POST['submit']))
{
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) && $email =="" ) {
$emailErr = "Enter valid email.";
}
else
{
$emailErr = "";
}
if( $emailErr==""){
mail($to,$subject,$message,$headers);
header("Location:../thank-you.html");
}
else
{
echo $emailErr;
header("Location:../error.html");
}
}

PHP Mail function didn't work

I have use php contact form for my web site.but it's doesn't work properly.when i fill all the field correctly and submit. it's display error message "Invalid data" .
<?php
$action=$_REQUEST['action'];
if ($action=="")
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30" required/><br>
Your email:<br>
<input name="email" type="email" value="" size="30" required /><br>
Your message:<br>
<textarea name="message" rows="7" cols="30" required></textarea><br> <br>
<input type="submit" value="Send email" class="topbarbtn"/>
</form>
<?php
$subject=$_REQUEST['name'];
$email=$_REQUEST['email'];
$body=$_REQUEST['message'];
}else{
$to = "abc#abc.com";
$subject = $subject;
$from = $email;
$message = $body;
if (($from=="")||($subject=="")||($message==""))
{
echo '<script type="text/javascript">alert("Invalid Details");</script>' ;
} else{
$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
?>
Like this it works:
<?php
$action=$_REQUEST['action'];
if ($action=="") {?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30" required/><br>
Your email:<br>
<input name="email" type="email" value="" size="30" required /><br>
Your message:<br>
<textarea name="message" rows="7" cols="30" required></textarea><br> <br>
<input type="submit" value="Send email" class="topbarbtn"/>
</form>
<?php
}
else {
$to = "...mail address...";
$subject = $_REQUEST['name'];
$from = $_REQUEST['email'];
$message = $_REQUEST['message'];
if (($from=="")||($subject=="")||($message=="")) {
echo '<script type="text/javascript">alert("Invalid Details");</script>' ;
}
else{
$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
}
?>
Noticed a flaw in your logics - you try to set $from as an $email, while you don't get a variable $email
in else statement add this lines:
$from = $_REQUEST['email'];
Plus if mail still is not sending you should considerthat some mail servers, such as qmail, will reject your message if it uses \r\n.
So you should try to use just \n or \n\n as a linebreak in a header.

Get Checkbox Values from Forms using an External PHP Processor

I have a form that sends its data to a process.php file for processing in that file I have the following code:
<?php
$name = $_GET['name'];
$number = $_GET['number'];
$email = $_GET['email'];
$comment = $_GET['message'];
$sales = $_POST['sales'];
$lettings = $_POST['lettings'];
$to = "me#me.com";
$subject = "New website registration";
$message = "Name: ".$name."\r\n";
if(isset($number) && $number!='')
$message.= "Number: ".$number."\r\n";
if(isset($email) && $email!='')
$message.= "Email: ".$email."\r\n";
if(isset($comment) && $comment!='')
$message.= "Comment: ".$comment."\r\n";
if(isset($sales))
{
$message.= "I am Interested in Sales" . "\r\n";
}else{
//
}
if(isset($lettings))
{
$message.= "I am Interested in Lettings";
}else{
//
}
$headers = "From: ".$name." <".$email.">";
$result = mail($to,$subject,$message,$headers);
return $result;
?>
The HTML for the form is as follows:
<form id="register_form" name="register" method="post" action="/content/contact/process.php">
<input type="text" id="name" name="name" class="regform" onFocus="if(this.value=='Name'){this.value='';}" onBlur="if(this.value==''){this.value='Name';}" value="Name" />
<input type="text" id="number" name="number" class="regform" onFocus="if(this.value=='Phone number'){this.value='';}" onBlur="if(this.value==''){this.value='Phone number';}" value="Phone number" />
<input type="text" id="email" name="email" class="regform" onFocus="if(this.value=='Email address'){this.value='';}" onBlur="if(this.value==''){this.value='Email address';}" value="Email address" />
<textarea id="message" name="message" class="regtext" onFocus="if(this.value=='Message'){this.value='';}" onBlur="if(this.value==''){this.value='Message';}">Message</textarea>
<label for"sales">Sales<input type="checkbox" name="sales" value="1" /></label>
<label for"sales">Lettings<input type="checkbox" name="lettings" value="1"/></label>
<input id="submit" type="submit" value="Submit" class="regsender" />
<div class="loading"></div>
</form>
When the checkboxes are checked, the expected messages do not come through in the email. Am I missing something here. I've tried several different approaches to getting this to work, none of which do. Any help would be appreciated. Thanks.
You are using method = POST and retrieving values with $_GET
I have updated your code check it
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$comment = $_POST['message'];
if (isset($_POST['sales']))
{
$sales = $_POST['sales'];
}
else if(isset($_POST['lettings']))
{
$lettings =$_POST['lettings'];
}
$to = "me#me.com";
$subject = "New website registration";
$message = "Name: ".$name."\r\n";
if(isset($number) && $number!='')
$message.= "Number: ".$number."\r\n";
if(isset($email) && $email!='')
$message.= "Email: ".$email."\r\n";
if(isset($comment) && $comment!='')
$message.= "Comment: ".$comment."\r\n";
if(isset($_POST['sales']))
{
$message.= "I am Interested in Sales" . "\r\n";
}
else if(isset($_POST['lettings']))
{
$message.= "I am Interested in Lettings";
}
echo $message;
$headers = "From: ".$name." <".$email.">";
$result = mail($to,$subject,$message,$headers);
return $result;
}
?>
<form id="register_form" name="register" method="post" action="testing_page.php">
<input type="text" id="name" name="name" class="regform" onFocus="if(this.value=='Name'){this.value='';}" onBlur="if(this.value==''){this.value='Name';}" value="Name" />
<input type="text" id="number" name="number" class="regform" onFocus="if(this.value=='Phone number'){this.value='';}" onBlur="if(this.value==''){this.value='Phone number';}" value="Phone number" />
<input type="text" id="email" name="email" class="regform" onFocus="if(this.value=='Email address'){this.value='';}" onBlur="if(this.value==''){this.value='Email address';}" value="Email address" />
<textarea id="message" name="message" class="regtext" onFocus="if(this.value=='Message'){this.value='';}" onBlur="if(this.value==''){this.value='Message';}">Message</textarea>
<label for"sales">Sales<input type="checkbox" name="sales" value="1" /></label>
<label for"sales">Lettings<input type="checkbox" name="lettings" value="1"/></label>
<input id="submit" name="submit" type="submit" value="Submit" class="regsender" />
<div class="loading"></div>
</form>

Categories