Why is my PHP/HTML form not sending me data - php

I am trying out PHP after years of inactivity, and I thought I had it but looks like I lost the touch. Can anyone see why I might not be getting any data sent to my email address?
HTML code:
<form name="Call Back Request" id="request-call-form" action="callbackrequest.php" method="POST" novalidate="novalidate">
<div class="col-md-6">
<input id="name" type="text" placeholder="Name" name="name">
<input id="email" type="text" placeholder="Email" name="email">
</div>
<div class="col-md-6">
<input id="phone" type="text" placeholder="Phone" name="phone">
<input id="subject" type="text" placeholder="Subject" name="subject">
</div>
<div class="col-md-6">
<button type="submit" value="submit" class="thm-btn">submit now</button>
</div>
<div class="col-md-12">
<div id="success"></div>
</div>
</form>
PHP Code:
<?php
if (isset($_POST['email'])) {
$email_to = "xxxxx#xxxx.com";
$email_subject = "Call Back Request Form - Home Page | rootlayertechnologies.com.au";
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 and submit the form again.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['subject'])) {
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['subject']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email_from)) {
$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($subject) < 2) {
$error_message .= 'The Subject 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 = [
"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 .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Subject: " . clean_string($subject) . "\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 -->
<p>Thanks for contacting us. We have received your request for a call back and a friendly member of our Product Solutions team will be in touch with you soon.</p>
<?php}?>

