How to send form to self email? - php

This is my contanct.html
<form action="sendmail.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>
<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>
<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
<p> </p>
<p>Powered by PHP form</p>
</form>
This is my php
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: confirmation.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
I got a problem when i upload in domain
It's working but didn't not send to my email of Form
I don't know what is my problem?
I'm newbie in web.dev
sorry for my bad english thanks.

I found and figured out what the problem is.
The function show_error($myError) was unfinished.
Plus you also needed to add $myemail = "email#example.com"; replacing it with your E-mail address.
Also make sure this page exists on your server header('Location: confirmation.htm');
Tested, working.
<?php
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
$myemail = "email#example.com"; // Replace with your E-mail address.
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
$headers="From: $name<$email>\r\nReturn-path: $email\r\n";
mail($myemail, $subject, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: confirmation.htm');
// echo "ok"; // For testing purposes only
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<b>We apologize for the inconvenience, an error occurred.</b><br />
<?php echo $myError; ?>
<?php
exit();
}

Related

php form not validatin or sending mails

I a trying to fix a php form that doesn't work
I am new to php, please help
I have the form in the index.html file and have another validation.php to validate the form fields.
<!-- contact form --->
<div class="containerq">
<div class="form_container">
<h2 class="text-center">Contact us</h2>
<form action="index.php" method="post" class="rd-mailform">
<input type="hidden" value="contact" name="form-type">
<fieldset>
<div class="row">
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfInput">
<input type="text" name="name" placeholder="Name">
</label>
</div>
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfInput">
<input type="text" name="email" placeholder="Email">
</label>
</div>
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfInput">
<input type="text" name="phone" placeholder="Phone">
</label>
</div>
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfSelect">
<select class="opt">
<option>Choose subject</option>
<option>I have a problem with a game</option>
<option>I have a problem with a course</option>
<option>I have an Idea</option>
<option>Collaboration with schools</option>
<option>Collaboration with game developers</option>
<option>General query and other subjects</option>
</select>
<ul class="dropdown">
<li class="option">I have a problem with a game</li>
<li class="option">I have a problem with a course</li>
<li class="option">I have an Idea</li>
<li class="option">Collaboration with schools</li>
<li class="option">Collaboration with game developers</li>
<li class="option">General query and other subjects</li>
</ul><span class="mfPlaceHolder"></span>
</label>
</div>
<div class="col-md-prefix-1 col-md-12">
<label data-add-placeholder="" class="mfInput">
<textarea data-constraints="" name="message" placeholder="Message" class="mtext"></textarea>
</label>
</div>
<!--<input class="header_button" type="submit" name="submit" value="Send Request">-->
<div class="header_button">Send Request </div>
<span class="success"><?php echo $successMessage;?></span>
<div class="mfInfo mfProgress"><span class="cnt"></span><span class="loader"></span><span class="msg"></span></div>
</div>
</fieldset>
</form>
</div>
</div>
in the header (which is in included at the index.php start) I added:
<?php require_once "validation.php";?>
because if I put require_once validation.php at the start of the index the form wouldn't be accessed.
in the validation.php this is the part that sends the mail:
if( !($name=='') && !($email=='') && !($phone=='') &&!($message=='') )
{ // Checking valid email.
if (preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$header= $name."<". $email .">";
$headers = "mywebsite.com"; /* Let's prepare the message for the e-mail */
$msg = "Hello! $name Thank you...! For Contacting Us.
Name: $name
E-mail: $email
Phone: $phone
Message: $message
This is a Contact Confirmation mail. We Will contact You as soon as possible.";
$msg1 = " $name Contacted Us. Hereis some information about $name.
Name: $name
E-mail: $email
Phone: $phone
Message: $message "; /* Send the message using mail() function */
if(mail($email, $headers, $msg ) && mail("receiver_mail_id#mywebsite.com", $header, $msg1 ))
{
$successMessage = "Message sent successfully.......";
}
}
else
{
$emailError = "Invalid Email";
}
}
but id doesn't send anything
the page gets reloaded to index.php#
I don't get the invalid email or Message sent successfully messages either
this is the complete validation.php:
<?php // Initialize variables to null.
//echo "validating";
$name =""; // Sender Name
$email =""; // Sender's email ID
$purpose =""; // Subject of mail
$message =""; // Sender's Message
$phone ="";
$nameError ="";
$emailError ="";
$phoneError ="";
$messageError ="";
$successMessage =""; // On submittingform below function will execute.
if(isset($_POST['submit']))
{ // Checking null values in message.
echo "validating2";
if (empty($_POST["name"])){
$nameError = "Name is required";
}
else
{
$name = test_input($_POST["name"]); // check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameError = "Only letters and white space allowed";
}
} // Checking null values inthe message.
if (empty($_POST["email"]))
{
$emailError = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
} // Checking null values inmessage.
if (empty($_POST["phone"]))
{
$phoneError = "Phone is required";
}
else
{
$phone = test_input($_POST["phone"]);
} // Checking null values inmessage.
if (empty($_POST["message"]))
{
$messageError = "Message is required";
}
else
{
$message = test_input($_POST["message"]);
} // Checking null values inthe message.
if( !($name=='') && !($email=='') && !($phone=='') &&!($message=='') )
{ // Checking valid email.
if (preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$header= $name."<". $email .">";
$headers = "mywebsite.com"; /* Let's prepare the message for the e-mail */
$msg = "Hello! $name Thank you...! For Contacting Us.
Name: $name
E-mail: $email
Phone: $phone
Message: $message
This is a Contact Confirmation mail. We Will contact You as soon as possible.";
$msg1 = " $name Contacted Us. Here is some information about $name.
Name: $name
E-mail: $email
Phone: $phone
Message: $message "; /* Send the message using mail() function */
if(mail($email, $headers, $msg ) && mail("contact#mywebsite.com", $header, $msg1 ))
{
$successMessage = "Message sent successfully.......";
}
}
else
{
$emailError = "Invalid Email";
}
}
} // Function for filtering input values.function test_input($data)
// Function for filtering input values.
function test_input($data)
{
$data = trim($data);
$data =stripslashes($data);
$data =htmlspecialchars($data);
return $data;
}
?>
any ideas?

