form to mail script not sending data - php

I have a simple form with 3 fields, contact_name contact_email & contact_message when i submit this, the data entered into the form is not emailed to me. however if i check what is being posted i can see an array is posted:
Array
(
[contact_name] => paul
[contact_email] => test#test.com
[contact_message] => D;LKF'DSKF;LSDKF
)
Here is my form code:
<form role="form" name="form1" action="send_form_email.php" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" name="contact_name" placeholder="Name" id="contact_name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" name="contact_email" placeholder="Email Address" id="contact_email">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea name="contact_message" placeholder="Message" class="form-control" rows="5" id="contact_message"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-success">Send</button>
</div>
</div>
</form>
And here is the php script:
<?php
// Contact subject
$subject ='Message from Leeds Computers';
// Details
$message="$contact_message";
// Mail of sender
$mail_from="$contact_email";
// From
$header="from: $contact_name <$mail_from>";
// Enter your email address
$to ='test#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>
<? echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
Any help would be much appreciated

You probably have to modify it to look at POST:
// Details
$message=$_POST["contact_message"];
// Mail of sender
$mail_from=$_POST["contact_email"];
// From
$header="from: " . $_POST["contact_name"] . " <$mail_from>";

Related

PHP - html form sent to gmail account

I never used PHP before, but I need to set up an HTML form from to be sent from my website to my gmail account. Checked some tutorials and came up with this code. But for some reason it is not working. When I hit submit it goes to a "Page Not Found
Looks like you've followed a broken link or entered a URL that doesn't exist on this site." Not sure what am I doing wrong.
I have my HTML document, and I have my PHP document. Saved together.
<form action="contact.php" method="POST" name="form">
<div class="row">
<div class="col-12 col-md-6">
<div class="contact-form">
<label for="name">Please enter your name:</label>
<input name="name" type="text" class="form-control" placeholder="Harry Potter">
</div>
</div>
<div class="col-12 col-md-6">
<div class="contact-form">
<label for="mail">Your email address:</label>
<input name="mail" type="email" class="form-control" placeholder="harry#potter.com">
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="contact-form">
<label for="message">How can I help you?</label>
<textarea name="message" class="form-control" id="form-text" cols="30" rows="10" placeholder="Let's do some magic"></textarea>
</div>
</div>
</div>
<div id="submit" class="text-center">
<button name="submit" type="submit" class="btn mt-5 mx-auto">Submit</button>
</div>
</form>
<?php
if(isset($_POST["submit"])) {
$name=$_POST["name"];
$mailFrom=$_POST["mail"];
$message=$_POST["message"];
$mailTo = "myaddress#gmail.com";
$subject = "Form from my website";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name."\n\n".$message;
if(mail($mailTo, $subject, $txt, $headers)) {
echo "<h1>I recieved your email! Will be in touch with you soon.</h1>";
}
else{
echo "<h1>Something went wrong! Try again.</h1>";
}
header("Location: https://www.whatever.com");
}
?>

405 Not Allowed error when submitting html form to php file [duplicate]

