PHP Contact Form not submitting - php

Hi I've previously used this very simple php contact script with success though when I've tried implementing it on a new HTML page with the form won't submit. Can anyone see any obvious errors?
Any help would be much appreciated
Here is the html of the form:
<div id="formContainer">
<form action="form.php" method="post" id="contactForm">
<fieldset>
<legend>Your details</legend>
<label for="name">Name *</label>
<input type="text" id="name">
<label for="email">Email *</label>
<input type="email" id="email">
<label for="tel">Telephone</label>
<input type="tel" id="tel">
</fieldset>
<fieldset>
<legend>Tutoring</legend>
<label for="type">Type of lesson</label>
<select name="type" id="type">
<option>Individual</option>
<option>Group</option>
</select>
<label for="subject">Subject</label>
<input name="subject" list="subjects" id="subject">
<datalist id="subjects">
<option>English</option>
<option>Biology</option>
<option>Geography</option>
</datalist>
<label for="level">Your level</label>
<select name="level" id="level">
<option>Beginner</option>
<option>GCSE</option>
<option>A-Level</option>
<option>University</option>
</select>
<label for="hours">Hours/week</label>
<input type="number" id="hours">
<label for="info">Additional Information</label>
<textarea name="info" id="info" rows="10" cols="6"></textarea>
</fieldset>
<input type="submit" name="submit" value="Send" id="sendButton">
<input type="hidden" name="submit_check" value="1" />
</form>
</div>
And here is the simple php script:
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'onjegolders#gmail.com';
$EmailSubject = 'Site contact form ';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY = "Telephone: ".$_POST["tel"]."<br>";
$MESSAGE_BODY = "Type: ".$_POST["type"]."<br>";
$MESSAGE_BODY = "Subject: ".$_POST["subject"]."<br>";
$MESSAGE_BODY = "Level: ".$_POST["level"]."<br>";
$MESSAGE_BODY = "Hours required: ".$_POST["hours"]."<br>";
$MESSAGE_BODY .= "Additional information: ".nl2br($_POST["info"])."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<html>
<h3>Thanks for your email</h3>
<h4>I'll get back to you as soon as possible</h4>
<p>Click here to go back to previous page</p>
</html>
<?php
} else {
?>
<html>Sorry, this form didn't work</html>
<?php
};
?>

Try with
if ( !empty($_POST["email"]) )
However you can check, what is posted in that page using:
echo '<pre>';
var_dump( $_POST );

Change your form.php as the following
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'onjegolders#gmail.com';
$EmailSubject = 'Site contact form ';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY = "Telephone: ".$_POST["tel"]."<br>";
$MESSAGE_BODY = "Type: ".$_POST["type"]."<br>";
$MESSAGE_BODY = "Subject: ".$_POST["subject"]."<br>";
$MESSAGE_BODY = "Level: ".$_POST["level"]."<br>";
$MESSAGE_BODY = "Hours required: ".$_POST["hours"]."<br>";
$MESSAGE_BODY .= "Additional information: ".nl2br($_POST["info"])."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
echo <<<EXCERPT
<html>
<h3>Thanks for your email</h3>
<h4>I'll get back to you as soon as possible</h4>
<p>Click here to go back to previous page</p>
</html>
EXCERPT;
} else {
echo "<html>Sorry, this form didn't work</html>";
}
?>