Change a field in PHP form from required to optional

I am a complete novice when it comes to PHP. I downloaded a form I found online to include in my Bootstrap site. It is a very simple form anyone can use to send me a message. I managed to set up Wamp to test out the PHP but when I leave the Phone field blank it gives me an error message telling me please go back and correct the error. I want to make it so if someone leaves out the phone number it still sends the email. Thank you.
HTML
<form name="contactform" method="post" action="index.php" class="form-vertical">
<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="First and Last">
</div>
<div class="form-group">
<label for="inputEmail" class="control-label">Email*</label>
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Required">
</div>
<div class="form-group">
<label for="inputPhone" class="control-label">Phone Number</label>
<input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Optional">
</div>
<div class="form-group">
<label for="inputMessage" class="control-label">Message</label>
<textarea class="form-control" rows="5" id="inputMessage" name="inputMessage" placeholder="Brief Description"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-custom pull-right hvr-underline-from-left">Send</button>
</div>
</form>
PHP
<?php
/* Set e-mail recipient */
$myemail = "myaccount#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = check_input($_POST['inputPhone'], "Optional");
$message = check_input($_POST['inputMessage'], "Brief Description");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Contact Message from mywebsite.net";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Phone: $phone
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:contact.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
Please change this line:
$phone = check_input($_POST['inputPhone'], "Optional");
to:
$phone = check_input($_POST['inputPhone']);
This way show_error($problem); won't be called.
Because function check_input include function show_error.
And function show_error have exit() when have errors.
So, your phone input == NULL => so have error and call to exit().
Solution for this case
Don't check require with phone number input.
PHP
<?php
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
/* Set e-mail recipient */
$myemail = "myaccount#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = $_POST['inputPhone'];
$message = check_input($_POST['inputMessage'], "Brief Description");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Contact Message from mywebsite.net";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Phone: $phone
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:contact.html');
exit();
?>
You are facing this error because you are validating the phone number i.e $_POST['inputPhone'] using the check_input function in the line
$phone = check_input($_POST['inputPhone'], "Optional");
You can avoid this error in multiple ways:
$phone = $_POST['inputPhone']; //not secure
OR
$phone = check_input($_POST['inputPhone'], "");
OR
$phone = filter_var($_POST['inputPhone'],FILTER_SANITIZE_STRIPPED);

How do I get the data to be sent to me in an email instead of populating on the same page?

