Contact Form, how does it work? - php

I'm right about to launch my site, but first I need to finish my contact form, to make sure users can contact me. I'm not an expert in PHP or anything close, so please to understand my ignorance.
I have my server ready and running. I have an email. I have the site. I have two documents, one contact.html and the other send.php.
My contact.html has the following content:
<!doctype html>
<html>
<head>
<title>Spotnight - About</title>
<link rel="stylesheet" href="style.css">
<link rel="Favicon" href="f.ico" type="image/x-icon">
</head>
<body>
<div class="ham_container">
<svg id="hamburger" title="Menu">
<path d="M0,5 30,5" stroke="#eeeeee" stroke-width="5"/>
<path d="M0,14 30,14" stroke="#eeeeee" stroke-width="5"/>
<path d="M0,23 30,23" stroke="#eeeeee" stroke-width="5"/>
</svg>
</div>
<nav>
<ul>
<li>Our Home</li>
<li>About Us</li>
<li>Hybon Sites</li>
<li>Orion Interactives</li>
<li>Spotnight Studios</li>
<li>Spotnight Shop</li>
<br>
<li>Contact Us</li>
</ul>
</nav>
<main>
<h1>Contact us! :)</h1>
<h5>Spotnight</h5>
<div>
<form action="send.php" method="POST">
<input type="text" name="first_name" placeholder="Your first name, eg: 'John'">
<input type="text" name="last_name" placeholder="Your last name, eg: 'Smith'">
<input type="email" name="email" placeholder="Your Email, eg: 'yourname#example.com'">
<select name="subj" size="1">
<option value="new_website">I need a website</option>
<option value="new_game">I want a game</option>
<option value="new_employee">I want to be part of the team</option>
<option value="new_employee_game">I want to participate making games</option>
<option value="new_employee_site">I want to participate building sites</option>
<option value="new_artist">I'm an artist and need help</option>
<option value="something_else">Something else!</option>
</select>
<textarea name="message" placeholder="Your Message Goes Here!"></textarea>
<input type="submit" value="Send!">
</form>
</div>
</main>
<footer>
© <span id="year">2015</span> : Spotnight.io : Proudly created by Hybon.
</footer>
</body>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="script.js"></script>
</html>
My send.php document has the following content:
<!doctype html>
<html>
<head>
</head>
<body>
<?php
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$email = $_POST["email"];
$subj = $_POST["subj"];
$message = $_POST["message"];
$formcontent="From: " . $first_name . $last_name . ", " . $email . "\n Subject: " . $subj . "\n Message: " . $message;
$recipient = "julian-avar#spotnight.io";
$subject = "Contact Form - " . $subj . " - Spotnight.io";
$mailheader = $email . " has tryed to reach you! \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Your message has been recived, we&apos;ll answer as soon as possible!" . $first_name . $last_name . " -" . "Go Back!";
?>
</body>
</html>
This is all literal, meaning, the code is exactly as right there. I'm having trouble understanding it, and I still don't understand how it works.
So if you are a super duper awesome php expert, please help me understand and fix this code so I can deal with it later, and so other people understand how it works.
I have searched many places, but the simplest form of contact form doesn't seen to appear in the world archives. Thank you very much!

echo "Thank You! Your message has been recived, we&apos;ll answer as soon as possible!" . $first_name . $last_name . " -" . "Go Back!";
Here's the problem. The SO's code highliter highlights it badly because of it.
Use this instead:
echo /* part of code missing */ $first_name . $last_name . " -" . "<a href='contact.html'>Go Back!</a>";
// mind the ' signs here ^^^^^^^^^^^^^
You cannot include a quote in another quote without escaping the "'s or using a single-quote (I might have been mistaken in terminology) sign.

