contact form, message is not recived - php

I need to make a contact form and I did, but when Im sending test message Im not reciving that message, I have check my email addres and test message never get there i also looked into spam folder but still no luck how to fix this? here is my code
<?php
$formMessage = "";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
if ($_POST['username']) { // If the form is trying to post value of username field
// Gather the posted form variables into local PHP variables
$senderName = $_POST['username'];
$senderEmail = $_POST['email'];
$senderMessage = $_POST['msg'];
// Make sure certain vars are present or else we do not send email yet
if (!$senderName || !$senderEmail || !$senderMessage) {
$formMessage = "The form is incomplete, please fill in all fields.";
} else { // Here is the section in which the email actually gets sent
// Run any filtering here
$senderName = strip_tags($senderName);
$senderName = stripslashes($senderName);
$senderEmail = strip_tags($senderEmail);
$senderEmail = stripslashes($senderEmail);
$senderMessage = strip_tags($senderMessage);
$senderMessage = stripslashes($senderMessage);
and here is part which im not sure about
// End Filtering
$to = "test#gmail.com";
$from = "contact#your_website_here.com";
$subject = "You have a message from your website";
// Begin Email Message Body
$message = "
$senderMessage
$senderName
$senderEmail
";
// Set headers configurations
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "Content-type: textrn";
// Mail it now using PHP's mail function
mail($to, $subject, $message, $headers);
$formMessage = "Thanks, your message has been sent.";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
} // close the else condition
} // close if (POST condition
?>
here is html form
<form id="form1" name="form1" method="post" action="contact.php">
<?php echo $formMessage; ?>
<br />
<br />
Your Name:<br />
<input name="username" type="text" id="username" size="36" maxlength="32" value="<?php echo $senderName; ?>" />
<br />
<br />
Your Email:
<br />
<input name="email" type="text" id="email" size="36" maxlength="32" value="<?php echo $senderEmail; ?>" />
<br />
<br />
Your Message:
<br />
<textarea name="msg" id="msg" cols="45" rows="5"><?php echo $senderMessage; ?> </textarea>
<br />
<br />
<br />
<input type="submit" name="button" id="button" value="Send Now" />
</form>

Related