I am new to php and I have post forms down but not I want some of my imput fields to be required.
I want this form to force the user to fill out the required fields but then be directed to my process.php page which will send me an email with the data the form collected. Right now the data is being posted at the bottom of the page. Please help me direct the data to an email.
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{$name = test_input($_POST["name"]);}
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{$email = test_input($_POST["email"]);}
if (empty($_POST["website"]))
{$website = "";}
else
{$website = test_input($_POST["website"]);}
if (empty($_POST["comment"]))
{$comment = "";}
else
{$comment = test_input($_POST["comment"]);}
if (empty($_POST["gender"]))
{$genderErr = "Gender is required";}
else
{$gender = test_input($_POST["gender"]);}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
I have the code to collect that data and email it. i just don't know how to get the form to validate and then direct to the process.php page.
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comment = $_POST['comment'];
$gender = $_POST['gender'];
$to = 'str#xxxxxxxx.com';
$subject = 'Executive Plaza Contact Form Response';
$message.= "Name: $name \n";
$message.= "Email: $email \n";
$message.= "email: $email \n";
$message.= "comment: $comment \n";
$message.= "gender: $gender \n";
$headers = "From: Eleven55\r\n";
mail($to, $subject, $message, $headers);
header('Location: thank-you.php');
Read up on the PHP mail() function here. It is basic and should get you started.
$to='you#domain.com';
$subject='Form data';
$body = "{$name} says {$comment}. \n Contact {$name} at {$email} or {$website}. \n {$name} is a {$gender}";
mail($to, $subject, $body);
http://php.net/manual/en/function.mail.php
$to='youremail#yourdomain.com';
$subject='Form data';
$body = "Name :$name \n ...";
mail($to, $subject, $body);
Read the mail function documentation for more info
My bad. Do this
document.forms.onsubmit = function(){ //onsubmit event
//validate form data using getElementById().val and so on
if(formdataisnotvalid){
alert('descriptive error message');
return false; // this will prevent the form from submitting
}

Contact Form with recaptcha producing 500 error

I have a contact form with recaptcha that i am trying to set up but produces a 500 error when information is entered and the submit button is clicked. I'm going to bet money that I have done something stupid as I'm not the greatest with PHP, so if I post my code here, would anyone be able to spot anything wrong?
This is the Contact Form itself.
<form class="email" action="mailer.php" method="post">
<p>Name:</p>
<input type="text" name="name" />
<p>E-mail:</p>
<input type="text" name="email" />
<p>Subject:</p>
<input type="text" name="subject" />
<p>Message:</p>
<textarea name="message"></textarea></p>
<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6Le8_t4SAAAAACIxacT6Xn8NVvDa93loylG-L6mk"></script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=PUBLICKEYGOESHERE" height="300" width="500" frameborder="0"></iframe>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
</noscript>
<input class="send" type="submit" value="Send">
</form>
And this is the mailer.php
<?php
require_once('recaptchalib.php');
if ($_POST['email'] != '')
{
$privatekey = "6Le8_t4SAAAAAME8kuqO1bvzcSWmGytwISUYLo3w";
$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
die("The reCAPTCHA wasn't entered correctly.
Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")");
}
else
{
/* Set e-mail recipient */
$myemail = "mandy#smarterbookkeeping.com.au";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");
$captcha = check_input($_POST['captcha']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.php');
exit();
/* Functions we used */
function check_input($data, $problem = '')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php
echo $myError;
?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
}
}
?>
I have the recaptchalib in the right place, same goes for the thanks.php. I'm not very good at PHP, but this one has me stumped.

Extracting HTML Form TextArea in PHP

Need help in understanding why the textarea ("message" below) field in the HTML5 form is not being grabbed from the PHP code (used to create email).
HTML Code (French):
<form method="post" action="contact.php">
<fieldset>
<p class="tight" style="font-size: 10pt; text-align: center;" >Message pour des demandes de renseignements d&apos;ordre <br /> général seulement. Veuillez utiliser demande formulaire situé <br /> sur la page Service pour commencer.</p>
<label><span>Nom</span>
<input class="inputcontact" name="name" type="text" /></label>
<label><span>Courrier <br />Électronique</span>
<input class="inputcontact" name="email" type="text" /></label>
<label><span>Téléphone</span>
<input class="inputcontact" name="telephone" type="text" /></label><br />
<label class="labelleft" for="message"> MESSAGE
<textarea name="message" rows="8" cols="45"> </textarea></label>
</fieldset>
<input class="submit" type="submit" value="ENVOYER" />
</form>
PHP Code (contact.php):
<?php
/* Set e-mail recipient */
$myemail = "xxxxxx#gmail.com";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['name']);
$email = check_input($_POST['email']);
$telephone = check_input($_POST['telephone']);
$messaage = check_input($_POST["message"]);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$subject ="Comment received from website";
$content = "Hello!
The contact form from the website has been submitted by:
Name: $yourname
E-mail: $email
Telephone: $telephone
Message:$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $content);
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
$messaage = check_input($_POST["message"]);
Message:$message.
is it ok or misspelling?
You have a typo in your variable name $message on line 9 (you wrote $messaage).
I recommend you to use filter_var($email, FILTER_VALIDATE_EMAIL); instead of your Regex. I think this would be more specific

Categories