PHP issues with mail() [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
This is my first time working in php and I'm running into issues getting mail() to work. I am using MAMP Pro to test (and normally there is a real email address to send to). When I click the send button it clears the form but does not show the thank you message (it also isn't showing the two dividers on the bottom that are just stripes) and I have yet to receive a successful email. Any help is appreciated!
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!--[if IE]><meta http-equiv="cleartype" content="on" /><![endif]-->
<title>Twin City Grill</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="styles/normalize.css">
<link rel='stylesheet' type="text/css" href="styles/main.css">
</head>
<body>
<nav class="navbar">
<a href="index.php">
<img class="logo" src="assets/Twin-City-Grill-white.png" alt="Twin City Grill, white blocky serif font">
<h1 class="screen-reader-only">Twin City Grill</h1>
</a>
<div class="links-container">
Contact
</div>
</nav>
<div class="wrapper">
<div class="divider"></div>
<div class="divider"></div>
<div class="contact-form">
<?php
$action=$_REQUEST['action'];
if ($action=="")
{
?>
<form action="" method="POST" enctype="multipart/form-data" role="form" id="contact-form">
<input type="hidden" name="action" value="submit">
<h2>Contact Us</h2>
<p class="form-instructions"><em>All fields are required.</em></p>
<div class="form-group">
<label for="name" class="screen-reader-only">Name:</label>
<input type="text" name="name" id="name" placeholder="Name" required="required">
</div>
<div class="form-group">
<label for="phone" class="screen-reader-only">Phone Number:</label>
<input type="tel" name="phone" id="phone" placeholder="Phone Number" required="required">
</div>
<div class="form-group">
<label for="email" class="screen-reader-only">Email Address:</label>
<input type="email" name="email" id="email" placeholder="Email Address" required="required">
</div>
<div class="form-group">
<label for="message" class="screen-reader-only">Message:</label>
<textarea name="message" id="message" cols="30" rows="10" placeholder="Your Message" required="required"></textarea>
</div>
<div class="form-group">
<input id="submit" name="submit" type="submit" value="Send" >
</div>
</form>
<?php
}
else
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message=="")||($phone==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
mail("testemail#gmail.com", $subject, $email_message, $from);
?>
<h2>Thanks!</h2>
<p>We will get back to you as soon as possible. Have a great day!</p>
<?php
}
}
?>
</div>
<div class="divider"></div>
<div class="divider"></div>
</div>
<footer>
</footer>
</body>
</html>

It looks like the mail() function is failing silently for whatever reason. try something like this to get the actual error:
$success = mail("testemail#gmail.com", $subject, $email_message, $from);
if (!$success) {
echo error_get_last()['message'];
}

Instead of $from variable, try this
$from = 'From: from#example.com' . "\r\n" .
'Reply-To: from#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Also check your spam

Related