Your 'send.php' script mixes html with php so unless you are using output buffering you will encounter an error when it tries to send the email due to html content already being sent to the client/browser. I would suggest that the send.php script is pure php only. Also, the mailheader variable - this looks wrong to me - I think it should contain, at the very least, the From: email#domain.com field. From the PHP manual:
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
<?php
/* send.php */
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$email = $_POST["email"];
$subj = $_POST["subj"];
$message = $_POST["message"];
$formcontent="From: " . $first_name . $last_name . ", " . $email . "\n Subject: " . $subj . "\n Message: " . $message;
$recipient = "julian-avar#spotnight.io";
$subject = "Contact Form - " . $subj . " - Spotnight.io";
$mailheader = "From: ".$email . "\r\n";
$res = mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
if( $res ) header( 'location:/index.php?mailsent=true' );
else header( 'location:/index.php?mailsent=false' );
?>

Related

PHP mail not showing senders email in from header frfom HTML form

Please could someone assist me in showing the senders email in the from header on the email. Please see code below, currently when i receive the email it shows the to email address in the from and the to.
HTML:
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP:
<?php
if(isset($_POST['submit'])){
$to = "bestwayhomemaintenance#gmail.com";
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
//$headers = "From:" . $from;
$headers = "From: $to \r\n";
$headers .= "Reply-To: $from \r\n";
//$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
//mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
You can't send from a gmail address unless you're sending through gmail's servers, which essentially means you can't use PHP's mail() function to do it. You may be able to try, but your messages will be marked as forgeries.
To set the envelope sender with the mail function, you need to use a -f parameter in the $additional_params parameter in the mail function.
Your script is vulnerable to header injection attacks, and it is also exploitable for cross-site scripting.
To avoid the forgery issue, I recommend sending directly through gmail, which mean you need to use SMTP, and the easiest way to do that is to use PHPMailer that you tagged this question with. Base your code on the examples provided with it.

Simple contact form not sending mail with PHP mail() function [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
HTML:
<form action="php/send-contact.php" class="contact-form" name="contact-form" method="post">
<div class="row">
<div class="col-sm-6 cols">
<input type="text" name="name" required="required" placeholder="Name*">
</div>
<div class="col-sm-6 cols">
<input type="email" name="email" required="required" placeholder="Email*">
</div>
<div class="col-sm-6 cols">
<input type="text" name="subject" required="required" placeholder="Subject*">
</div>
<div class="col-sm-12 cols">
<textarea name="message" required="required" cols="30" rows="5" placeholder="Message*"></textarea>
</div>
<div class="col-sm-12 cols">
<input type="submit" name="submit" value="Send Message" class="btn btn-send">
</div>
</div>
</form>
php/send-contact.php:
<?php
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'hello#domain.co.uk';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $body, 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<script>
alert("Thank you for getting in touch. We will contact you as soon as possible.");
</script>
<meta HTTP-EQUIV="REFRESH" content="0; url=../index.html">
</head>
The HTML alert activates and refreshes as it should, but it doesnt send any email...
I have tried numerous email address recipients.
Also, are there any special measures I should take into account (regarding the PHP elements) when adding Google ReCaptcha to this form?
So I have tested this, and I think it is doing what you want.
$name = htmlentities($_POST['name']);
$email_from = htmlentities($_POST['email']);
$subject = htmlentities($_POST['subject']);
$message = htmlentities($_POST['message']);
$email_to = 'Admin#Domain.com';//replace with your email
$headers = "From: webmaster#example.com" . "\r\n" . "CC: ". $email_from; //This adds a from field, as well as CC's the person submitting the request.
//Build the body of the eamil
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email_from . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'message: ' . $message;
$success = mail($email_to, "Contact from site X, regarding: ".$subject, $body,$headers);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<?php if($success){ //If the email was sent correctly?>
<script>
alert("Thank you for getting in touch. We will contact you as soon as possible.");
</script>
<?php header('Location: ../index.html'); }else{?>
<script>
alert("There was an error when sending the email, please try again later.");
</script>
<?php header('Location: ../index.html'); } //If the email falied?>
</head>
The other file (the html) remains the same. Simply replace your code in php/send-contact.php with this.

Simple PHP mail form not working on server

I've written an simple PHP mail form but it refuses to run on one server but will on another. Both servers are running PHP 5.4 and other PHP progs run on the problematic server but mail will not send.
I've pared the mailer back to the simplest code but am stumped as why one server wont send the mail. Is there a configuration I need to activate or a test I can perform to diagnose the problem?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Simple PHP Mailer</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="sender_email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="subdata" value="Submit">
</form>
<?php
//Taken from: http://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script
if(isset($_POST['subdata'])){
$to = 'myname#gmail.com'; // this is your Email address
$from = $_POST['sender_email']; // this is the sender's Email address
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$subject = "PHP Email Form Test";
$message = $fname . ' ' . $lname . ' (' . $from . ') wrote the following:' . '\n\n' . $_POST['message'];
$headers = 'From: user#gmail.com' . "\r\n" .
'Reply-To: user#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo 'Mail Sent. Thank you ' . $fname . ', we will contact you shortly.';
}else{
echo 'Mail Error';
}
}
?>
</body>
</html>
Any help would be appreciated, many thanks.
Kw

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

PHP Contact Form Not Being Received In Email

I know there is a few of these posts already up, and I read through them, but was unable to find the solution to my problem. The PHP and HTML looks good so I am not exactly sure why I am not receiving any email from the submitted contact form.
Here is the PHP:
<?php
$Fname = $_POST ['Fname'];
$Lname = $_POST ['Lname'];
$email = $_POST ['email'];
$message = $_POST ['message'];
$to = 'myName#mywebsite.com';
$subject = 'Contact From My Website';
$msg = " First Name: $Fname/n" .
"Last Name: $Lname/n" .
"Email: $email/n" .
"Message: $message";
mail ($to, $subject, $msg,"From: " . $Fname . $Lname);
$confirm = "Thank you for contacting us! Please allow 48 hours for a representative to respond. Click <a href='contact.php'>here</a> to return to the previous page.";
?>
And here is the HTML form code:
<form id="form1" name="form1" method="post" action="send.php">
<tr>
<label><td width="160px" class="labels">First Name: </td>
<td class="input"><input type="text" name="Fname" id="Fname"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Last Name: </td>
<td class="input"><input type="text" name="Lname" id="Lname"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Email: </td>
<td class="input"><input type="email" name="email" id="email"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Message: </td>
<td class="input"><textarea name="message" id="message" cols="30" rows="5"></textarea></td>
</label>
</tr>
<tr>
<td class="labels"><input type="submit" name="submit" id="submit" value="Submit"/></td>
</tr>
</form>
Any help would be greatly apreciated
EDIT:
There were many errors.
Improper use of \n, mail() headers and trying to echo a success message at the end.
Here is a tested and working mail handler script.
NOTE: I added an if{isset condition.
Change email#example.com with your own E-mail address.
<?php
if(isset($_POST['submit']))
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#example.com"; // <<< change this to your own E-mail address
$subject = "Contact From My Website";
$msg = "First Name: $Fname\n" . "Last Name: $Lname\n" . "Email: $email\n" . "Message: $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $msg, $headers);
echo "Thank you for contacting us! Please allow 48 hours for a representative to respond. Click <a href='contact.php'>here</a> to return to the previous page.";
?>
Supplement
Please read up on the mail( ) function and its proper use of headers by visiting:
http://php.net/manual/en/function.mail.php
There are plenty of examples on that page.
One major error is your use of /n and all need to be changed to \n if anything.
That alone will ultimately make your handler fail.
"From" should not include the name, instead it should include the from email address.
So, instead of
"From: " . $Fname . $Lname
You should be doing
"From: ". $myEmail
Here's a valid header from the php manual:
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
I guess Puggan and Fred get credit for this answer as-well, since they have posted out the obvious but not yet made it an answer.
You should also make sure that the id of the message labels match to $_POST['msg'], so you should not be using 'msg' in one place and 'message' in another.
FYI: skip the spaces ' ' between the function name and the parameter arguments. i.e.
$_POST ['stuff'];
Add
$headers = "From:" . $Fname . $Lname;
and use this:
mail ($to, $subject, $msg, $headers);

Categories