Having an issue with getting data from a form that I created and passing it into my email through php. Here's the link with the form http://kapena.github.io/pp_web/#contact-pp
Here's the exact issue..
When I click submit on the form button I get this error message saying..
I'm currently hosting this build on a gh-pages
I did some research on this and I found a patch for the error 405 but I have no idea how to implement this solution into my project. I am just a beginner and I need some instruction..Here's the solution I found https://gist.github.com/baskaran-md/e46cc25ccfac83f153bb#file-nginx-conf
How can I implement this solution correctly into my site?
Here's my php code so you can inspect that to see if there's and issue there thats causing this problem.
Any help would be greatly appreciated here..Trying to launch b4 the new year :)
<?php
if($_POST["submit"]) {
$recipient = "brentw.white#gmail.com"; //my email
$subject = 'Email message from Point Plumbing';
$name = $_POST ["yourName"];
$email = $_POST["yourEmail"];
$phone = $_POST["yourPhone"];
$location = $_POST["yourLocate"];
$message = $_POST["message-me"];
$mailBody="Name: $name\nEmail: $email\n\n$message";
$mail($recipient, $subject, $mailBody, "From: $name <$email>");
$thankYou="<p>Thank you! We will be in contact with you shortly.</p>";
}
?>
My HTML..I have a feeling it has something to do with method='post' possible that's the issue?
<form action="submit_form.php" method="post" id="spaceing" data-abide>
<div class="form-bg">
<div class="row">
<div id="name" class="large-6 medium-6 small-12 columns">
<label> <h5>Name</h5>
<input type="text" name="yourName" placeholder="What is you're name?"></input>
</label>
</div>
<div id="email" class="large-6 medium-6 small-12 columns">
<label><h5>Email</h5>
<input type="email" name="yourEmail" placeholder="Please enter in you're email."required></input>
</label>
<small class="error">An email Address is required.</small>
</div>
</div>
<div class="row">
<div id="phone"class="large-6 medium-6 small-12 columns">
<label><h5>Phone</h5>
<input type="text" name="yourPhone" placeholder="Please enter you're phone number."></input>
</label>
</div>
<div id="location" class="large-6 medium-6 small-12 columns">
<label><h5>Location</h5>
<input type="text" name="yourLocate" placeholder="Where on Oahu are you currently living?"></input>
</label>
</div>
</div>
<div class="row">
<div id="message-area" class="large-12 medium-12 small-12 columns">
<label><h5>Message</h5>
<textarea id="message" name="yourMessage" placeholder="Please tell us about you're plumbing issues."></textarea>
</label>
</div>
</div>
<div class="row">
<div id="submit" class="large-3 medium-3 small-3 columns">
<button type="submit-me">Submit</button>
</div>
</div>
</div>
</form>
look http://php.net/manual/en/function.mail.php
$mail($recipient, $subject, $mailBody, "From: $name <$email>");
$mail is not variable is function then
mail($recipient, $subject, $mailBody, "From: $name <$email>");
I modified your code and find lots of error.correct code is below:-
found following error on your code
1. use if(isset($_POST["submit"])) { instead of if($_POST["submit"]) {
2. you use wrong variable name for textarea. you should use $message = $_POST["yourMessage"]; instead of $message = $_POST["message-me"];
4. another error is your form action is at submit_form.php page but i found your mail code is at top of your page. if you want to submit to another page then use php code to submit_form.php page.
5. Main and important error of your code i found you using your gmail email at the place of sending mail.but you should use your domain email like brentw.white#kapena.github.io if you using another domain email these mail are count as suspicious and your mail goes to spam
I think it will be help full for you
I used your link here:-
http://smartzonewebservices.com/mtg/del.php
<?php
if(isset($_POST["submit"])) {
$recipient = "brentw.white#gmail.com"; //my email
echo $subject = 'Email message from Point Plumbing';
echo $name = $_POST ["yourName"];
echo $email = $_POST["yourEmail"];
echo $phone = $_POST["yourPhone"];
echo $location = $_POST["yourLocate"];
echo $message = $_POST["yourMessage"];
$mailBody="Name: $name\nEmail: $email\n\n$message";
mail($recipient, $subject, $mailBody, "From: $name <$email>");
echo $thankYou="<p>Thank you! We will be in contact with you shortly.</p>";
}
?>
<form action="" method="post" id="spaceing" data-abide>
<div class="form-bg">
<div class="row">
<div id="name" class="large-6 medium-6 small-12 columns">
<label> <h5>Name</h5>
<input type="text" name="yourName" placeholder="What is you're name?"></input>
</label>
</div>
<div id="email" class="large-6 medium-6 small-12 columns">
<label><h5>Email</h5>
<input type="email" name="yourEmail" placeholder="Please enter in you're email."required></input>
</label>
<small class="error">An email Address is required.</small>
</div>
</div>
<div class="row">
<div id="phone"class="large-6 medium-6 small-12 columns">
<label><h5>Phone</h5>
<input type="text" name="yourPhone" placeholder="Please enter you're phone number."></input>
</label>
</div>
<div id="location" class="large-6 medium-6 small-12 columns">
<label><h5>Location</h5>
<input type="text" name="yourLocate" placeholder="Where on Oahu are you currently living?"></input>
</label>
</div>
</div>
<div class="row">
<div id="message-area" class="large-12 medium-12 small-12 columns">
<label><h5>Message</h5>
<textarea id="message" name="yourMessage" placeholder="Please tell us about you're plumbing issues."></textarea>
</label>
</div>
</div>
<div class="row">
<div id="submit" class="large-3 medium-3 small-3 columns">
<button name="submit" type="submit">Submit</button>
</div>
</div>
</div>
</form>
I thing this going because you develop on github server and he does not allow make POST
change your code HTML:
<form action="submit_form.php" method="get" id="spaceing" data-abide>
and PHP change $_POST for $_GET but there is problem with length because all data is sending in URL.
If you want public page to normal server return back to POST method

PHP POST / Bootstrap Form

Im not receiving emails from my email form.
I created a simple form that takes 4 fields and sends them to our emails. I tested my php code with no errors. I double checked the html to see if the script was referring properly which it was. I believe that the ids and names are correct. I also triple checked the variables I created on the php code. I've googled everything I could come up with no luck. Thanks!
var dump $body shows
Thank you! One of our station representatives will contact you soon.
string(119) "
Name: TEST
Email: test#emaol.com
Station: 1234
Message:
I need help with.
FORM
<form class="form-horizontal" role="form" method="post" action="feedback.php">
<fieldset>
<!-- Form Name -->
<legend>Our team will reach out to you as soon as possible.</legend>
<!-- Text input-->
<div class="control-group col-xs-4">
<label class="control-label" for="textinput">Full Name</label>
<div class="controls">
<input id="name" name="name" placeholder="John Doe" class="input-xlarge" type="text">
</div>
</div>
<!-- Text input-->
<div class="control-group col-xs-4">
<label class="control-label" for="textinput">E-Mail</label>
<div class="controls">
<input id="email" name="email" placeholder="affiliate.marketing.app#123.com" class="input-xlarge" type="text">
</div>
</div>
<!-- Text input-->
<div class="control-group col-xs-4">
<label class="control-label" for="textinput">Station Code</label>
<div class="controls">
<input id="station" name="station" placeholder="xxx" class="input-xlarge" type="text">
</div>
</div>
<!-- Textarea -->
<div class="control-group">
<label class="control-label" for="textarea">Message</label>
<div class="controls">
<textarea id="message" name="message">I need help with....</textarea>
</div>
</div>
<!-- Button -->
<div class="control-group">
<label class="control-label" for="submit"></label>
<div class="controls">
<button type="submit" id="submit" value="Send" name="singlebutton" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
PHP PAGE
<?php
/* These are the variable that tell the subject of the email and where the email will be sent.*/
$emailSubject = 'Station App Support Request';
$mailto = 'myemail#domain.com, myemail2#domain.com';
/* These will gather what the user has typed into the fieled. */
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$stationField = $_POST['station'];
$messageField = $_POST['message'];
/* This takes the information and lines it up the way you want it to be sent in the email. */
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Station: $stationField <br>
Message: $messageField <br>
EOD;
$headers = "From: $emailField\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
?>

Using php to send form to my email..Having an issue with 405 Not Allowed when form is submitted

Having an issue with getting data from a form that I created and passing it into my email through php. Here's the link with the form http://kapena.github.io/pp_web/#contact-pp
Here's the exact issue..
When I click submit on the form button I get this error message saying..
I'm currently hosting this build on a gh-pages
I did some research on this and I found a patch for the error 405 but I have no idea how to implement this solution into my project. I am just a beginner and I need some instruction..Here's the solution I found https://gist.github.com/baskaran-md/e46cc25ccfac83f153bb#file-nginx-conf
How can I implement this solution correctly into my site?
Here's my php code so you can inspect that to see if there's and issue there thats causing this problem.
Any help would be greatly appreciated here..Trying to launch b4 the new year :)
<?php
if($_POST["submit"]) {
$recipient = "brentw.white#gmail.com"; //my email
$subject = 'Email message from Point Plumbing';
$name = $_POST ["yourName"];
$email = $_POST["yourEmail"];
$phone = $_POST["yourPhone"];
$location = $_POST["yourLocate"];
$message = $_POST["message-me"];
$mailBody="Name: $name\nEmail: $email\n\n$message";
$mail($recipient, $subject, $mailBody, "From: $name <$email>");
$thankYou="<p>Thank you! We will be in contact with you shortly.</p>";
}
?>
My HTML..I have a feeling it has something to do with method='post' possible that's the issue?
<form action="submit_form.php" method="post" id="spaceing" data-abide>
<div class="form-bg">
<div class="row">
<div id="name" class="large-6 medium-6 small-12 columns">
<label> <h5>Name</h5>
<input type="text" name="yourName" placeholder="What is you're name?"></input>
</label>
</div>
<div id="email" class="large-6 medium-6 small-12 columns">
<label><h5>Email</h5>
<input type="email" name="yourEmail" placeholder="Please enter in you're email."required></input>
</label>
<small class="error">An email Address is required.</small>
</div>
</div>
<div class="row">
<div id="phone"class="large-6 medium-6 small-12 columns">
<label><h5>Phone</h5>
<input type="text" name="yourPhone" placeholder="Please enter you're phone number."></input>
</label>
</div>
<div id="location" class="large-6 medium-6 small-12 columns">
<label><h5>Location</h5>
<input type="text" name="yourLocate" placeholder="Where on Oahu are you currently living?"></input>
</label>
</div>
</div>
<div class="row">
<div id="message-area" class="large-12 medium-12 small-12 columns">
<label><h5>Message</h5>
<textarea id="message" name="yourMessage" placeholder="Please tell us about you're plumbing issues."></textarea>
</label>
</div>
</div>
<div class="row">
<div id="submit" class="large-3 medium-3 small-3 columns">
<button type="submit-me">Submit</button>
</div>
</div>
</div>
</form>
look http://php.net/manual/en/function.mail.php
$mail($recipient, $subject, $mailBody, "From: $name <$email>");
$mail is not variable is function then
mail($recipient, $subject, $mailBody, "From: $name <$email>");
I modified your code and find lots of error.correct code is below:-
found following error on your code
1. use if(isset($_POST["submit"])) { instead of if($_POST["submit"]) {
2. you use wrong variable name for textarea. you should use $message = $_POST["yourMessage"]; instead of $message = $_POST["message-me"];
4. another error is your form action is at submit_form.php page but i found your mail code is at top of your page. if you want to submit to another page then use php code to submit_form.php page.
5. Main and important error of your code i found you using your gmail email at the place of sending mail.but you should use your domain email like brentw.white#kapena.github.io if you using another domain email these mail are count as suspicious and your mail goes to spam
I think it will be help full for you
I used your link here:-
http://smartzonewebservices.com/mtg/del.php
<?php
if(isset($_POST["submit"])) {
$recipient = "brentw.white#gmail.com"; //my email
echo $subject = 'Email message from Point Plumbing';
echo $name = $_POST ["yourName"];
echo $email = $_POST["yourEmail"];
echo $phone = $_POST["yourPhone"];
echo $location = $_POST["yourLocate"];
echo $message = $_POST["yourMessage"];
$mailBody="Name: $name\nEmail: $email\n\n$message";
mail($recipient, $subject, $mailBody, "From: $name <$email>");
echo $thankYou="<p>Thank you! We will be in contact with you shortly.</p>";
}
?>
<form action="" method="post" id="spaceing" data-abide>
<div class="form-bg">
<div class="row">
<div id="name" class="large-6 medium-6 small-12 columns">
<label> <h5>Name</h5>
<input type="text" name="yourName" placeholder="What is you're name?"></input>
</label>
</div>
<div id="email" class="large-6 medium-6 small-12 columns">
<label><h5>Email</h5>
<input type="email" name="yourEmail" placeholder="Please enter in you're email."required></input>
</label>
<small class="error">An email Address is required.</small>
</div>
</div>
<div class="row">
<div id="phone"class="large-6 medium-6 small-12 columns">
<label><h5>Phone</h5>
<input type="text" name="yourPhone" placeholder="Please enter you're phone number."></input>
</label>
</div>
<div id="location" class="large-6 medium-6 small-12 columns">
<label><h5>Location</h5>
<input type="text" name="yourLocate" placeholder="Where on Oahu are you currently living?"></input>
</label>
</div>
</div>
<div class="row">
<div id="message-area" class="large-12 medium-12 small-12 columns">
<label><h5>Message</h5>
<textarea id="message" name="yourMessage" placeholder="Please tell us about you're plumbing issues."></textarea>
</label>
</div>
</div>
<div class="row">
<div id="submit" class="large-3 medium-3 small-3 columns">
<button name="submit" type="submit">Submit</button>
</div>
</div>
</div>
</form>
I thing this going because you develop on github server and he does not allow make POST
change your code HTML:
<form action="submit_form.php" method="get" id="spaceing" data-abide>
and PHP change $_POST for $_GET but there is problem with length because all data is sending in URL.
If you want public page to normal server return back to POST method

Blank PHP Emails

There's a lot of blank php email posts on here but none of them have solved this for me.
I tweaked this simple php code I found to simply email a specified email address (in this case, my client's email) with a feedback message from a customer on their website. Through testing, I could only get it to send emails when I didn't include the initial if statement as validation, but even then, the emails would have no subject or body.
contact.html
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
<div class="form-group">
<label for="inputName" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="inputName" placeholder="Name"><br />
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" name="inputEmail" placeholder="Email"><br />
</div>
</div>
<div class="form-group">
<label for="inputMessage" class="col-sm-3 control-label">Message</label>
<div class="col-sm-9">
<textarea type="text" class="form-control" name="inputMessage" placeholder="Message"></textarea><br />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-default" type="submit" value="Submit">
</div>
</div>
</form>
send_form_email.php
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Contact subject
$name =$_POST['inputName'];
// Details
$message=$_POST['inputMessage'];
// Mail of sender
$mail_from=$_POST['inputEmail'];
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='test#gmail.com';
$send_contact=mail($to,$name,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
header("Location: http://wetzelscontracting.com/postcontact.html");
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}}
?>
Ok guys, long story, but Mailto isn't actually in the action attr, I removed it from the post.
Actually, I don't know what kind of frankenstein code I originally posted, but that was full of errors that are no longer there. Hopefully I posted the right code this time.
Why is your form action MAILTO:?
<form name="feedback" class="form-horizontal" role="form" action="MAILTO:send_form_email.php" method="post">
It should just be a clean call to the PHP page like this:
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
The only time you would use MAILTO: is when constructing an <a href="mailto:someguy#someplace.somedomain">. For an HTML form using PHP like this the goal is to submit the form, and the the $_POST data gets parsed by the PHP which then acts on it to send an e-mail.
Additionally, you are not setting name values in any of the input fields & the names you have for id values dont even match what the PHP is attempting to do. So try this for the HTML:
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
<div class="form-group">
<label for="inputName" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputName" placeholder="Name" name="inputName"><br />
</div></div>
<div class="form-group">
<label for="inputEmail" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input name="email" type="email" class="form-control" id="inputEmail" placeholder="Email" name="inputEmail"><br />
</div></div>
<div class="form-group">
<label for="inputMessage" class="col-sm-3 control-label">Message</label>
<div class="col-sm-9">
<textarea type="text" class="form-control" id="inputMessage" placeholder="Message" name="inputMessage"></textarea><br />
</div></div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-default" type="submit" value="Submit">
</div></div>
</form>
Also here is the reworked PHP code.
The first thing I did was take all of your $_POST checks into a structure that uses one main array ($post_array) and then rolls through that array to process the values & assign them to similarly named variables. You had absolutely no input validation before. This is technically not even really great “validation” since isset() just checks to see if the $_POST value even exists. But this is step up.
Also I reworked your error checking logic at the end since it all happened after headers were sent. Meaning none of that the whole "We've recived your information" would never work. This is the best I can do with the info you’re providing, but I am doing this to convey the basic concepts:
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST"){
// Set the post values array.
$post_array = array('inputName','inputEmail','inputMessage');
// Roll through the post values array.
foreach($post_array as $post_key => $post_value) {
$$post_key = isset($_POST[$post_key] ? $_POST[$post_key] : null;
}
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='test#gmail.com';
$send_contact=mail($to,$name,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header("Location: http://wetzelscontracting.com/postcontact.html");
}
else {
echo "ERROR";
}
}
?>
As none of the other answers have covered the issue of validation apart from the one accepted, but if your going to do that you might as well just use the extract() function, (it also won’t protect from header injection or email validation).
It’s very important to validate user input and a layer of simple CSRF protection, else bots or spammers can directly POST to your PHP and it will send you a bombardment of emails, you won’t see the forest for the trees (legit emails), or worse inject headers into your inputEmail field and send their own emails using your server which is obviously something you don't want to happen.
Also I’ve added an easy way that you can pass errors from your PHP script that sends the user back to the form for you to echo out.
So for the send_form_email.php file.
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_SESSION['csrf'])){
//set error array to fill
$errors = array();
// Validate Contact subject
if(!empty($_POST['inputName'])){
$name = $_POST['inputName'];
}else{
$error['inputName'] = 'Required!';
}
// Validate Details
if(!empty($_POST['inputMessage'])){
$message = $_POST['inputMessage'];
}else{
$error['inputMessage'] = 'Required!';
}
// Validate Mail of sender
if(!empty($_POST['inputEmail'])){
if(filter_var($_POST['inputEmail'], FILTER_VALIDATE_EMAIL)){
$mail_from = $_POST['inputEmail'];
}else{
$error['inputEmail'] = 'Invalid Email!';
}
}else{
$error['inputEmail'] = 'Required!';
}
if(!isset($_POST['csrf']) || $_SESSION['csrf'] != $_POST['csrf']){
$_SESSION['email_status'] = 'Invalid csrf token!';
$error = true;
}
//stop multiple attempts - just remove csrf token
unset($_SESSION['csrf']);
//no errors send mail
if(empty($error)){
$headers ='MIME-Version: 1.0'."\r\n";
$headers.='Content-type: text/html; charset=utf8'."\r\n";
$headers.='From:<'.$mail_from.'>'."\r\n";
$headers.="X-Mailer: PHP"."\r\n";
if(mail('test#gmail.com', 'Website email form: '.$name, $message, $headers)){
$_SESSION['email_status'] = "We've received your contact information";
//send to success page
exit(header("Location: http://wetzelscontracting.com/postcontact.html"));
}else {
$_SESSION['email_status'] = 'There was an error sending the mail';
//backup to file
file_put_contents('mail.log.txt',print_r($_POST, true).PHP_EOL, FILE_APPEND);
}
}else{
//assuming its this url
exit(header("Location: http://wetzelscontracting.com/contact.php"));
$_SESSION['email_error'] = $error;
}
}else{
//stop multiple attempts
unset($_SESSION['csrf']);
//dont allow GET request/direct access
exit(header("Location: http://wetzelscontracting.com/contact.php"));
}
?>
Then in your page with the form, start a session to read from the $_SESSION array, and then echo out your errors if any.
<?php
session_start();
//make a session key that we will check against in send_form_email.php
$_SESSION['csrf'] = sha1(uniqid(true));
?>
<?php echo isset($_SESSION['email_status']) ? $_SESSION['email_status'] : null ?>
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
<input type="hidden" name="csrf" value="<?php echo $_SESSION['csrf'];?>"/>
<div class="form-group">
<label for="inputName" class="col-sm-3 control-label">Name <?php echo isset($_SESSION['email_error']['inputName']) ? $_SESSION['email_error']['inputName'] : null?></label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputName" placeholder="Name" name="inputName"><br />
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-3 control-label">Email <?php echo isset($_SESSION['email_error']['inputEmail']) ? $_SESSION['email_error']['inputEmail'] : null?></label>
<div class="col-sm-9">
<input type="email" class="form-control" id="inputEmail" placeholder="Email" name="inputEmail"><br />
</div>
</div>
<div class="form-group">
<label for="inputMessage" class="col-sm-3 control-label">Message <?php echo isset($_SESSION['email_error']['inputMessage']) ? $_SESSION['email_error']['inputMessage'] : null?></label>
<div class="col-sm-9">
<textarea type="text" class="form-control" id="inputMessage" placeholder="Message" name="inputMessage"></textarea><br />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-default" type="submit" value="Submit">
</div>
</div>
</form>
<?php
//unset the errors so there only shown once
unset($_SESSION['email_status']);
unset($_SESSION['email_error']); ?>

Categories