I would begin by removing the <a> tag inside the <submit> button. I think that by clicking the link, you may be just opening the page instead of submitting the form.
Then, on the PHP code, i would confirm that $email_to is properly set. (It may seem obvious, but it's always worth confirming.)
I couldn't test the code, but i didn't find any bugs per se. (Some things could be improved, e.g. lack of filter_var and perhaps html_escape, addslashes, or strip_tags.)
This leads me to think that the problem may be at the server level. So, here are some things to consider:
From: must be an address from the same domain as the site (e.g. info#rootlayertechnologies.com.au). Keep Reply-to: as is, though.
Ensure that the SPF record is properly set. You may also want to delve into DKIM and DMARC.
Some hosting companies disable PHP mail. You may need to implement SMTP.

Related

Contact Form, sending an email is just downloading my PHP file

I'm having a weird error that I cannot figure out with a PHP file, I've never touched PHP and I am trying to use it to send a Name, Email Address, and a message back to me from a website, but for some reason, the code I wrote is just downloading the PHP file instead, can anyone else see where I may be going wrong here:
HTML
<div class="modal-body">
<div class="container"> <!-- TODO add in contact form -->
<form action="form-to-action.php">
<label for="fname">Name</label>
<input type="text" id="Name" name="Name" placeholder="Name">
<label for="lname">Email Address</label>
<input type="text" id="Email" name="Email" placeholder="Email Address">
<br>
<label for="subject">How can we Help?</label>
<textarea id="Message" name="Message" placeholder="Write something.." style="height:200px"></textarea>
<input type="submit" value="Send"id="contactBtnModal"style="text-decoration: none;" >
</form>
</div>
</div>
PHP:
<?php
if (isset($_POST['Email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "testingemail#gmail.com";
$email_subject = "Testing";
function problem($error)
{
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['Message'])
) {
problem('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['Name']; // required
$email = $_POST['Email']; // 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 Message you entered do not appear to be valid.<br>';
}
if (strlen($error_message) > 0) {
problem($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 .= "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);
?>
<!-- include your success message below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Are you installed a PHP server in your Computer?
If not, download WAMP and try again, you need access the page via http://localhost, and not via file://path_to_document in your web navigator.
Or run the script in a web server.

Sending an html form email with PHP [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 5 years ago.
When I click my submit button, It pulls up my form-to-email.php source code. This is my first time using any PHP. How do i get it to send the email? It could just be linked incorrectly or formatted incorrectly.
<form method="post" name="myemailform" action="form-to-email.php">
<div>
<label for="name">Name:</label><br>
<input type="text" id="name" name="user_name" size="45"><br>
</div>
<div>
<label for="email">Email:</label><br>
<input type="text" id="email" name="user_email" size="45"><br>
</div>
<div>
<label for="message">Message:</label><br>
<textarea type="text" id="message" name="user_message" size="600"></textarea><br>
</div>
<div>
<input type = "submit" value = "Send Form">
<input type = "reset" value = "reset">
</div>
</form>
here is my PHP code
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "codymaheu#yahoo.com";
$email_subject = "Testing";
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['user_name']) ||
!isset($_POST['user_email']) ||
!isset($_POST['user_message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['user_name']; // required
$email_from = $_POST['user_email']; // required
$comments = $_POST['user_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_from)) {
$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 First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Message 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_from)."\n";
$email_message .= "Message: ".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
}
?>
add name into the button like this then try again
<input type = "submit" value = "Send Form" name="email">

Properly coding Email Form with PHP? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So, two problems:
1.) My email form is not working, on submit, it advises "web page can't be displayed".
2.) The email form, when properly coded, did not send an email.
I'm trying to get my email form to properly work. It was working, (however was not sending email) then I rearranged some things (added subject line in the form). Now, I can't seem to figure out what went wonky once I added a subject line. Any suggestions?
https://jsfiddle.net/ebxam743/1/
My PHP form is in the CSS section of JSFIDDLE.
Contact Form HTML
<div class="col-md-8">
<form name="contactform" method="post" action="send_form_email.php">
<div class="row contact-row">
<div class="col-md-6 contact-name">
<input type="text" name="first_name" placeholder="Name">
</div>
<div class="col-md-6 contact-email">
<input type="text" name="email" placeholder="E-mail*">
</div>
</div>
<input type="text" name="subject" placeholder="Subject*">
<textarea name="comments" placeholder="Message"></textarea>
<input type="submit" class="btn btn-lg btn-color btn-submit" value="Send Message">
</div>
</div>
</form>
<!-- end col -->
PHP (can't get it to format properly)...
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you#yourdomain.com";
$email_subject = "Your email subject line";
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['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['subject']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Message 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($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($subject)."\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 -->
<center><img src="img/rebelliouslogob.png"
Thank you for contacting us. We will be in touch with you very soon.</center>
<h1>Go Back</h1>
<?php
}
?>
Okay, so I found 2 errors.
First error
This line:
echo "Please go back and fix these errors.<br /><br />";
Should be changed to:
echo "Please go back and fix these errors.<br /><br />";
Because the quote symbol is breaking the string, so if you need to use the quote symbol inside a string you need to escape it by adding a backslash or create strings using single quotes.
Second error
You are referring to a variable called $subject that doesn't exist.
So instead of using a variable called $telephone which contains the subject data, change the name of it to $subject (you never use the variable $telephone, so it shouldn't affect you).
This line:
$telephone = $_POST['subject']; // not required
Should be changed to:
$subject = $_POST['subject']; // not required
EDIT: Tested the code on my server after the two changes I mentioned above, and I received an email as expected.

Form not submitting in Chrome and Safari

I am new to PHP, so I am not sure what is wrong with the code. I have tested the form in Internet Explorer, FireFox, Chrome, and Safari and it works great in both I.E. and FireFox, but it doesn't work in Chrome or Safari. In both Chrome and Safari I get the successfully submitted page, but I do not get an email sent to me.
HTML page:
<form name="balxfrform" action="baltransfer.php" method="POST">
<input type="hidden" name="_SUBJECT" value="Transfer Request Form">
<b>* Name:</b> <input name="name" type="text" size="60"><br>
<b>* Email:</b> <input name="email" type="text" size="60"><br>
<b>Member Number (Last 3 Digits) XXX:</b> <input name="account" type="text" size="10"><br>
<b>Card Number:</b> <input name="ccnumber" type="text" size="40"><br>
<b>Phone Number:</b> <input name="pnumber" type="text" size="20"><br>
<b>Best Time to reach you<sup>1</sup>:</b> <input name="time" type="text" size="40"><br>
<b>* I agree to the terms and conditions listed below:</b> Yes <input name="terms" type="checkbox" value="Yes"><br>
<input type="submit" value="Submit">
</form>
PHP page:
<?php
if(isset($_POST['email'])) {
$email_to = "email#test.com";
$email_subject = "Transfer Request Form";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted.<br /><br /> ";
echo $error."<br /><br />";
echo "Please go back and fix the error(s).<br /><br />";
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['account']) ||
!isset($_POST['ccnumber']) ||
!isset($_POST['pnumber']) ||
!isset($_POST['time']) ||
!isset($_POST['terms'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$account = $_POST['account']; // not required
$ccnumber = $_POST['ccnumber']; // not required
$pnumber = $_POST['pnumber']; // not required
$time = $_POST['time']; // not required
$terms = $_POST['terms']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$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($error_message) > 0) {
died($error_message);
}
if(!isset($terms)) {
$error_message .= 'You must agree to the Terms and Conditions to continue.';
}
$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 .= "Member Number XXX: ".clean_string($account)."\n";
$email_message .= "Card Number: ".clean_string($ccnumber)."\n";
$email_message .= "Telephone: ".clean_string($pnumber)."\n";
$email_message .= "Best Time to be Reached: ".clean_string($time)."\n"."\n";
$email_message .= "Agree to Terms and Conditions: ".clean_string($terms)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: email#test.com'.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
Probably there will be nothing to do with the PHP part. PHP is run on the server side. Since one browsers interpret HTML code differently, this might be an HTML syntax issue of your HTML form. You may try validating your HTML. Try this tool .
Try following solutions
1.) add # before mail() function
ex : #mail($email_to, $email_subject, $email_message, $headers);
2.)if (!mail(...)) {
// again Call your code
}

Email form from a website via PHP issues

I am coding a personal website and having an issue with my contact from. If you can help me find what's wrong I would really appreciate it.
The link to the website is www.tiryakicreative.com and the code for the php form is given below:
<div id="form">
<form id="ajax-contact-form" action="contact_form/send_form_email.php…
<fieldset class="info_fieldset">
<div id="note"></div>
<div id="fields">
<label>Name</label>
<input class="textbox" type="text" name="name" value="" />
<label>E-Mail</label><input class="textbox" type="text" name="email" value="" />
<label>Subject</label>
<input class="textbox" type="text" name="subject" value="" />
<label>Message</label>
<textarea class="textbox2" name="message" rows="5" cols="25"></textarea>
<label> </label><input class="button" type="image" src="send2.gif" id="submit" Value="Send Message" />
</div>
</fieldset>
</form>
</div>
</div>
Here is the php code for the given html code:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "ian_tiryaki#hotmail.com";
$email_subject = "New Email from Website";
function died($error) {
// ERROR CODE GOES HERE
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.";
echo $error."";
echo "Please go back and fix these errors.";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
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['subject']; // not required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Z…
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The subject you entered does not appear to be valid.';
}
if(strlen($comments) < 2) {
$error_message .= 'The message you entered do not appear to be valid.';
}
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:",…
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($telephone)."\n";
$email_message .= "Message: ".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 success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Are you receiving the emails after the user sends the email?
Try adding
error_reporting(E_ALL);
to the top of your script.
You could also try removing the # from the mail command
mail($email_to, $email_subject, $email_message, $headers);
As this will be suppressing any errors that is being generated.
It could be something as simple as the PHP mail function having additional headers disabled (some hosts do this for security reasons) in which case the mail function will fail.
set this is in your action form ...
and you will definately get mail from here..
and although there is an error you may be use use below code for send mail using php without declare a variable...
like
$email_to=$_POST['email'];
$email_subject=$_POST['subject'];
$email_message=$_POST['message'];
$headers=$_POST['title'];
mail('$email_to', '$email_subject', '$email_message', '$headers');
otherwise
use below code for send mail
mail('$_POST['email']','$_POST['subject']','$_POST['message']','$_POST['title']');

Categories