HTML Form Sent to Email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I'm having an issue with my html form sending to my email.
I have it set up so that it goes to a php file and it works fine, but when I change the filename from index.php to contact.php it doesn't send anymore.
contact.php:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style/stylesheet.css">
<script src="script/scripts.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<div class="banner">
<img src="images/banner.jpg">
</div>
<div class="navigation">
<div class="navbar">
Home
News
<div class="dropdown">
<button class="dropbtn">Parts
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Cases
Motherboards
Processors
Graphics Cards
Storage
Power Supplies
RAM
Other
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Builds
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Placeholder
Placeholder
Placeholder
Placeholder
Placeholder
Placeholder
Placeholder
Placeholder
</div>
</div>
<div class="contact" id="navright">
Contact
</div>
</div>
</div>
<div class="contact_page">
<form class="contact_form" action="contactform.php" method="post">
<label>Full Name:</label><br/>
<input type="text" name="name" placeholder="Full Name"><br/>
<label>Your Email:</label><br/>
<input type="text" name="mail" placeholder="Email"><br/>
<label>Subject:</label><br/>
<input type="text" name="subject" placeholder="Subject"><br/>
<label>Message:</label><br/>
<textarea name="message" class="contacttext" placeholder="Message..."></textarea><br/>
<button class="submit" type="submit">Submit</button>
</form>
</div>
<div class="footer">
<div class="footertext">
<p>Here at Terry's Computers we do not claim to own any of
the products showed on this website.</p>
<p>Contact Us</p>
</div>
</div>
</body>
</html>
contactform.php:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "terryjtowell#terrytowell.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an Email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.php?mailsend");
}
?>
It's not my web hoster because it worked with this file.
index.php:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<meta charset="UTF-8">
<title>Email Form</title>
</head>
<body>
<main>
<p class="header">E-MAIL FORM</p>
<form class="contact-form" action="contactform.php" method="post">
<p class="title">Your Name</p>
<input type="text" name="name" placeholer="Full Name"><br/>
<p class="title">Your E-Mail</p>
<input type="text" name="mail" placeholer="Your E-mail"><br/>
<p class="title">Subject</p>
<input type="text" name="subject" placeholer="Subject"><br/>
<p class="title">Message</p>
<textarea name="message" maxrows="10" placeholder="Message"></textarea><br/>
<button type="submit" name="submit"><h2>SUBMIT</h2></button><br/>
</form>
</main>
</body>
</html>
Your PHP line:
if (isset($_POST['submit'])) {
Will never execute because you need an element with the name of submit, but you have:
<button class="submit" type="submit">Submit</button>
Which you have in the example you say works. The solution should be as simple as adding a name attribute like this:
<button class="submit" name="submit" type="submit">Submit</button>

Bootstrap form to email doesn't send [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I found a Bootstrap form on the internet and now I want to send it to my email.
I have the form online, so no issues with localhost etc. When I try send it I get a message 'Thank You! I will be in touch'. So it sends? But I don't receive the email in my mailbox. Anyone an idea? See code below.
ps. When I run the code local he got some errors, see image. I don't have those errors online, but maybe that is the reason why it doesn't send anything?
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#example.com';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
<meta name="author" content="BootstrapBay.com">
<title>Bootstrap Contact Form With PHP Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="mail.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
Use this modified code to get rid of the initial localhost errors.
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email']; //Sender's email
$message = $_POST['message'];
$human = intval($_POST['human']);
$to = "receivers#email.address";
$subject = "Message from Contact Demo";
$body = "From: $name\n";
$body .= "E-Mail: $email\n";
$body .= "Message:\n $message";
$header = "From:" . $email . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body, $header)) {
$result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
<meta name="author" content="BootstrapBay.com">
<title>Bootstrap Contact Form With PHP Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
<?php if (isset($errName)) echo "<p class='text-danger'>$errName</p>" ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
<?php if (isset($errEmail)) echo "<p class='text-danger'>$errEmail</p>" ?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
<?php if (isset($errMessage)) echo "<p class='text-danger'>$errMessage</p>" ?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php if (isset($errHuman)) echo "<p class='text-danger'>$errHuman</p>" ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php if (isset($result)) echo $result ?>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
PHP isset() will determine if the variable is set and is not NULL. Initially the variables are NULL so it will omit showing you the errors.
Note: most of the time email will be delivered to your email address. check spam/junk folders to see the email. This happens due to unverified source of the email server.
Edit 1
Combining PHPMailer with mailgun platform will ensure you will get your emails to the destination address and possibly avoid going to spam/junk email folder.
Edit 2
I've update my answer with the recent modifications. Please have a look. Tested on a live server. received the contact demo email to my spam/junk folder on Outlook, Straightly to my inbox on Gmail.
Screens
Form submit Outlook Gmail
Hope you will have good luck!
if you want the code ready without change it you only need to change your php.ini file and change the line relate to the error reporting.
search for something like
error_reporting = E_ALL
and change to error_reporting = E_ALL & ~E_NOTICE
with this you hide the undefine issue
and then you need to configure your smtp provider in your php.ini too

Alerts for Contact Form after being sent / error found

I myself am not good with PHP so I am wondering If someone on here can help me with my issue. My main page is the following code
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Leave a Message</title>
<link href="bootstrap.min.css" rel="stylesheet">
<link href="font-awesome.min.css" rel="stylesheet">
<link href="custom.css" rel="stylesheet">
</head>
<body id="particles">
<body id="particles">
<div class="main">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title">Leave a message</h4>
</div>
<div class="modal-body">
<form method="post" action="contact_me.php">
<div class="form-group">
<label>Your name</label>
<br>
<input type="text" class-"form-control" name="name" required="" style="width: 100%;">
</div>
<div class="form-group">
<label>Your email</label>
<br>
<input type="text" class-"form-control" name="email" required="" style="width: 100%;">
</div>
<div class="form-group">
<label>Your message</label>
<br>
<textarea type="text" class-"form-control" name="message" required="" style="width: 100%; max-width: 100%; min-height: 100px"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-info" style="width: 100%;">Submit</button>
</div>
</form>
</div>
<div class="modal-footer" style="text-align: center;">
<small>Copyright © 2016. All Rights Reserved.</small>
</div>
</div>
</div>
</div>
<script src="jquery.min.js"></script>
<script src="particles.jquery.min.js"></script>
<script src="custom.min.js"></script>
</body>
</html>
My action is contact_me.php and that code is
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
header( 'Location: http://www.wotm8.net/error-pages/contact' ) ;
return false;
} header('Location: ' . $_SERVER['HTTP_REFERER']);
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = 'me#lolnick.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
I am wondering If someone can teach me how to add alerts. For example If a error Is found It will show up a Red Box of Text and say Error. If the contact form is correct than It will show a Green Box of Text and say Contact Form Sent.

php code appearing in form fields - bootstrap email form

