Properly coding Email Form with PHP? [closed] - php

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.

Related

Why is my PHP/HTML form not sending me data

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.

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">

Form send button php not working [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 7 years ago.
Improve this question
I have a simple form:
<form action="send_form_email.php" method="post" enctype="text/plain">
<input type="text" name="first_name" placeholder="NAME:" size="100" class="grey-bg"/>
<input type="text" name="last_name" placeholder="E-MAIL:" size="100" class="grey-bg"/>
<br />
<br />
<textarea type="text" name="message" rows="20" cols="201" placeholder="MESSAGE:" size="162"></textarea>
<br />
<div>
<input id="contact-send-btn" type="submit" value="Send">
</div>
</form>
And this is the php file:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xxxxxxxx";
$email_subject = "Hello";
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['last_name']) ||
!isset($_POST['message']) ||
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // 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_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 First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last 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 .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Message: ".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.
<?php
}
?>
However when I click the button it just opens the php file in the browser and nothing is received in my inbox? All I want is to receive the email with the details in the form?
You have several problems.
Encoding Type
enctype="text/plain"
PHP does not support plain text encoded form data. Remove that attribute (and use the default encoding type).
See also the spec:
Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at the end of the value).
Test for submission
You said if(isset($_POST['email'])) {
… but you don't have any field with name="email". Test to see if a field you actually have exists*
Opening PHP in the browser
This statement is a little ambiguous:
it just opens the php file in the browser
If you mean that your browser displays the PHP source code, then see this question: PHP code is not being executed (I can see it on source code of page)
Asides
placeholder="NAME:"
The HTML5 placeholder attribute is not a substitute for the label element
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
This fails to match many perfectly valid email addresses (such as those with a + in the section before the # or those from the .museum top level domain).
You did not add name attribute to submit button.
Change
<input id="contact-send-btn" type="submit" value="Send">
To:
<input id="contact-send-btn" type="submit" value="Send" name="email"/>
While posting forms, only elements with name get posted.
In your case, submit button has no name attribute, so, it was not getting posted.
Hence, it is not coming into the if.
Looks like you don't have apache , php installed thats why you are seeing the content of the .php file . If you are on Windows Install XAMPP if you are on Linux Install LAMP Stack . Hope this helps

My PHP form won't send e-mail to me and redirects to a blank page

I have researched this question, but I don't think I will find an answer, because I'm guessing it is something wrong with my code personally. I have tried every combination of trying to get my PHP contact form to Submit & send me an e-mail but it does nothing, even when it has been uploaded to the server. It simply displays a blank page once I click submit, and I get no e-mail at all to the e-mail address I entered into the code. I don't know what I'm missing as I can't find an answer to my problem anywhere.
One thing I did notice was that I have some different fields in my HTML than specified in the PHP to be required fields. However, I don't know that this makes a difference as I'm not getting any error messages or anything else for these fields. Please let me know if you need any additional details. I appreciate your help in advance.
HTML
<form id="form_884913" method="post" action="send_form_email.php" class="appnitro">
This is where ALL of my fields are, so I omitted it since quite a large script. Please let me know if you need any of this to help me
Rest of HTML
<input type="hidden" name="form_id" value="884913" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
<input type="reset" value="Reset">
</form>
PHP
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemailaddresswastyped#blank.com";
$email_subject = "Contact";
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['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!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
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // 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 First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last 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($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\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 me! I will be in touch with you very soon.
<?php
}
?>
As mentioned by GaijinJim, if it isn't already a field you've posted, since you omitted some of the fields, make sure to add a field in HTML:
<input type="hidden" name="email" value="" />
Might also add for testing, but not for production, to the last line in PHP:
<?php
} else {
echo "$_POST['email'] is not set.";
}
?>
If you want to use $_POST (Same with $_GET method) with a form in HTML&PHP you must name your field like so:
<input type="text" name="MyfieldName" value="1">
Only then you can call your $_POST['MyfieldName']
Now if you would echo $_POST['MyfieldName']; it would return 1
In your PHP-file you check whether all of the following input fields exist in your form,
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
Thus in order for your form to validate all of those fields must exist, you must have,
<input type="text" name="first_name" />
<input type="text" name="last_name" />
<input type="text" name="email" />
<input type="text" name="telephone" />
<input type="text" name="comments" />
If you do not want some of those fields, for example email, simply remove `!isset($_POST['email']) from your PHP's if-statement.
As per your comment I get the impression that the input field named email does not exists, which is the reason why you get a blank page. If you were to add that input field on your HTML-page you would see some sort of error message instead of a blank page :)

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