php contact form sending mail to server admin [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I have little issue about my contact form. it doesn't send mail the server admin.
<?php
session_start();
// get the data from the form
$admin_email = 'server#admin.com';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$subject = 'Contact Form';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['www.mywebsite.com'];
// check if the fields are empty
if(empty($email) or empty($name) or empty($email) or empty($message)){
$output = "All fields are required!";
}else{
if(md5($captcha) == $img_session){
$header = "From: $email"."\r\n"
.'Content-Type: text/plain; charset=utf-8'."\r\n";
$message = "
New entry from $subject!
Name: $name
E-Mail: $email
Message:
$message
This message was sent from http://$website";
if(mail($admin_email, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
$output = "Your message was sent!<br />Thank you!";
}
}else{
$output = "Wrong captcha code!";
}
}
echo $output;
?>
UPDATED
the wrong part is if $admin_email is not match the contact form email address wrong captcha error pops up? I don't know why. I mean if I don't write contact form email line, same address with admin_email I can't send mail??? What I am trying to do here: send mail to the guest (your mail sent), and also $admin_email: server#admin.com (You have email) $messege.
Form
<form action="" id="contact_form" method="POST">
<p>Name:</p>
<input type="text" name="name" placeholder="Enter name" required=""/>
<p>Email:</p>
<input type="email" name="email" placeholder="Enter email" required=""/>
<p>Message:</p>
<textarea name="message" rows="10" cols="30" required=""></textarea>
<p>Captcha:</p>
<img src="captcha.php" id="captcha"/>
<input type="text" name="captcha" placeholder="Enter code" required=""/>
<input type="submit" value="Submit" />
</form>
If you want to send both email to user and admin, try to call the mail function twice, for example
if(mail($user_email_address, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
$output = "Your message was sent!<br />Thank you!";
}
if(mail($admin_email_address, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
$output = "You've got a new message from #user_email_address <br /> ";
}

Unable to replace form with message on submission

I've created an HTML5 form, which incorporates reCAPTCHA, and I've also written a PHP script that sends an email when the form is submitted. At the moment, the script redirects the user to an error or thankyou page, but I'm trying to adjust it to dynamically replace the form within a message within the same page.
I've tried the following script, but it displays the message as soon as the page loads, before any user interaction.
PHP/HTML:
<?php
if ($_POST) {
// Load reCAPTCHA library
include_once ("autoload.php");
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$emailFrom = $email;
$emailTo = "my#email.com";
$subject = "Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
$secret = 'XXX';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
echo 'Your message was submitted!';
} else {
?>
<div class="contact-form">
<form role="form" method="post" action="index.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<label><span> </span><div id="recaptcha"><div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div></div></label>
<label><span> </span><input type="submit" value="" class="submit-button" /></label>
</form>
</div>
<?php
}
?>
I'm new to PHP, so I'm not sure if it's a syntax or semantics issue. Any help would be greatly appreciated!
Here's one way of doing it.
Check to see if the form has been submitted with if(isset($_POST['submit'])). You can also use if($_SERVER['REQUEST_METHOD'] == 'POST') to see if the form has been submitted.
Then we check if the email has been successfully sent, and if it has we set the $success_message variable.
We then check to see if the $success_message variable is set, and if it isn't, we show the form.
Also, note that I added name="submit" to the submit button element. This is how we're checking to see if the form has been submitted.
I also changed stripslashes() to strip_tags() to prevent any malicious code from getting through.
<?php
// Load reCAPTCHA library
include_once ("autoload.php");
if(isset($_POST['submit'])) {
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = trim(strip_tags($_POST['message']));
$emailFrom = $email;
$emailTo = "my#email.com";
$subject = "Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
$secret = 'XXX';
$lang = 'en';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
// EDIT: repositioned recaptcha from OP's PasteBin script, as requested and adjusted messaging
// changed $success var to $message and added error message
// Original if statement, which redirected the user
if($resp->isSuccess()){
// send the email
if(mail($emailFrom, $subject, $body, $headers)) {
// set the success message
$success_message = 'The form was sent! Yay!';
} else {
// error message
$error_message = 'Could not send email';
}
} else {
$error_message = 'Prove you are a human!';
}
}
?>
<div>
<!-- quick and dirty way to print messages -->
<?php if(isset($success_message)) { echo $success_message; } ?>
<?php if(isset($error_message)) { echo $error_message; } ?>
</div>
<?php if(!isset($success_message)): ?>
<div class="contact-form">
<form role="form" method="post" action="index.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div>
<script type="text/javascript"
src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>">
</script>
<label><span> </span><input type="submit" name="submit" value="" class="submit-button" /></label>
</form>
</div>
<?php endif; ?>

PHP is loading, but not sending the email to me

I will admit that I am not the best at PHP, but what I am doing usually works well enough for me because the clients just need the information sent to their emails, and the security question works well enough to keep spam away from their inbox. But for some reason, this code will not work and I cannot figure out why. This same code with different variables is on another site I have and is tested and working. Does anyone have any advice?
<form action="submit_form.php" method="POST" >
<p>Name<br /><input type="text" name="name" required /></p>
<p>Email Address<br /><input type="text" name="email" required /></p>
<p>Phone Number<br /><input type="text" name="phone" required /></p>
<p>County<br /><input type="text" name="county" required /></p>
<p>Annual Income<br /><input type="text" name="income" required /></p>
<p>What is 4 + 1? (anti-spam)<br /><input type="text" name="answer" required /></p>
<input type="submit" value="Take The First Step" />
</form>
<?php # BOOST
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$county = $_POST['county'];
$income = $_POST['income'];
$correct_answer = '5';
$answer = $_POST['answer'];
if ($correct_answer != $answer) {
die("You're not a valid user of this site!");
}
else {
$to = "glenn#boostbizseo.com";
$subject = "USDA LOANS";
$message = "USDA LOANS:\n
Name: $name
Email: $email
Phone: $phone
County: $county
Annual Income: $income";
$from = "USDA Loans";
mail($to,$subject,$message,$headers);
echo "Thank you for getting in contact with us. We will be in contact with you soon regarding your USDA Loan! <a href=http://www.usdaloansmo.com>Click Here</a> to go back to our website!";
}
?>
$from = "someonelse#example.com"; //email ID
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
I believe this will work.
This is wrong:
$from = "USDA Loans";
mail($to,$subject,$message,$headers);
You don't use $from and $headers is undefined.
Here is how you send email using php.
$to="some#domain.com";
$subject="Your subject";
$header="from: ABCName <me#mydomain.com>";
$message="Message \r\n";
$sentmail = mail($to,$subject,$message,$header);
if($sentmail)
{
echo "Done";
}
else
{
echo "Oops, Something went wrong";
}

My PHP form submits but does not validate the email address

I am an eager novice with PHP so please forgive my errors as I am learning as I go. Basically, I am building a simple contact form for my website and have successfully been able to have the form send the user's first and last name, subject, email address and message. I am using a second file, "form_process.php" to process the form data from "index.php".
The problem is that the email address does not seem to be validating and will send any words typed. I would greatly appreciate it if some more seasoned eyes could take a look and help me sort this out. Thank you in advance.
Michael.
HTML:
<div id="form">
<form action="form_process.php" method="post" enctype="multipart/form-data">
<p>
<input type="text" maxlength="100" size="50" name="fName" value="<?php echo $stored_fName;?>" placeholder="First Name" />
</p>
<p>
<input type="text" maxlength="100" size="50" name="lName" value="<?php echo $stored_lName;?>" placeholder="Last Name" />
</p>
<p>
<input type="text" maxlength="80" size="50" name="email" value="<?php echo $stored_email;?>" placeholder="Email Address" />
</p>
<p>
<input type="text" maxlength="100" size="50" name="subject" value="<?php echo $stored_subject;?>" placeholder="Subject" />
</p>
<p>
<textarea name="message" rows="6" cols="38" placeholder="Message"></textarea>
</p>
<br />
<input type="submit" value="Submit" name="submit" />
<input type="reset" value="Clear" name="clear">
</form>
</div>
<!-- form ends -->
PHP: "form_process.php"
<?php
session_start();
// Report all PHP errors
error_reporting(E_ALL);
//use $_POST to to store data from submitted form into these variables
$fName = check_input($_POST['fName']);
$lName = check_input($_POST['lName']);
$sender = check_input($_POST['email']);
$subject = check_input($_POST['subject']);
$message = check_input($_POST['message']);
//check_input function to strip unnessessary characters and sanitize user data
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$name = $fName ." ". $lName;//concatenating first and last names to new name variable
$sanitizedEmail = filter_var($sender, FILTER_SANITIZE_EMAIL);
//generates error messages on index.php if form fields left blank
if ($fName == ''){
header("Location:index.php?message=1");
exit();
}
if ($lName == ''){
header("Location:index.php?message=2");
exit();
}
if ($sender == ''){
header("Location:index.php?message=3");
exit();
}
if ($subject == ''){
header("Location:index.php?message=4");
exit();
}
if ($message == ''){
header("Location:index.php?message=5");
exit();
}
//headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= $name . "\r\n";
$headers .= "From:" . " " . $sanitizedEmail . "\r\n";
//mail function
$to = "me#myemail.com";
$subject = $subject;
$message = $message;
//send message
$send_message = mail($to,$subject,$message,$headers);
if($send_message){
header("Location:index.php?message=6");
}else {
header("Location:index.php?message=9");
exit();
}
?>
"index.php" error messages:
<?php
//all fields empty until user inputs data for session to store
$stored_fName = '';//init as NULL
$stored_lName = '';//init as NULL
$stored_email = '';//init as NULL
$stored_subject = '';//init as NULL
$stored_message = '';//init as NULL
//session data used to repopulate form fields if any info is missing or incorrect
if (isset($_SESSION['fName'])){
$stored_fName = $_SESSION['fName'];
}
if (isset($_SESSION['lName'])){
$stored_lName = $_SESSION['lName'];
}
if (isset($_SESSION['email'])){
$stored_email = $_SESSION['email'];
}
if (isset($_SESSION['subject'])){
$stored_subject = $_SESSION['subject'];
}
if (isset($_SESSION['message'])){
$stored_message = $_SESSION['message'];
}
//error messages displayed to user if text fields have been left blank
$_GET['message'];
if ($_GET['message'] == 1) {//first name
echo "<strong>Please type your first name.</strong>";
}
if ($_GET['message'] == 2) {//last name
echo "<strong>Please type your last name.</strong>";
}
if ($_GET['message'] == 3){//email address
echo "<strong>Please type an email address.</strong>";
}
if ($_GET['message'] == 4){//subject
echo "<strong>Please type a subject.</strong>";
}
if ($_GET['message'] == 5){//message text
echo "<strong>Please type your message.</strong>";
}
if ($_GET['message'] == 6){//message success from form_process.php
echo "<strong>Your message was sent successfully. Thank you.</strong>";
}
if ($_GET['message'] == 9){
echo "<strong>I'm sorry but your message was not sent. Please try again, thank you.</strong>";
}
?>
You should be using it like this:
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
// is email
$sender = $email;
}else{
// isn't email
$sender = '';
}
Read more about PHP Validate Filters

PHP contact form validation

I was just wondering if anybody could please help if this PHP code would work for validating HTML contact form inputs. I followed a tutorial to create this PHP validation, but i'm not sure if it will work. I don't have a webhost yet to test it out, but if anybody has a server,I will really be appreciated if anybody could do me a favour & try the code if you can send/receive email. Thank you!!
I'm using jQuery Validation Plugin for validating the form on client-side and this is the tutorial http://www.youtube.com/watch?v=rdsz9Ie6h7I
HTML form:
<form action="contact.php" method="post">
<label for="yourname">Your Name:</label>
<input type="text" name="YourName"/>
<label for="youremail">Your Email:</label>
<input type="text" name="YourEmail" />
<label for="yourmessage">Your Message:</label>
<textarea name="YourMessage"></textarea>
<fieldset>
<input type="submit" id="submit" value="Send"/>
<input type="reset" id="reset" value="Reset"/>
</fieldset>
PHP Code:
<?php
/* Subject and Email Variables */
$emailSubject = 'Email from site visitor';
$webMaster = 'YourEmail#mail.com';
/* Getting Form Data Variables */
$nameField = $_POST['YourName'];
$emailField = $_POST['YourEmail'];
$messageField = $_POST['YourMessage'];
$body = <<<EOD
<br><hr><br>
Name: $YourName <br>
Email: $YourEmail <br>
Message: $YourMessage <br>
EOD;
$headers = "From: $YourEmail\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<html>
<head>
</head>
<body>
<p style="font-size:12px;font-family:Tahoma,Verdana;">Thanks for your Message.</p>
</body>
</html>
EOD;
echo "$theResults";
?>
If you don't have a webserver right now you can install a local one and test it there. Completly free.
http://www.apachefriends.org/en/xampp-windows.html#641
/* Getting Form Data Variables */
$nameField = $_POST['YourName'];
$emailField = $_POST['YourEmail'];
$messageField = $_POST['YourMessage'];
$body = <<<EOD
<br><hr><br>
Name: $nameField<br>
Email: $emailField <br>
Message: $messageField <br>
Lots of issues
You're not validating at all on the PHP side. I added validation and conditional rendering of success or failure messages below
You're not using the retrieved variable names (instead using $YourEmail) - the post names
Your form labels don't match the input names
Your form element isn't closed, and your fields aren't all in the fieldset, and the fieldset has no legend.
On case of failure, I added a value prefill in the form, so people don't get returned to a blank form when it falls to submit.
To do that, I put the form at the end, after the PHP
Code:
(Note: This would all be put into the file contact.php)
<?php
if (isset($_POST['submit'])) {
/* Getting Form Data Variables */
$nameField = isset($_POST['YourName']) ? $_POST['YourName'] : null;
$emailField = isset($_POST['YourEmail']) ? $_POST['YourEmail'] : null;
$messageField = isset($_POST['YourMessage']) ? $_POST['YourMessage'] : null;
// Validate
$failures = array();
if (strlen($nameField)) $failures[] = "Name is required";
if (strlen($emailField)) $failures[] = "Email is required";
if (filter_var($email,FILTER_VALIDATE_EMAIL) === false) $failures[] = "Email is invalid";
if (strlen($messageField)) $failures[] = "Message is required";
// If validation errors, render them
if (count($failures)) {
echo "<p><b>Failed to submit: " . implode(", ", $failures) . "</b></p>";
} else {
/* Subject and Email Variables */
$emailSubject = 'Email from site visitor';
$webMaster = 'YourEmail#mail.com';
$body = <<<EOD
<br><hr><br>
Name: {$nameField} <br>
Email: {$emailField} <br>
Message: {$messageField} <br>
EOD;
$headers = "From: {$emailField}\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<p>Thanks for your Message.</p>
EOD;
echo "$theResults";
}
}
?>
<form action="contact.php" method="post">
<fieldset>
<legend>Contact Us</legend>
<label for="YourName">Your Name:</label>
<input type="text" name="YourName" value="<?=$nameField?>" />
<label for="YourEmail">Your Email:</label>
<input type="text" name="YourEmail" value="<?=$emailField?>" />
<label for="YourMessage">Your Message:</label>
<textarea name="YourMessage"><?=$messageField?></textarea>
<input type="submit" id="submit" value="Send"/>
<input type="reset" id="reset" value="Reset"/>
</fieldset>
</form>

Categories