I am trying to make a contact form using HTML and PHP. The PHP for the form is below:
<?php
if(isset($_POST['email'])) {
// Email to information
$email_to ="personalemail#email.com";
$email_subject ="Contact";
$email_from ="Person";
// Error code
function died($error) {
echo "We are sorry, but there were error(s) found with the form you submitted.";
echo "These errors appear below.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
// Validation
if(!isset($_POST['fname']) || !isset($_POST['lname']) || !isset($_POST['email']) || !isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message .= 'The email address you entered does not appear to be valid.<br/>';
}
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $fname)) {
$error_message .= 'The first name you entered does not appear to be valid.<br/>';
}
if(!preg_match($string_exp, $lname)) {
$error_message .= 'The last name you entered does not appear to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered does not appear to be valid.<br/>';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string($fname) . clean_string($lname) . "\n";
$email_message .= "Email:" . clean_string($email) . "\n";
$email_message .= "Message:" . clean_string($message) . "\n";
// Create email headers
$headers = 'From: ' .$email_From . "\r\n". 'Reply-To:' . $email. "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
Thankyou for contacting me. I will be in contact with you shortly. <br/>
Please click here to go back to the main website
<?php
}
?>
The issue is that when the form is submitted, it never sends an email to my own personal email. Is there something more I have to set up with the web hosting or is the problem purely in the code? I did some research about the appropriate code to put in and have tried many different options but none seem to work.
Forms that send email are errorprone, and worse yet, if something goes wrong neither you nor the end user usually get an error message. They will just think your customer service is not responding. Things that do go wrong: webserver email server down or malconfigured, recieving mailbox full, spamfilter eat email, rules on webserver's emailserver change and throws away your mails.
I recommend a cloud hosted form, that stores the form submissions and notify you by email, but email is not the primary data storage.
Something like this perhaps, has a free plan:
http://www.wufoo.com/features/
Related
I have a PHP contact form, when a user fills the form and hits send button the form gets redirected to the home page. I want to send a success/failure message on the home/index page while redirecting. How can I do the same?
I have an idea to store a success/failure message in a session and then call it on the home page but touching php afer a long time hence need assistance on how to achieve the same. Any ideas and thoughts will be greatly appreciated.
Form Codes (it's on a separate file than home page):
<?php if (isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "admin#hillierroaddentalclinic.com.au";
$email_subject = "Enquiry from Hiller Road Dental Clinic Website";
function died($error)
{
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error . "<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if (strlen($message) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if (strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string)
{
$bad = array(
"content-type",
"bcc:",
"to:",
"cc:",
"href"
);
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email) . "\n";
$email_message .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Message: " . clean_string($message) . "\n";
// create email headers
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
function Redirect($url, $permanent = false)
{
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit();
}
Redirect('http://hillierroaddentalclinic.com.au/', false); } ?>
Contact from PHP
if(mail($email_to, $email_subject, $email_message, $headers )){
header("location: index.php?status=1"); //success
}else{
header("location: index.php?status=0"); //failure
}
in index.php
if(isset($_GET['status'])){
$status = $_GET['status'];
if($status == 1){
echo "Thank you for your message";
}else if($status == 0){
echo "Unable to send message";
}
}
Getting the status value from URL in index.php, if the value is '1' then success message, if the status is '0' then the error message.
For achieving a redirection with PHP either you have to use header functionality. This will redirect to the page which you need to load. Otherwise, you can use form actions to change get redirection after form submission.
You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the declaration, for example).
header('Location: '.$URL);
Here you can pass a parameter with a header according to the status. For example when you call header you can pass a parameter for displaying the message. Pass a variable and according to the variable show appropriate message.
In your case after sending an email your page in the header and either display message from the session and reset session or pass a variable with status and checking the status of the message display appropriate message in UI
PHP Header Reference
It's not a good practice to send an email like these if you are using it in a production environment. it may blacklist your domain. Use any SMTP credentials and mailer class to do it
I am trying to send a data from a form to my email.
I try using the form in my server (free hosting) and it is fine. When I try to use it on another server (runs through cloudflare ) it gives me 504 gateway error. Any idea why?
Is it on my side or do I have to fix something?
My server page : http://mywg1.x10host.com/m1/
Cloudflare run page : http://wholesaledrywalltoronto.therenopros.ca/
the PHP for the form :
<?php
if(isset($_POST['Email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email.x#gmail.com";
$email_subject = "Drywall Pros Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['Name']) ||
!isset($_POST['Email']) ||
!isset($_POST['Phone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$Name = $_POST['Name']; // required
$Email = $_POST['Email']; // required
$Phone = $_POST['Phone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$Name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($Name)."\n";
$email_message .= "Email: ".clean_string($Email)."\n";
$email_message .= "Phone: ".clean_string($Phone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Fixed it . For anyone who is having same errors . It is probably because the server you are working with only allows SMTP and not the PHP mail function.
I am quite new to php so excuse my poor knowledge but I am using an email form for users to register on my web page but I am getting errors running it.
the full script is as follow:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "contact#slapmybeat.com";
$email_subject = "New e-mail subscriber";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form your submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if
(!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the email your submitted.');
}
$email_from = $_POST['email']; // required
$error_message = "";
$email_exp = "/^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/";
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?
}
?>
First I had a "eregi depreciated" error because of the initial script which was as follow:
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
After googling it I replaced it with:
$error_message = "";
$email_exp = "/^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/";
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
but now it is giving me the following, although the email adress is correct:
We are very sorry, but there were error(s) found with the form your
submitted. These errors appear below.
The Email Address you entered does not appear to be valid.
Please go back and fix these errors.
any help will be much apreciated
eregi is case-insensitive, preg_match is not.
You have to add i to the end of the pattern (/..../i).
Not a direct answer, but if you want to check for a valid e-mail address, it is a lot easier to use php's built-in filters:
$email = filter_var($email_from, FILTER_VALIDATE_EMAIL);
Now $email will contain the filtered e-mail address or false if the filter failed (not a valid address in this case).
You should escape the "." inside the [] because it will match every character if you don't do it, you'll have:
"/^[A-Z0-9\._%-]+#[A-Z0-9\.-]+\.[A-Z]{2,4}$/i"
I've created a PHP file to work with my form on my jQuery mobile site. The form works perfectly and sends the email and errors work and all. But I keep getting the email from an (unknown sender). Subject line and email information is there. (Email coming from the form to host the host email address). Thanks for any help that can be provided.
<?php
if(isset($_POST['email'])){
// Here is the email to information
$email_to = "hostemail#email.com";
$email_subject = "Customer Service Form";
$email_from = "Company";
//error code
function died($error){
echo "We are sorry, but there were errors found with the form you submitted.";
echo "These errors appear bellow.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
//validation
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
//$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z] {2,4}$/';
//(!preg_match($email_exp, $email)) {
//$error_message .='The Email Address you entered does not appear to be valid.<br/>';
// }
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $name)){
$error_message .= 'The name you entered does not seem to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The comments you entered do not appear to be valid.<br/>';
}
if(strlen($error_message) > 0 ) {
died($error_message);
}
$email_message = "Form details below. \n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string ($name) . "\n";
$email_message .= "E-Mail:" . clean_string ($email) . "\n";
$email_message .= "Message:" . clean_string ($message) . "\n";
//create email headers
$headers = 'From:' .$email_From . "\r\n" . 'Reply-To:' . $email. "\r\n" .
'X-MAILER: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success Message Goes Here -->
Thank you for contacting us. We will be in touch with you shortly. <br/>
Please Click here to go back to the contact page.
<?php
}
?>
The format for the From header is:
Display Name <email address>
For example:
Company <foo#company.com>
Right now, you're just using "Company", which is neither a valid e-mail address on its own, nor has an e-mail address at the end.
Ok, I have a form which sits inside of a popup box. I want the confirmation message to display inside of the pop up box when the form has been successfully submitted.
How do I do this?
I have the site live that I am working on, take a look here www.firestarmediallc.com
When you get to the site, click the GET QUOTE link on the top of the page and the box will appear.
Actually, if you guys could take a look at the mailer functions and see if those are correct too, that would be awesome. I have been trying to get this form functional for the last two days and haven't had any luck.
Sorry, I just realized that you can't see the php file. Thank you in advance. Here it is:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "tabethamoe#yahoo.com";
$email_subject = "Quote Request";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
$('#contactform').submit(function () {
sendContactForm();
return false;
});
<?php
}
?>
The site's not loading, but if you want a form to submit to itself, you simply have:
[... deleted irrelevant code, now that I can see how the site works...]