I have a simple .html contact form with recaptcha added to it and I have a correspond .php file set up to send the populated form fields information back to me via my email. After the form has been submitted, I redirect the user to my homepage. I'm trying to figure out what code is needed to add the recaptcha verification to the .php file and where, while still retaining the functionality I already have set up.
html code:
<script src='https://www.google.com/recaptcha/api.js' async defer>
</script>
<form method="post" action="mail.php">
<input id="name" name="name" placeholder="Name" required />
<input id="email" name="email" placeholder="Email" type="email" required />
<textarea id="message" name="message" placeholder="Question/Comment" required></textarea>
<div class="g-recaptcha" data-sitekey="MY SITE KEY"></div>
<input class="btn-success formBtn" name="submit" type="submit" />
<input class="formBtn" type="reset" />
</form>
php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "MY EMAIL";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or
die("Error!");
header('Location: /');
?>
your html code seems good, you can try the following php code to see if it works with your form. also make sure you are using the v2 reCaptcha key.
php code
<?php
if(isset($_REQUEST['submit'])){
$captcha = $_REQUEST['g-recaptcha-response'];
$handle = curl_init('https://www.google.com/recaptcha/api/siteverify');
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, "secret=YOUR_SECRET_KEY&response=$captcha");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
$explodedArr = explode(",",$response);
$doubleExplodedArr = explode(":",$explodedArr[0]);
$captchaConfirmation = end($doubleExplodedArr);
if(trim($captchaConfirmation) == "true") {
$to ='example#email.com';
$subject = 'Form';
$headers = "From: " . $_POST['email'] . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '';
$message .= "<strong>First Name:</strong> " .$_POST['name'] ;
$message .= "<strong>Email:</strong> " . $_POST['email'];
$message .= "<strong>Message:</strong> " . $_POST['message'] ;
$send = mail($to, $subject, $message, $headers);
if($send) {
echo "Message Sent. Thank You!";
} else {
echo "<script> alert('Message Not Sent. Please try again.";
}
} else {
echo "Captcha entry was wrong. Please try again.";
}
}
?>
Related
I receive an email but it will read from unknown. Everything else seems to work. Here is a link to the page link I don't know much about php.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.html?mailsend");
}
?>
<form action="contactform.php" method="post" onsubmit="return confirm('Are you sure you want to submit this form?');">
<input type="text" name="name" placeholder="Name" required="required">
<input type="text" name="email" placeholder="Email" required="required">
<textarea name="message" placeholder="Write message here..." required="required"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</form>
In this you have mention this `$headers = "From: ".$mailFrom; but not $mailFrom found. Use $email in place of $mailFrom.
I changed the code. You guys were right. Thank you!
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsend");
}
?>
I have created a simple html/php form where visitors on my site can write their name, email and message and then send the message to my email. Problem is that when they submit the email, my site then performs a full refresh (it looks like) and therefore just reloads to the top of my site. I would like for the user to remain at the same scroll position after submit, so that they can instantly see whether the submit was succesful or not. So either a solution that prevents the refresh or some other solution that automatically scrolls down vertical to the form.
Can you tell me if this is possible using php? Or do I have to use some jquery/ajax solution?
Below is the code I am using. I am a complete novice, so please be gentle.
<form action="" method="post" id="form">
<div class="contact-info-group">
<label for="name"><span>Your name</span>
<input type="text" id="name" name="name" autocomplete="off" value="<?php echo $name; ?>"></label>
<label for="email"><span>Your email</span>
<input type="email" id="email" name="email" autocomplete="off"></label>
</div>
<label for="message"><span>Your message</span>
<textarea id="message" name="message"></textarea></label>
<input id="button1" type="submit" class="button next" name="contact_submit" value="Send message">
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die();
}
if (!$name || !$email || !$msg) {
echo '<div class="contact-warning"><h2>! Error - Please note that all of the above fields are required !</h2></div>';
exit;
}
// Add the recipient email to a variable
$to = "email#email.com";
// Create a subject
$subject = "Message via website.com - $name";
// Construct the message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message: \r\n\r\n$msg";
// Clean up the message
$message = wordwrap($message, 72);
// Set the mail headers into a variable
$headers = "MIME-Version 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $message, $headers);
echo '<div class="contact-warning"><h2>Thank you for your message. We will get back to you shortly.</h2></div>';
}
?>
</form>
I am trying to create a simple contact form that takes user-inputted data and e-mails it to me. however, when I use the following code and the user presses submit, the page is redirected to a blank page (/form.php) - not even the "Thank you!" shows up on it, nor is the e-mail even sent. Can someone point out any errors I'm making? thanks!
PHP:
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message = "
Name: $name
E-mail: $email
Message:
$message
";
/* Sends to e-mail. */
mail($myEmail, $subject, $message, "hello");
?>
Thank you!
<?php
}
?>
HTML:
<form id="form" method="post" name="contact-form" action="form.php">
Name: <br>
<input type="text" name="name" /><br><br>
Email:<br>
<input type="text" name="email" /><br><br>
Message:<br>
<textarea name="message" placeholder="Tell me anything!"></textarea><br><br>
<input type="submit" value="Send">
</form>
You're declaring $message with your message (as part of the mail() header) then you're including $message inside it, in turn hashing and bashing your header.
Try this method instead.
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message2 = "
Name: $name
E-mail: $email
Message:
$message
";
/* Sends to e-mail. */
if(mail($myEmail, $subject, $message2, "hello")){
echo "<b>Thank you</b>"; // HTML bold text.
}
else{
echo "<h2>Sorry, there was an error.</h2>";
}
}
?>
If you want to send email as HTML you need to use the following:
$headers = "From: $email" . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
then change what is above:
if(mail($myEmail, $subject, $message2, "hello"))
to read as:
if(mail($myEmail, $subject, $message2, $headers))
Fixed 2 problems.
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message = "
Name: ".$name."
E-mail: ".$email."
Message:
".$_POST['message']."
";
/* Sends to e-mail. */
mail($myEmail, $subject, $message);
?>
Thank you!
<?php
}
?>
I've tried every php contact form tutorial but I can't get any of them to send the email properly. They all just show the code but don't send the email. This is the HTML code I have right now:
<form method="POST" action="scripts/contact.php">
<input id="name" name="name" placeholder="Name*" type="text" /> <br>
<input id="subject" name="subject" placeholder="Subject" type="text" /> <br>
<input id="email_address" name="email" placeholder="Email address*" type="text" /> <br>
<textarea id="message" name="message" placeholder="Your message*"></textarea>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
and this is the php I have:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = "From: $email" . $email;
mail($name,$email,$subject,$message,$headers);
echo "Mail Sent.";
?>
I have decent knowledge of HTMl but next to none of PHP. Any help would be greatly appreciated.
first you be sure that php file is exist in path scripts/contact.php
second look at phpinfo or php.ini for find is smptp server was setting or not
use following php code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$email. "\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully.";
}
else
{
echo "Error in Mail Sent.";
}
?>
I have an Contact us page on my website. what i want is when someone fills the form and click on send button. The message should be arrived to my gmail. i wrote the following code for it. its not working. is there any other way i can accomplish the same.
Html code:
<form id="ContactForm" action="contacts.php" method="post">
<div>
<div class="wrapper"> <strong>Name:</strong>
<div class="bg">
<input type="text" class="input" name="name">
</div>
</div>
<div class="wrapper"> <strong>Email:</strong>
<div class="bg">
<input type="text" class="input" name="email">
</div>
</div>
<div class="textarea_box"> <strong>Message:</strong>
<div class="bg">
<textarea cols="1" rows="1" name="message"></textarea>
</div>
</div>
<span>Send</span> <span>Clear</span> </div>
</form>
php code
<?php
session_start();
$to = "someemail#gmail.com";
$subject = "Someone Tried to contact you";
$message = $_POST['message'];
$fromemail = $_POST['email'];
$fromname = $_POST['name'];
$lt= '<';
$gt= '>';
$sp= ' ';
$from= 'From:';
$headers = $from.$fromname.$sp.$lt.$fromemail.$gt;
mail($to,$subject,$message,$headers);
echo "mail sent";
exit();
?>
Firstly, you should check your inputs for PHP injection.
$message = stripslashes($_POST['message']);
$fromemail = stripslashes($_POST['email']);
$fromname = stripslashes($_POST['name']);
Apart from that, there doesn't seem to be anything wrong with your mail script. The problem is most likely caused from your PHP server. Does your web hosting definitely provide PHP mail? Most free web hosts do not provide this as they are often used for spamming.
Sorry, but your code is crappy (especially, those concatenations). Use Swift mailer which provides OOP-style and does all the header job for you. And make sure you've got any mail server installed (did you check if you have any?).
PHP form:
<?php
header( 'Content-Type: text/html; charset=utf-8' );
// Your Email
$receiver = 'max.mustermann#domain.tld';
if (isset($_POST['send']))
{
$name = $_POST['name']
$email = $_POST['email'];
if ((strlen( $_POST['subject'] ) < 5) || (strlen( $_POST['message'] ) < 5))
{
die( 'Please fill in all fields!' );
}
else
{
$subject = $_POST['subject'];
$message = $_POST['message'];
}
$mailheader = "From: Your Site <noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "Reply-To: " .$name. "<" .$email. ">\r\n";
$mailheader .= "Return-Path: noreply#" .$_SERVER['SERVER_NAME']. "\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/plain; charset=UTF-8\r\n";
$mailheader .= "Content-Transfer-Encoding: 7bit\r\n";
$mailheader .= "Message-ID: <" .time(). " noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n\r\n";
if (#mail( $receiver, htmlspecialchars( $subject ), $message, $mailheader ))
{
echo 'Email send!';
}
}
?>
HTML form:
<form action="mail.php" method="post">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Message: <textarea name="message" cols="20" rows="2"></textarea><br />
<input name="send" type="submit" value="Send Email" />
</form>