How to make autoresponse email for custom optin form - php

I am using a custom optin form on My lead page
I want to add an autoresponse confirmation email to be sent to everyone who submits the form. Here is code currently being used in mail-script.php.
<?php
/* Set e-mail recipient */
$myemail = "email#gmail.com";
/* Check all form inputs using check_input function */
$fname = check_input($_POST['fname'], "Enter your first name");
$lname = check_input($_POST['lname'], "Enter your last name");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$altphone = check_input($_POST['altphone']);
$resort = check_input($_POST['resort']);
$amount = check_input($_POST['amount']);
$call = check_input($_POST['call']);
/* 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 = '';
}
/* Lets Save in txt file */
$text = "$fname\n$lname\n$email\n$phone\n$resort\n$amount\n$call\n\n";
file_put_contents('secretfilenae.txt', $text, FILE_APPEND);
/* Let save in cvs file */
$fields = array($fname,$lname,$email,$phone,$resort,$amount,$call);
$fp = fopen('secretfilenae.csv', 'a');
fputcsv($fp, $fields);
fclose($fp);
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
First Name : $fname
Last Name : $lname
E-mail: $email
Phone : $phone
Alt Phone : $altphone
Resort: $resort
Amount: $amount
Call : $call
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: index.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)
{
?>
if (strtolower($_POST['code']) != 'mycode') {die('Wrong access code');}
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>

Use the php mail() function:
mail($myEmail, $headerForEmail, $message);

Related

Post contact form to html document

I currently have a contact page that emails me when it gets submitted. I want to send the fields to another htm page on the server instead so staff can access the information by viewing the page in their browser. So even if they don't have access to email, they can view the page from their browser.
The information sent must accumulate on the form, ie new submissions must submit to the ame form without deleting previous submissions.
Do I amend the code? Do I make a few changes like changing the 'mymail' to 'myform = "form.htm" ' I made a few changes and tried submitting but have no idea how to make the form work.
Below is the code that sends the email.
<?php
/* Set e-mail recipient */
$myemail = "mail#abc.co.za";
$subject = "Contact Form";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name']);
$contactnr = check_input($_POST['contactnr']);
$email = check_input($_POST['email']);
$phoneoremail = check_input($_POST['phoneoremail']);
$message = check_input($_POST['message']);
$ipaddress = $_SERVER['REMOTE_ADDR'];
$iphost = $_SERVER['HTTP_HOST'];
/* 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 = '';
}
/* Prepare the message for the e-mail */
$message = "Contact form has been submitted
Details:
Name: $name
Phone Number: $contactnr
E-mail: $email
Message: $message
Sender IP Address: $ipaddress
Form submitted via server: $iphost
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thankyou.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)
{
?>
<html>
<body>
<b>Please provide missing information so we can assist you:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
If you want to post this to an HTML page aswell as sending it per E-Mail, why don't you just write to an HTML file?
$file = 'formdata.html';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "Details:<br>
Name: $name<br>
Phone Number: $contactnr<br>
E-mail: $email<br>
Message: $message<br>
Sender IP Address: $ipaddress<br>
Form submitted via server: $iphost<br><hr>";
// Write the contents to the file
file_put_contents($file, $current);
Maybe include formdata.html then as an iFrame on a page only the staff can access?

PHP form data not collecting

I'm creating a simple form in bootstrap. I'm not that familiar with php, but I came up with the below based on what I can find online. When a user clicks on my submit button, an email is sent, all the form fields are indicated in the email I receive, but the data is not collected. What am I doing incorrect? PHP below and form can be viewed at: http://esdclient.com/formb/masterform.html
<?php
/* Set e-mail recipient */
$myemail = "ken#elschwartzodesign.com";
/* Check all form inputs using check_input function */
$Name = check_input($_POST['Name']);
$Last = check_input($_POST['Last']);
$City = check_input($_POST['City']);
$State = check_input($_POST['State']);
$Zip = check_input($_POST['Zip']);
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $Name
Last: $Last
City: $City
State: $State
Zip: $Zip
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.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)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
enctype is wrong, try enctype="multipart/form-data"

php form validation required fields

