Getting Duplicate emails after filling bootstrap contact form - php

i am first time asking here, I don't know php coding ,i have one contact form on my webpage ,after filling which i get emails but twice in my inbox, I really don't know what changes should make in scripting here.i have added recptcha code here too by refering videos and docs but in php scripting there getting problem after submitting form
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['tel']) && isset($_POST['loc']) && isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$loc = $_POST['loc'];
$message = $_POST['message'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Kindly provide valid Email Address.";
} else {
$body = $name . "\n" . $email . "\n" . $tel . "\n" . $loc . "\n" . $message;
if (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email)) {
echo (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email))."Thanks for contacting us.";
} else {
echo "<script>alert('Sorry!, there is a problem in sending email. Please call us on +61 452 534 200')</script>";
}
}
} else {
echo 'Message could not be sent.';
}
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curlx, CURLOPT_HEADER, 0);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlx, CURLOPT_POST, 1);
$post_data =
[
'secret' => '6LfRXOEhAAAAAOWyRGugvbPdYOhkztlz1aMcEwKP', //<--- your reCaptcha secret key
'response' => $_POST['g-recaptcha-response']
];
curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
$resp = json_decode(curl_exec($curlx));
curl_close($curlx);
if ($resp->success)
{
echo "<script>alert('Thanks For connecting us..we will get back to you soon..')</script>";
} else
{
// failed
echo "<script>alert('Please verify you are human..')</script>";
exit;
}
?>
<div class="col-md-6 padding-15">
<div class="contact-form">
<!-- https://brickboys.com.au/contact.php -->
<form action ="https://brickboys.com.au/contact.php"
method="post" id="ajax_form" class="form-horizontal">
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="text" id="name" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="col-sm-6">
<input type="email" id="email" name="email" class="form-control" placeholder="Email" required>
</div>
</div>
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="tel" id="tel" name="tel" class="form-control" pattern="[0-9]{10}" placeholder="Phone 0452534200" required>
</div>
<div class="col-sm-6">
<input type="text" id="loc" name="loc" class="form-control" placeholder="Project Location">
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<textarea id="message" name="message" cols="30" rows="5" class="form-control message" placeholder="Message" required></textarea>
</div>
<div class="g-recaptcha col-sm-6 mt-2" data-sitekey="6LfRXOEhAAAAAJMek8MJn-kTKIzO-AKfg7JIp3zu"></div>
</div>
<div class="form-group row">
<div class="col-md-12">
<button id="submit" class="default-btn" type="submit">Send Message</button>
</div>
</div>
<div id="form-messages" class="alert" role="alert"></div>
</form>
</div>
</div>
</div>
</div>
</section>

if (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email)) {
echo (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email))."Thanks for contacting us.";
}
The problem in these lines you're calling mail() function within the if statement. You just need to echo your message there doesn't need to call the mail function again.
This is how you'll write your if statement.
if (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email)) {
echo "Thanks for contacting us.";
}

Related

want to render back to page after submiting form and form shoud be clear

i have this contact form and php file to submit form, but in this, when i submit form it displays script alert msg about success and failure but i need to go back tp page, where form fields are still filled with info. instead of this i want that form should show alert on same page and form field shoud be cleared. how to do it? i am very beginner in php. dont know where to update code and how.ajax,json and PHP used in form
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['tel']) && isset($_POST['loc']) && isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$loc = $_POST['loc'];
$message = $_POST['message'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Kindly provide valid Email Address.";
} else {
$body = $name . "\n" . $email . "\n" . $tel . "\n" . $loc . "\n" . $message;
if (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email)) {
echo "Thanks for contacting us.";
}
else {
echo "<script>alert('Sorry!, there is a problem in sending email. Please call us on +61 452 534 200')</script>";
}
}
} else {
echo 'Message could not be sent.';
}
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curlx, CURLOPT_HEADER, 0);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlx, CURLOPT_POST, 1);
$post_data =
[
'secret' => '6LfRXOEhAAAAAOWyRGugvbPdYOhkztlz1aMcEwKP', //<--- your reCaptcha secret key
'response' => $_POST['g-recaptcha-response']
];
curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
$resp = json_decode(curl_exec($curlx));
curl_close($curlx);
if ($resp->success)
{
echo "<script>alert('Thanks For connecting us..we will get back to you soon..')</script>";
} else
{
// failed
echo "<script>alert('Please verify you are human..')</script>";
exit;
}
?>
<div class="col-md-6 padding-15">
<div class="contact-form">
<!-- https://brickboys.com.au/contact.php -->
<form action ="https://brickboys.com.au/contact.php"
method="post" id="ajax_form" class="form-horizontal">
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="text" id="name" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="col-sm-6">
<input type="email" id="email" name="email" class="form-control" placeholder="Email" required>
</div>
</div>
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="tel" id="tel" name="tel" class="form-control" pattern="[0-9]{10}" placeholder="Phone 0452534200" required>
</div>
<div class="col-sm-6">
<input type="text" id="loc" name="loc" class="form-control" placeholder="Project Location">
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<textarea id="message" name="message" cols="30" rows="5" class="form-control message" placeholder="Message" required></textarea>
</div>
<div class="g-recaptcha col-sm-6 mt-2" data-sitekey="6LfRXOEhAAAAAJMek8MJn-kTKIzO-AKfg7JIp3zu"></div>
</div>
<div class="form-group row">
<div class="col-md-12">
<button id="submit" class="default-btn" type="submit">Send Message</button>
</div>
</div>
<div id="form-messages" class="alert" role="alert"></div>
</form>
</div>
</div>
</div>
</div>
</section>
<!-- begin snippet: js hide: false console: true babel: false -->