in your
if ($_POST["email"] <> '') {
change that into
if ($_POST["email"] != '') {

You should try this:
if(!empty($_POST["email"])){
//your email preparation code
}
Insted of using this:
if($_POST["email" <> ''){
//your email preparation code
}
The ! basically means not, so the !empty means not empty

You could also just use:
if ($_POST["email"]) {
This works much like the != "" or empty() check. PHP was incepted to handle forms well. And you can just let it probe incoming form fields. It uses some magic boolean conversion rules for strings, which most of the time will accomplish what you want.
Another advantage of this simpler style is that it eases debugging if you enable the E_ALL and E_NOTICE (=debug) error_reporting mode.

Related

Php contact form coding issue

I am having trouble with the contact form that I've created for my website. I am not a PHP expert but I thought a proper contact form would be more professional than a simple href mailto link.
I managed to get the email, it tells me the sender but there is no subject and is all just blank without text. Also I keep receiving 2 emails everyday from no sender.
This is what I done in PHP in the page named contact.php.
I hope you can help:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$form1_services = $_POST['form1_services'];
$msg = $_POST['msg'];
$formcontent="From: $name \n Message: $message";
$recipient = "dandrea.alessandro81#gmail.com";
$subject = "Customer Inquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Keep in touch soon!" . " -" . "<a href='index.html' style='text-decoration:none;font-family: 'gooddogregular';color:#009999;'> Return Home</a>";
?>
And this is the actual form in HTML:
<form action="contact.php" method="post">
<fieldset>
<legend></legend>
<div class="controlgroup">
<label for="form1_name">Name *</label>
<input type="text" placeholder="Enter your full name*" name="name" value>
</div>
<div class="controlgroup">
<label for="form1_email">Email *</label>
<input type="text" placeholder="Enter a valid email address*" name="email" value>
</div>
<div class="controlgroup">
<label for="form1_services">Services Required</label>
<select id="form1_services" name="services">
<option value="Website Design"> Website Design (from scratch) </option>
<option value="Resposive Design"> Responsive Design </option>
<option value="Customize a Site"> Customize a Site </option>
<option value="Quotation"> Quotation </option>
</select>
</div>
<div class="controlgroup">
<label>Project Info*</label>
<textarea placeholder="Ciao Alessandro, I am contacting you because...*" id="msg" name="msg" required aria-required="true"></textarea>
</div>
<input type="submit" name="submit_btn" id="send" value="Hit me up!" class="wow rubberBand animated" data-wow-delay="2s">
</fieldset>
</form>
Thanks a lot in advance.
Alessandro
Taking a look at your PHP, you're collecting a number of fields, and then not actually using them. Try this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$form1_services = $_POST['services'];
$msg = $_POST['msg'];
$message = 'From: ' . $name . ' <' . $email . '>' . "\n";
$message .= 'Service: ' . $form1_services . "\n";
$message .= 'Message: ' . "\n";
$message .= $msg;
$recipient = "dandrea.alessandro81#gmail.com";
$subject = "Customer Inquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $message, $mailheader) or die("Error!");
echo "Thank You! Keep in touch soon!" . " -" . "<a href='index.html' style='text-decoration:none;font-family: 'gooddogregular';color:#009999;'> Return Home</a>";
Keep in mind that at this point, you're not actually validating any of this information, so you can't be sure that the email address or the name is actually valid at all, but this should at least show you what is getting posted.
Try adding some headers:
$mailheader = "From: $email \r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Unable to send mails with php mail function [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$contact = $_POST['num'];
$email = $_POST['email'];
$message = $_POST['message'];
$ToEmail = 'info#kesems.com';
$EmailSubject = 'School Enquiry';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
header('Location:contact-us.php');
}
?>
contact-us.php
<form role="form" action="contact-us.php" id="main-contact-form" class="contact-form" name="contact-form" method="post">
<div class="row ml0">
<div class="form-group">
<input type="text" class="form-control" name="name" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="text" class="form-control" name="num" required="required" placeholder="Contact number">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" required="required" placeholder="Email address">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" name="message" class="form-control" rows="3" placeholder="Any Queries/suggestions" style="resize:none"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Send Message" class="btn btn-primary btn-lg"/>
</div>
</div>
</form>
Simple script that sends email headers...still not working.
is code is correct?
any particular solution for this?
any particular solution for this?
thank you in advance.
Try this it worked for me
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: yourmail#gmail.com" . "\r\n" .
"Reply-To: no-reply#gmail.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$to = "tomail#gmail.com";
$subject = "This is subject";
$from = "yourmail#gmail.com";//optional
$message = ' this is my message hello';
if (mail($to, $subject, $message, $headers, 'ADMIN')) {
echo "mail sent"
}
else{
echo "error cannot send mail";
}
Check whether all the arguments are actually set (using ternary), just because the submit is set does not mean that all the arguments are set:
$name = isset($_POST['name']) ? $_POST['name'] : "";
$contact = isset($_POST['num']) ? $_POST['num'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$message = isset($_POST['message']) ? $_POST['message'] : "";
Check these values and make a condition that you display some error if something is not set.
If you're certain the values are set then you need to change
$ToEmail = 'info#example.com';
to
$ToEmail = $email;
Otherwise nothing's going to happen.
Finally, I recommend identifying your html inputs with a proper id, instead of a name.
Unrelated to the error but yet important to you
Like already pointed out by #Fred -ii-, a proper concatenation (.=) is required instead of overwriting (=) in the second header assignment of $MESSAGE_BODY:
MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
Should be:
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";

Sending mail directly from a form

I have an Contact us page on my website. what i want is when someone fills the form and click on send button. The message should be arrived to my gmail. i wrote the following code for it. its not working. is there any other way i can accomplish the same.
Html code:
<form id="ContactForm" action="contacts.php" method="post">
<div>
<div class="wrapper"> <strong>Name:</strong>
<div class="bg">
<input type="text" class="input" name="name">
</div>
</div>
<div class="wrapper"> <strong>Email:</strong>
<div class="bg">
<input type="text" class="input" name="email">
</div>
</div>
<div class="textarea_box"> <strong>Message:</strong>
<div class="bg">
<textarea cols="1" rows="1" name="message"></textarea>
</div>
</div>
<span>Send</span> <span>Clear</span> </div>
</form>
php code
<?php
session_start();
$to = "someemail#gmail.com";
$subject = "Someone Tried to contact you";
$message = $_POST['message'];
$fromemail = $_POST['email'];
$fromname = $_POST['name'];
$lt= '<';
$gt= '>';
$sp= ' ';
$from= 'From:';
$headers = $from.$fromname.$sp.$lt.$fromemail.$gt;
mail($to,$subject,$message,$headers);
echo "mail sent";
exit();
?>
Firstly, you should check your inputs for PHP injection.
$message = stripslashes($_POST['message']);
$fromemail = stripslashes($_POST['email']);
$fromname = stripslashes($_POST['name']);
Apart from that, there doesn't seem to be anything wrong with your mail script. The problem is most likely caused from your PHP server. Does your web hosting definitely provide PHP mail? Most free web hosts do not provide this as they are often used for spamming.
Sorry, but your code is crappy (especially, those concatenations). Use Swift mailer which provides OOP-style and does all the header job for you. And make sure you've got any mail server installed (did you check if you have any?).
PHP form:
<?php
header( 'Content-Type: text/html; charset=utf-8' );
// Your Email
$receiver = 'max.mustermann#domain.tld';
if (isset($_POST['send']))
{
$name = $_POST['name']
$email = $_POST['email'];
if ((strlen( $_POST['subject'] ) < 5) || (strlen( $_POST['message'] ) < 5))
{
die( 'Please fill in all fields!' );
}
else
{
$subject = $_POST['subject'];
$message = $_POST['message'];
}
$mailheader = "From: Your Site <noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "Reply-To: " .$name. "<" .$email. ">\r\n";
$mailheader .= "Return-Path: noreply#" .$_SERVER['SERVER_NAME']. "\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/plain; charset=UTF-8\r\n";
$mailheader .= "Content-Transfer-Encoding: 7bit\r\n";
$mailheader .= "Message-ID: <" .time(). " noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n\r\n";
if (#mail( $receiver, htmlspecialchars( $subject ), $message, $mailheader ))
{
echo 'Email send!';
}
}
?>
HTML form:
<form action="mail.php" method="post">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Message: <textarea name="message" cols="20" rows="2"></textarea><br />
<input name="send" type="submit" value="Send Email" />
</form>

PHP form not submitting entries to email. I'm missing something

Updated: I have a simple PHP form that should submit data to an email address but it isn't sending it. it's just sending the field names.
Here's the code:
<?php
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".nl2br($_POST["phone"])."\r\n";
$MESSAGE_BODY .= "Email: ".nl2br($_POST["email"])."\r\n";
$MESSAGE_BODY .= "Opt in: ".nl2br($_POST["send"])."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<form class="form-signin" name="index" action="index.php" method="post">
<img src="img/coffee.png" width="235" height="227">
<h2 class="form-signin-heading">Enter your information</h2>
<input name="name" type="text" class="input-block-level" id="name" placeholder="Name">
<input type="text" class="input-block-level" name="surname" id="surname" placeholder="Surname">
<input type="text" class="input-block-level" name="designation" id="designation" placeholder="Designation">
<input type="text" class="input-block-level" name="phone" id="phone" placeholder="Cell Number">
<input type="text" class="input-block-level" name="email" id="email" placeholder="Email">
<label class="checkbox">
<input type="checkbox" value="Send-me-helpful-information" name="send" checked> <p>Send me helpful information</p>
<input name="submit" type="submit" value="submit" class="btn btn-large btn-primary" id="go" rel="leanModal" href="#thankyou">
<p>Terms & Conditions Apply.
Click HERE</p>
</form>
Please help
Comment: It's sending now but submits a blank email on page load. Does anyone know a fix for this?
Change your submit button to this:
<input type="submit" value="Submit" />
Your PHP should look more like
if(!empty($_POST)) { // This is to say if the form is submitted.
// All your PHP post stuff here. The email sending stuff.
$errors = array();
// Do this for all your required fields.
if(isset($_POST['email']) && $_POST['email'] != '') { // If there's a variable set and it isn't empty.
$mailheader = "From: " . $_POST['email'] . "\r\n";
} else {
$errors[] = "No email submitted";
}
print_r($_POST); // This is for debugging. Remove it when satisfied.
echo $MESSAGE_BODY; // Also for debugging. Ensure this is fine.
if(empty($errors)) {
if(mail()) { // Your mail function
// Successful mail send, either redirect or do whatever your do for successful send
} else {
// Mail failure, inform user the sending failed. Handle it as you wish. Errors will be sent out.
}
} else {
print_r($errors); // See what's going error wise.
}
}
<form name="form1" id="form1" action="" method="post"> // form like this
<input type="submit" name="submit" id="submit" value="submit"> // submit button like this
</form>
<?php if(isset($_REQUEST['submit']))
{
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
} ?>
<?php
if(isset($_POST['submit'])){
//your sending email php code
}
?>
function mail()
uses SMTP Server so make sure it is perfectly configured and installed if you are using it on your local server such as WAMP or ZAMP.
rest if it still not working put your php code in between:
<?php
if(isset($_POST['submit']))
{
// ur php code here
}
?>
Since your form is posting some values, you need to have an <input type="submit"> to post those. name is just an indentifier, if you will call it "submit", it won't work.
Try adding <input type="submit" value="Whatever you want your text to be"> instead of <button name="submit" type="submit" class="btn btn-large btn-primary"><a id="go" rel="leanModal" href="#thankyou">Submit</a></button>.
If you have another problem, please explain it more. What errors do you get?
Try adding some echo to see where your program stops working.
Try this
<?php
if($_POST['submit']!="")
{
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
}
?>

html showing in php mail

I'm trying to send a php mail but it seems that I have a error in my foreach because the mail shows html..
This is my code:
<form method="post">
<fieldset>
<legend>Contact Form</legend>
<label for="fullname">Votre Nom :
<input id="fullname" name="fullname" type="text" value="nelson" />
</label>
<label for="emailaddress" class="margin">Votre e-mail:
<input id="email" name="email" type="text" value="" />
</label>
<label for="message">Message:<br />
<textarea id="message" name="message" cols="40" rows="8"></textarea>
</label>
<p>
<input id="submit-button" class="button gray stripe" type="submit" name="submit" value="Envoyer le message" />
</p>
</fieldset>
</form>
<?php
foreach ($_POST as $value) {
$value = strip_tags($value);
$value = htmlspecialchars($value);
}
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
if (isset($_POST['submit'])) {
mail($to, $suject, $message);
echo"mail had been sent";
}
?>
Can anyone help me please
You need to set the Content-type header in your email message:
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (isset($_POST['submit'])) {
mail($to, $suject, $message, $headers);
echo"mail had been sent";
Your message body also needs to be contained in <html> tags.
Your foreach is kind of useless, just do that. More fast than a loops
$name = strip_tags(htmlspecialchars($_POST["fullname"]));
$email = "email:" .strip_tags(htmlspecialchars($_POST["email"]));
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .strip_tags(htmlspecialchars($_POST["message";));
To send email containing HTML you must set the header so that the email client knows that the email contains HTML. You also have to make the body of your email an HTML document.
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
mail($to, $suject, $message, $header);
And then in the message itself:
<html>
<head></head>
<body>
Content here
</body>
</html>

Categories