I have an HTML file and a PHP file here. The form is to send an email. What is happening is that bits of "code" are appearing in the form fields. Other than that, it does work and sends an email. Once the code has been deleted from the fields and the correct info entered instead. The code is from a bootstrap email form tutorial.
Click here for a Screenshot of what I am describing
Thanks guys!!
--HTML FILE--
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
<meta name="author" content="BootstrapBay.com">
<title>Bootstrap Contact Form With PHP Example</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</body>
</html>
-- PHP FILE --
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#domain.com';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
The thing is with your script, is that if you have it in two seperate files, it won't work.
(Not so fast Fred)... It will still work, but it won't work as intended in order to show you the error message(s) if one or more fields have not been filled, being the whole purpose with your code.
It needs to be inside a single file (PHP first, then your HTML form) sidenote: the order isn't important; the choice is yours. Plus, you will also need to modify your code so that it contains a valid From:. More on this below.
This, on top of what I already said about it must be a .php extension.
Therefore you will need to change this line:
$from = 'Demo Contact Form';
to:
$from = "From: ". $name . " <" . $email . ">\r\n";
in order to show the person's name in the Email's from, but at the same time having an Email address as the "From:"
Otherwise, it will look as if it were coming from your own server, rather than the person's Email.
(I've tested for myself)
For more information on the mail() function, visit PHP.net:
http://php.net/manual/en/function.mail.php

PHP form submission opens empty page

I copied the code for a form to my website. It doesn't submit... Can someone see the problem?
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "danielko#intrahouse.co.il";
$email_subject = "פנייה מהאתר";
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 " מלא את כל הפרטים בבקשה.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died(' אחד הפרטים חסר. מלא את כל הפרטים בבקשה');
}
$first_name = $_POST['first_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 .= 'נא מלא פרטים נכונים.<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($first_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);
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" dir"=rtl">
<meta name="keywords" content="פורום בית חכם,בית חכם, חשמל חכם, בקרת חשמל, ניהול חשמל, חסכון בחשמל, אמצעי בקרת חשמל, כמה עולה בית חכם, מחירי חשמל חכם, מערכות בית חכם, תכנון בית חכם">
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="contact_style.css" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.uniform.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(e){
$("input:checkbox, input:radio").uniform();
$('#top').css('float','none');
$('.content_window').css('height','auto');
});
</script>
<title>IntraHouse - צור קשר</title>
</head>
<body>
<div class="background">
<div class="content_window">
<header>
<div class="languages">
English
עברית
</div>
<div id="app_theme" style="margin-top:3px" ></div>
<div id="top" style="margin-top:2px" style="float:none" >
<nav id="topmenu">
<ul>
<li id="home">דף הבית </li>
<li id="about">אודות </li>
<li id="prices">מחירים</li>
<li id="projects">פרוייקטים</li>
<li id="store">חנות</li>
<li id="contact_us">צור קשר</li>
</ul>
</nav>
</div>
</header>
<div class="contact" style="margin-top:10px">
<article dir="rtl">
<h1 style="margin-top:10px"> נשמע מעניין? השאירו פרטים ונחזור אליכם</h1>
<form>
<ul>
<li>
<label for="name"> שם: </label>
<input type="text" size="40" id="name" />
</li>
<li>
<label for="telephone"> טלפון: </label>
<input type="text" size="40" id="name" />
</li>
<li>
<label for="email"> כתובת אימייל: </label>
<input type="email" size="40" id="email" />
</li>
</li>
<li>
<label for="message">תוכן ההודעה:</label>
<textarea cols="50" rows="5" id="message"></textarea>
</li>
</ul>
<p>
<button type="reset" class="right"> אפס </button>
<button type="submit" class="action"> שלח </button>
</p>
</form>
</article>
</div>
</div>
</body>
</html>
<?php
}
?>
HTML appears at the end, as you can see. All buttons work and email # check work, but the form submmision just opens an empty page.
Your form has no method or action I believe this doesn't work in some browsers.
Change
<form>
To:
<form method="post" action="[the url of the page]">
The default method for a form is GET so if you don't specify it in the form tag, you would need $_GET instead of $_POST.
You also need an action attibute, so your tag should look like:
<form action="" method="post">
Then you would also need to add name attributes to all your form fields as that is what gets sent to the server, not the id.
Here are two crucial problems. The id of an HTML element needs to be unique, so here you have two elements with the same id:
<li>
<label for="name"> שם: </label>
<input type="text" size="40" id="name" />
</li>
<li>
<label for="telephone"> טלפון: </label>
<input type="text" size="40" id="name" />
</li>
This needs to change, and also each element needs a name attribute, or it will not appear in the POST array properly:
<li>
<label for="name"> שם: </label>
<input type="text" size="40" id="name" name="name" />
</li>
<li>
<label for="telephone"> טלפון: </label>
<input type="text" size="40" id="telephone" name="telephone" />
</li>
Etc. - all element must have a name and a unique id. In particular, your email field needs name="email" so that your if(isset($_POST['email'])) check at the beginning of the script will work. Also, the form needs the method POST in order for those elements to pass into the POST array. If you simply omit the action, the form will submit to the same page:
<form method="POST">
your <form> should have method="POST" attribute
like this :
<form method="POST">
You have errors :
<input type="email" size="40" id="email" />
it should be
<input type="text" size="40" id="email" name="email" />
See, you have $_POST['first_name'] in your code, so you must have <input type="text" name="first_name" /> , a name attribute is a must.
in order to get the value of an input field in php you have to use name attribute

Categories