Some issue in sending email using php email function

I am trying to send an email from a enquiry form available on a catalogue website on which I am working and found a strange issue.
On the same domain I have a file to test the email function with following code in it:
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
and its working fine. But the same code is not working on the index.php page where I have the enquiry form and also not showing any error at all (I have checked the error log. I have also tried by putting this code in a separate file and by including it on index.php but the same result).
<form name="frm_enquiry" id="frm_enquiry" method="post" autocomplete="off">
<div class="row">
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="fullname">Name:</label>
<input type="text" class="form-control" id="fullname" name="fullname" placeholder="Enter your name">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="mobile">Mobile No:</label>
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Enter your mobile number">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label style="width: 100%;" for="mobile"> </label>
<button type="submit" class="btn btn-block btn-primary" name="submit">Submit</button>
</div>
</div>
</div>
</form>
php:
if( isset( $_REQUEST['submit'] ) )
{
$name = $_REQUEST['fullname'];
$email = $_REQUEST['email'];
$mobile = $_REQUEST['mobile'];
echo $name . ' : ' . $email . ' : ' . $mobile . '<br>';
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
}
Someone please guide me, what I am doing wrong here, I am stuck on this from last 2 days.
Thanks in advance.

PHP X-Mailer not sending all fields

I am by no means a developer. Self-taught, but can usually wing it enough to make things happen. Working on company website for boss, and having trouble with the form. Regardless of how I try to define the PHP variable, SOME are not sending the input value to the e-mail. I'm sure I'm missing something simple, thank you in advance for your help! This is what's not showing:
Website:
Average Monthly Volume:
Preferred Contact Method:
Here is what I have so far:
HTML FORM:
<div class="product-screens2">
<div style="padding:200px">
<div class="form">
<div id="sendmessage">Your message has been sent. Thank you!</div>
<div id="errormessage">Please retry.</div>
<form action="form-email" method="post" role="form" class="contactForm">
<div class="form-row">
<div class="form-group col-lg-6">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group col-lg-6">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Business Name" />
</div>
<div class="form-group col-lg-6">
<input type="text" name="name" class="form-control" id="business_site" placeholder="Business Website" />
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="name" id="avgvolume" placeholder="Average Monthly Volume" />
</div>
<div class="form-group col-lg-6">
<input type="text" class="form-control" name="name" id="contactmethod" placeholder="Preferred Contact Method" />
</div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Brief Description of Goods or Services Sold"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><button type="submit" title="Send Message">SUBMIT</button></div>
</form>
</div></div>
</div>
PHP MAILER SCRIPT
<?php
/***************** Configuration *****************/
// Enter your email, where you want to receive the messages.
$contact_email_to = "Support#XXXXX.com";
// Subject prefix
$contact_subject_prefix = "Message From XXXXX Website: ";
// Name too short error text
$contact_error_name = "Name is too short or empty!";
// Email invalid error text
$contact_error_email = "Please enter a valid email!";
// Subject too short error text
$contact_error_subject = "Subject is too short or empty!";
// Message too short error text
$contact_error_message = "Too short message! Please enter something.";
/********** Do not edit from the below line ***********/
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die('Sorry Request must be Ajax POST');
}
if(isset($_POST)) {
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$business_site = $_POST["business_site"];
$avgvolume = $_POST["avgvolume"];
$contactmethod = $_POST["contactmethod"];
if(strlen($name)<4){
die($contact_error_name);
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
die($contact_error_email);
}
if(strlen($message)<3){
die($contact_error_subject);
}
if(strlen($message)<3){
die($contact_error_message);
}
if(!isset($contact_email_from)) {
$contact_email_from = "contactform#" . #preg_replace('/^www\./','', $_SERVER['SERVER_NAME']);
}
$sendemail = mail($contact_email_to, $contact_subject_prefix . $subject,
"Name: $name" . PHP_EOL .
"Reply-To: $email" . PHP_EOL .
"Business: $subject" . PHP_EOL .
"Website: $business_site" . PHP_EOL .
"Average Monthly Volume: $avgvolume" . PHP_EOL .
"Preferred Contact Method: $contactmethod" . PHP_EOL .
"Business Description: $message" . PHP_EOL .
"X-Mailer: PHP/" . phpversion()
);
if( $sendemail ) {
echo 'OK';
} else {
echo 'Could not send mail! Please check your PHP mail configuration.';
}
}
?>

Page is not redirecting to the php page from Html, when send button click in the web form?

