Copy link to clipboard
Copied
Hi,
I am trying to add an auto responder to one of my existing forms I made in Dreamweaver. If it has to be php, please give me detailed instructions on how to set up the form, and more importantly, how to set up a php file. Also, where does that php file go? Where on the server?
Thanks so much!
Greg
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$company = substr(strip_tags(html_entity_decode($_POST['company'])),0,50);
$phone = substr(strip_tags(html_entity_decode($_POST['phone'])),0,15);
$subject = substr(strip_tags(html_entity_decode($_POST['subject'])),0,50);
$message = substr(strip_tags(html_entity_decode($_POST['message'])),0,200);
$email = filter_input(INPUT_POST, 'email', F
...Copy link to clipboard
Copied
A web form is made with HTML code. It consists of input tags surrounded by a pair of form tags. The form tag contains a property called action, which determines where the form will be processed.
PHP is the most common scripting language used to process forms, but there are other possibilities. The PHP code can be included in the same "page" as the form (usually at the very top), but can also be in a separate script (page).
A good introduction to processing forms with PHP is PHP SOLUTIONS by Powers.
Copy link to clipboard
Copied
Could you give me an example of a form with a successful php script? I'm lost with this.
Copy link to clipboard
Copied
I made a simple form like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP form to email sample form</title>
<!-- define some style elements-->
<style>
label,a
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px;
}
</style>
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>
<body>
<!-- Start code for the form-->
<form method="post" name="myemailform" action="form-to-email.php">
<p>
<label for='name'>Enter Name: </label><br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Enter Email Address:</label><br>
<input type="text" name="email">
</p>
<p>
<label for='message'>Enter Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" name='submit' value="submit">
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator = new Validator("myemailform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
frmvalidator.addValidation("email","email","Please enter a valid email address");
</script>
<p>
<a href='http://www.html-form-guide.com/email-form/php-form-to-email.html'
>PHP form to email article page</a>
</p>
</body>
</html>
It goes to a PHP file like this:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'tom@amazing-designs.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "tom@amazing-designs.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
Now, when I submit the form, I get a message saying "Thank You". I never get a responder in my email. What am I doing wrong?
Copy link to clipboard
Copied
What am I doing wrong?
There are a few things about your script that are wrong,
Try the following script (update the values for your email address). This script will work.
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($name && $email){
$message = 'Welcome to my world!';
$email_from = 'youremail@domain.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "youremail@domain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to,$email_subject,$email_body,$headers)){
$message="The message has been emailed.";
} else {
$message = "good email and name, but could not send.";
}} else {
$message = "Bad email, or bad name, or bad day!";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php echo $message; ?>
<form action='' method='post'>
<p>Email: <input name='email' type='email' required/></p>
<p>Name: <input name='name' type='text' required/></p>
<p><input name='submit' type='submit'/></p>
</form>
</body>
</html>
Copy link to clipboard
Copied
OK, I kind of got it. Instead of the message "Welcome to my world", how do I get it so it displays the user's message?
And how do I get it so the user gets an email, saying something like, "We got it"?
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($name && $email){
$message = 'Welcome to my world!';
$email_from = 'webmaster@voipccg.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "webmaster@voipccg.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to,$email_subject,$email_body,$headers)){
$message="The message has been emailed.";
} else {
$message = "good email and name, but could not send.";
}} else {
$message = "Bad email, or bad name, or bad day!";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php echo $message; ?>
<form action='' method='post'>
<p>Email: <input name='email' type='email' required/></p>
<p>Name: <input name='name' type='text' required/></p>
<p><input name='submit' type='submit'/></p>
</form>
</body>
</html>
Copy link to clipboard
Copied
In fact, I just want the user to receive an email saying their info has been submitted. And I want the person on the receiving end of the email, to get the email. So really, I just want a confirmation email to go to the user when they hit "Submit".
Copy link to clipboard
Copied
How do I get it so it displays the user's message?
You can use the textarea field you used in your original, like this:
<p>
<label for='message'>Enter Message:</label> <br>
<textarea name="message"></textarea>
</p>
Then at the top of my code example, add the sanitization for it like this:
$message= substr(strip_tags(html_entity_decode($_POST['message'])),0,300);
And delete theis line in my code:
$message = 'Welcome to my world!';
So really, I just want a confirmation email to go to the user when they hit "Submit".
Then replace the code as follows. New code in red
if (mail($to,$email_subject,$email_body,$headers)){
mail($email,$email_subject,"Your email has been received.",$headers);
$message="The message has been emailed.";
Copy link to clipboard
Copied
Here is a copy of my form I want to use:
<form id="form1" name="form1" method="post" action="http://www.dotster.com/scripts/formemail.bml">
<p><input type="hidden" name="my_email" value="webmaster@voipccg.com" />
</p>
<div align="center"><img src="../images/head_banner2.PNG" alt="VoIP CCG Virtual and Hosted PBX Business Phone Sytems" width="970" height="121" /> </div>
<table width="970" height="100%" border="0" align="center" bordercolor="#FFFFFF">
<tr>
<td height="21" colspan="2" background="images/tile_back.gif" bgcolor="#FFFFFF"><strong class="style6">Service Request Form </strong></td>
<td width="46%" background="images/tile_back.gif" bgcolor="#FFFFFF"> </td>
</tr>
<tr>
<td height="21" colspan="2" background="images/tile_sub.gif" bgcolor="#FFFFFF"><span class="style4">Please fill in all fields with a * </span></td>
<td background="images/tile_sub.gif" bgcolor="#FFFFFF"><div align="right"><span class="style6"><a href="file:///D|/voipccg.com/support_page.html" class="style33"></a></span><a href="../Customer_Login/support_page.html" class="style33">HOME</a></div></td>
</tr>
<tr>
<td width="6%" height="30" bgcolor="#66FFFF"><img src="../images/bc_new.gif" width="28" height="28" /></td>
<td width="48%" bgcolor="#66FFFF"><span class="style6"><strong>Full Name * </strong></span></td>
<td bgcolor="#66FFFF"><p class="style6"><strong>
<label for="Phone Number"></label>
<input name="Full Name" type="text" required="required" id="Full Name" />
<br />
</strong></p></td>
</tr>
<tr>
<td bgcolor="#66FFFF"><img src="../images/bc_new.gif" width="28" height="28" /></td>
<td height="48" bgcolor="#66FFFF"><span class="style6"><strong>Company *</strong></span></td>
<td bgcolor="#66FFFF"><p class="style6"><strong>
<label for="textfield2"></label>
<input name="Company" type="text" required="required" id="Company" />
<br />
</strong></p></td>
</tr>
<tr>
<td height="40" bgcolor="#66FFFF"><img src="../images/bc_new.gif" width="28" height="28" /></td>
<td bgcolor="#66FFFF" class="style6"><strong>E-mail *</strong></td>
<td bgcolor="#66FFFF"><input name="Email" type="email" required="required" id="Email" /></td>
</tr>
<tr>
<td height="40" bgcolor="#66FFFF"><img src="../images/bc_new.gif" width="28" height="28" /></td>
<td bgcolor="#66FFFF" class="style6"><strong>Phone *</strong></td>
<td bgcolor="#66FFFF"><input name="Phone Number" type="text" required="required" id="Phone Number" /></td>
</tr>
<tr>
<td height="40" bgcolor="#66FFFF"><img src="../images/bc_new.gif" width="28" height="28" /></td>
<td bgcolor="#66FFFF" class="style6"><strong>What is your measured internet speed? <br />
<span class="style35"> <<a href="http://www.speedtest.net">http://www.speedtest.net</a>></span></strong></td>
<td bgcolor="#66FFFF"><label>
<input name="Measured Internet Speed" type="text" id="Measured Internet Speed" />
</label></td>
</tr>
<tr>
<td height="85" bgcolor="#66FFFF"><img src="../images/bc_new.gif" width="28" height="28" /></td>
<td bgcolor="#66FFFF"><span class="style6"><strong>Message * </strong></span></td>
<td bgcolor="#66FFFF"><p class="style6"><strong>
<label for="textarea"></label>
<textarea name="Message" cols="50" rows="5" required="required" id="Message"></textarea>
<br />
</strong></p></td>
</tr>
<tr>
<td height="45" background="images/tile_sub.gif" bgcolor="#66FFFF"> </td>
<td background="images/tile_sub.gif" bgcolor="#66FFFF"><span class="style6"><strong>
<label> </label>
</strong>
<label></label>
</span>
<label>
<div align="center" class="style6"><strong>
<input name="submit" type="submit" value="Submit Form" />
</strong></div>
</label></td>
<td background="images/tile_sub.gif" bgcolor="#66FFFF"><span class="style6"><strong>
<label> </label>
</strong>
<label></label>
</span>
<label>
<div align="center" class="style6"><strong>
<input type="reset" name="Submit2" value="Reset Form" />
</strong></div>
</label></td>
</tr>
</table>
<input type="hidden" name="thankyou_url" value="http://www.voipccg.com/forms/form_thankyou1.html" />
<input type="hidden" name="required" value="Full Name,Company,Email,Phone Number,Message">
<input type="hidden" name="order" value="Full Name,Company,Email,Phone Number,Measured Internet Speed,Message">
<input type="hidden" name="subject" value="Service Request Ticket">
</form>
How do I make it so that when a user fills out this form and hits "Submit", a confirmation email goes to their email inbox? Meanwhile I want the actual form data to go to my inbox, in this case webmaster@voipccg.com.
Copy link to clipboard
Copied
I wrote a tutorial in 3 parts for creating a responsive contact form with Bootstrap and PHP code below.
Alt-Web Design & Publishing: Responsive Contact Form with Bootstrap 3.2 and PHP (Part 1)
NOTE: Your server must support PHP scripts and more specifically the PHP Mail() function. If unsure, contact your hosting provider to confirm. Otherwise, it won't work and you'll need to find another kind of script.
Nancy
Copy link to clipboard
Copied
ok, this is working out very nicely. Here is my code so far:
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($name && $email){
$email_from = 'webmaster@voipccg.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "webmaster@voipccg.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to,$email_subject,$email_body,$headers)){
mail($email,$email_subject,"Your email has been received.",$headers);
$message="The message has been emailed.";
} else {
$message = "good email and name, but could not send.";
}} else {
$message = "Bad email, or bad name, or bad day!";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php echo $message; ?>
<form action='' method='post'>
<p>Email: <input name='email' type='email' required/></p>
<p>Name: <input name='name' type='text' required/></p>
<p><input name='submit' type='submit'/></p>
</form>
</body>
</html>
Currently, if I do this, the user fills this out with their email address. The other guy gets an email from webmaster@voipccg.com. I want that person, on the receiving end to receive that new form submission, but the from address has to be the whatever email address the user submits when they do the form. So when the tech or agent receives the email, they can just hit "Reply" to the email.
Copy link to clipboard
Copied
Don't leave yourself open to header injection. That's like saying to bots," Hey look I'm vulnerable, use my site as a spam relay!"
The email you receive is coming from YOUR server. It's not coming from the user's email account.
Nancy
Copy link to clipboard
Copied
so what do I edit this line with to grab the user's email, and not just mine:
$email_from = 'webmaster@voipccg.com';//<== update the email address
Copy link to clipboard
Copied
$email_from = $email;//<== update the email address
Copy link to clipboard
Copied
One more thing:
This is the message I receive on the tech or agent side:
You have received a new message from the user Gregory.
Here is the message:
How do I get rid of it saying "webmaster@voipccg.com".
Copy link to clipboard
Copied
Replace the period with a semicolon, like this:
"Here is the message:\n $message";
Copy link to clipboard
Copied
I'll mention that I NEVER use the PHP mail function. It's a good way to get your emails delivered to the spam folder.
I use the swiftmailer class. David Powers has a good tutorial about it on lynda.com.
Copy link to clipboard
Copied
Thanks! It works great. Tomorrow, I'll continue adding form fields to this. If I run into trouble, I'll post to this forum again.
Copy link to clipboard
Copied
I was also wondering if I can put a capcha on my form. Some way to keep the go s way. Do you know of anything?
Copy link to clipboard
Copied
I am using reCAPTCHA: Easy on Humans, Hard on Bots
Copy link to clipboard
Copied
I personally really really hate recapchas. Very often it takes me more than one try to get it right, which is a buzz kill.
I have found that using a honeypot works well, and doesn't punish the form submitter.
Don't put style=display:none in the input tag of your honey pot. Put it in a class the honeypot uses. Make the honeypot seem like a normal question.
If a honey pot doesn't turn out to be enough, then validating user input can be an additional way to filter out spam. For instance, often the spam tries to put email addresses in any field, so you can reject form submissions that have email addresses as responses to questions like: do you like ice cream?
Copy link to clipboard
Copied
I agree, sometimes captcha's can be a pain, but I would like to learn about honeypot.
So here's my form:
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($name && $email){
$email_from = $email;//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "webmaster@voipccg.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to,$email_subject,$email_body,$headers)){
mail($email,$email_subject,"Your email has been received.",$headers);
$message="The message has been emailed.";
} else {
$message = "good email and name, but could not send.";
}} else {
$message = "Bad email, or bad name, or bad day!";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php echo $message; ?>
<form action='' method='post'>
<p>Email: <input name='email' type='email' required/></p>
<p>Name: <input name='name' type='text' required/></p>
<p><input name='submit' type='submit'/></p>
</form>
</body>
</html>
So you're saying honeypot is just a tag? Or do I need to set up for it to link to images in a folder on my server?
How would you do it?
Copy link to clipboard
Copied
To create a honeypot:
1. Within your form, add the following:
<p><label class='automake'>What is the make of your automobile? <input type='text' name='auto' value='$auto'/></label></p>
2. n your CSS style sheet, add the following:
.automake, label.automake { display: none; }
3. Wrap your PHP processing code in the following
if (empty($_POST['auto'])){
// all your code here
} //closing brace
Copy link to clipboard
Copied
Thanks.
I was hoping, and sorry for taking up so much of your time. I seem to be having trouble adding form fields. I need a Subject, Name, Email, Phone number, Company, and a message field. Every time I add these, I get an error that the message cold not be sent:
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($name && $email){
$email_from = $email;//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "webmaster@voipccg.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to,$email_subject,$email_body,$headers)){
mail($email,$email_subject,"Your email has been received.",$headers);
$message="The message has been emailed.";
} else {
$message = "good email and name, but could not send.";
}} else {
$message = "Bad email, or bad name, or bad day!";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php echo $message; ?>
<form action='' method='post'>
<p>Email: <input name='email' type='email' required/></p>
<p>Name: <input name='name' type='text' required/></p>
<p><input name='submit' type='submit'/></p>
</form>
</body>
</html>
Copy link to clipboard
Copied
And all of those things, of course, have to appear to the person receiving the email, or the agent. The user can still just get a message saying that we received your message.