I have a php form that i'm trying to make required fields for but I cannot seem to get it to work right and I know its something i'm missing. I just need to make the form require $name $bank $email and $phone.
<?php
/* Set e-mail recipient */
$myemail = "email#email.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$bank = check_input($_POST['bank'], "Enter your name");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$headphones = check_input($_POST['headphones']);
$subject = "Allied Affiliated Funding - Bose Landing Page Form Submission";
/* 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 = "Hello!
Your contact form has been submitted by:
Name: $name
Bank: $bank
E-mail: $email
Phone: $phone
Head Phones: $headphones
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
if($_POST['headphones'] == Yes){
header("Location: landing1/thank-you-bose.html");
}
else {
header("Location: landing1/thank-you.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();
}
?>
I have looked at several different tutorials on how to complete this but none of the solutions seem to be working.
I think your problem is at this line
if($_POST['headphones'] == Yes){
It needs to be
if($_POST['headphones'] == "Yes"){
You dont have a variable named Yes so i think you want to check if the content of headphones is "yes".

PHP Validation showing all errors at once

I am using the below script for validation. I want to know if there is a way to convert this script to show all errors at once instead of one at a time? Also, is there anything more I can do to prevent header injection?
thanks.
<?php
session_start();
/* Check all form inputs */
$fname = check_input($_POST['fname'], "Friend's Name cannot be empty.");
$femail = check_input($_POST['femail'], "Friend's email cannot be empty.");
$yname = check_input($_POST['yname'], "Your Name cannot be empty.");
$yemail = check_input($_POST['yemail'], "Your email cannot be empty.");
$subject = check_input($_POST['subject'], "Subject cannot be empty.");
$comments = check_input($_POST['comments'], "Comments cannot be empty.");
/* alphabet only */
if(!preg_match("/^([A-Za-z\s\-]{2,45})$/i", $fname))
{
show_error("Friend's name is not valid.");
}
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $femail))
{
show_error("Your friend's email address is not valid.");
}
if(!preg_match("/^([A-Za-z\s\-]{2,45})$/i", $yname))
{
show_error("Your name is not valid.");
}
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $yemail))
{
show_error("Your email address is not valid.");
}
htmlentities ($message, ENT_QUOTES);
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlentities($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
Something I often do is use an $errors array. So you would define an empty array before all your checks, and then inside of each check for show_error you would add that string to your errors array:
$errors[] = "Friend's name is not valid.";
Then at the end, check to see if the error array is empty. If it is, then nothing failed. Otherwise, you now have an array of all the errors that you can display however you'd like.

queries regarding PHP mail() 'issues'

So I recently made a basic site for a family members small company. I included a mail form, for enquiries etc.
here is the code i use:
<?php
function check_input($data){ // SANITIZE USER STRING INPUT
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$name = check_input($_POST['name']);
$surname = check_input($_POST['surname']);
$email = check_input($_POST['email']);
$telephone = check_input($_POST['telephone']);
$comments = check_input($_POST['message']);
$message = "From: $name $surname
Email: $email
Telephone: $telephone
--------------------------------------------------------------
Comments: $comments
";
mail("#############.com","Website Enquiry from www.#######.co.uk",$message,"From: webserver");
?>
now when I try it, it works absoloutely fine. However I have noticed sometimes it is realllllly slow and so we have been receiving blank emails through the form (the user input data is not present), so it appears someone has attempted to use it and given up perhaps because it is taking too long?
I am assuming this is to do with the mail server rather than php mail. But I wanted to see if anyone could highlight potential issues that I could take to the company hosting for her?
many thanks,
check if name and email fields are entered and then proceed with mail function..this reduces getting blank emails.
<?php
if (isset($_POST['name']) && isset($_POST['email'])) //check if name and email fields are entered and then proceed with mail function
{
//process the data and send mail.
}
else
{
echo "Error missing name or email field.please enter";
}
?>
Alternatively you can also use array_key_exists()
<?php
if (array_key_exists("name", $_POST) && $_POST["name"] != "" && array_key_exists("email", $_POST) && $_POST["email"] != "")
//check if name and email fields are entered and then proceed with mail function
{
//process the data and send mail.
}
else
{
echo "Error missing name or email field.please enter";
}
?>
Actually you are not checking if someone fill the form empty that's why you are getting blank fields
<?php
function check_input($data){ // SANITIZE USER STRING INPUT
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(!empty($data))
{
$name = check_input($_POST['name']);
$surname = check_input($_POST['surname']);
$email = check_input($_POST['email']);
$telephone = check_input($_POST['telephone']);
$comments = check_input($_POST['message']);
$message = "From: $name $surname
Email: $email
Telephone: $telephone
--------------------------------------------------------------
Comments: $comments
";
mail("#############.com","Website Enquiry from www.#######.co.uk",$message,"From: webserver");
}
?>

Categories