When I try to click sent button, the content from the webpage is not redirecting to the .php page.I am using recaptcha in the form .Can you please help me to solve this issue..
my HTML code is:
<form action="sendform.php" id="contact-form" class="form-horizontal"
method="post">
<fieldset>
<div class="form-group">
<label class="col-sm-4 control-label" for="name">Your Name</label>
<div class="col-sm-8">
<input type="text" placeholder="Your Name" class="form-control" name="name" id="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="email">Email Address</label>
<div class="col-sm-8">
<input type="text" placeholder="Enter Your Email Address" class="form-control" name="email" id="email">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="subject">Subject</label>
<div class="col-sm-8">
<input type="text" placeholder="Subject" class="form-control" name="subject" id="subject" list="exampleList">
<datalist id="exampleList" >
<option value="a">A</option>
<option value="b">B Combo</option>
</datalist>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="message">Your Message</label>
<div class="col-sm-8">
<textarea placeholder="Please Type Your Message" class="form-control" name="message" id="message" rows="3"></textarea>
</div>
</div>
<div class="col-sm-8" class="form-group" class="g-recaptcha" data-sitekey="xxxxxxyyyyyyy"></div>
<div class="col-sm-offset-4 col-sm-8">
<button type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l">Submit</button>
<button type="reset" class="btn btn-primary">Cancel</button>
</div>
</fieldset>
</form>
And my PHP Code sendform.php
<?php
if (isset($_POST['submit']) && !empty($_POST['submit'])):
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'xxxxxxxxxxxxx';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if ($responseData->success):
$to = "aaa#abc.com"; // this is your Email address
$from = !empty($_POST['email']) ? $_POST['email'] : ''; // this is the sender's Email address
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$subject = !empty($_POST['subject']) ? $_POST['subject'] : '';
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From:" . $from;
$headers .= 'From:' . $name . ' <' . $from . '>' . "\r\n";
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
$succMsg = 'Your request have submitted successfully.';
else:
$errMsg = 'Robot verification failed, please try again.';
endif;
else:
$errMsg = 'Please click on the reCAPTCHA box.';
endif;
else:
$errMsg = '';
$succMsg = '';
endif;
?>
I have tested your code and it works.
Please make sure that you have placed your html and php files in same directory and also your files should be served via a local running server.
So your url should look like this http://localhost/testing/index.html
Although, your sendform.php gives me captcha error ofcourse.
"Please click on the reCAPTCHA box."

Form does not check reCaptcha

I have a form that I want to add ReCaptcha to, however, somewhere along the line something isnt working correctly because my form will still submit whether or not reCaptcha is verified.
This is the form:
<?php if(isset($_GET[ 'CaptchaPass'])){ ?>
<div>Thank you! Your Form was Successfully Submitted</div>
<?php } ?>
<?php if(isset($_GET[ 'CaptchaFail'])){ ?>
<div>Captcha Error. Please verify that you are human!</div>
<?php } ?>
<form action="http://vmobileautoglass.com/php/func_contact.php">
<label>Name</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="name">
</div>
</div>
<label>Email
<span class="color-red">*</span>
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="email">
</div>
</div>
<label>Phone
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="phone">
</div>
</div>
<label>Message</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-8 col-md-offset-0">
<textarea rows="8" class="form-control" name="message"></textarea>
</div>
</div>
<div class="g-recaptcha" data-sitekey="MYSITEKEYFROMGOOGLE"></div>
<p>
<button type="submit" class="btn btn-primary" name="ContactButton">Send Message</button>
</p>
</form>
This goes at the very top of the page where the form is located:
<?php
if (isset($_POST['ContactButoon'])) {
$url = 'https://google.com/recaptcha/api/siteverify';
$privatekey = "MYPRIVATEKEYFROMGOOGLE";
$response = file_get_contents($url . "?secret=" . $privatekey . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$date = json_decode($response);
if (isset($data->success) AND $data->success == true) {
header('Location: contact.php?CaptchaPasss=True');
} else {
header('Location: contact.php?CaptchaFail=True');
}
}
?>
And this is the forms functionality:
// Receiving variables
#$pfw_ip = $_SERVER['REMOTE_ADDR'];
#$name = addslashes($_GET['name']);
#$email = addslashes($_GET['email']);
#$phone = addslashes($_GET['phone']);
#$message = addslashes($_GET['message']);
// Validation
if (strlen($name) == 0) {
header("Location: http://vmobileautoglass.com/php/err_name.php");
exit;
}
if (strlen($email) == 0) {
header("Location: http://vmobileautoglass.com/php/err_email.php");
exit;
}
if (strlen($message) == 0) {
header("Location: http://vmobileautoglass.com/php/err_message.php");
exit;
}
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = "vMobile Contact Form";
$pfw_email_to = "vmobileag#gmail.com";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "name: $name\n"
. "email: $email\n"
. "phone: $phone\n"
. "message: $message\n";
#mail($pfw_email_to, $pfw_subject, $pfw_message, $pfw_header);
header("Location: http://vmobileautoglass.com/php/successform.php");
You have a typo:
$date = json_decode($response);
if(isset($data->success) AND $data->success==true){
$date should be $data.

Categories