Forum Moderators: coopster

Message Too Old, No Replies

Simple Email Form

A simple way I'm sure..?

         

PatrickKerby

8:07 pm on Jun 17, 2004 (gmt 0)

10+ Year Member



Hi, I'm not too familiar with PHP, but I have a client who wants a simple email form.

Is there some quick code that would take care of this easily?
what I'm trying to make is:
- name (required)
- phone number & email (either one needs to be required [i.e, if one isn't filled out, the other is required])
- mailing address (not required)
- submit to "emailaddresshere"

If anyone has some quick fixes it would be much appreciated.. thanks!

jatar_k

11:19 pm on Jun 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assume some type of contact us form

a simple form could be firstname, lastname, email, message

contact.php
<html>
<head><title></title></head>
<body>
<?
if (isset($errormsg)) echo '<p><font color="red">',$errormsg,'</font>';
?>
<p><form name="contactform" method="post" action="sendemail.php">
<p>First Name
<br><input type="text" name="firstname">
<p>Last Name
<br><input type="text" name="lastname">
<p>Email
<br><input type="text" name="theiremail">
<p>Comments
<br><textarea cols="20" rows="10" name="comments"></textarea>
<p><input type="submit" value="Send Comments">
</form>
</body>
</html>

Then a very simple mail script could be

sendemail.php
<?php
$errormsg = "";
if ($_POST['firstname']!= "") $firstname = $_POST['firstname'];
else $errormsg .= "<br>Please enter your first name";
if ($_POST['lastname']!= "") $lastname = $_POST['lastname'];
else $errormsg .= "<br>Please enter your last name";
if ($_POST['theiremail']!= "") $theiremail = $_POST['theiremail'];
else $errormsg .= "<br>Please enter your email";

if ($errormsg!= "") {
include "contact.php";
die();
}

$headers .= "From: $firstname $lastname <$theiremail>\n";
$headers .= "X-Sender: $theiremail\n";
$headers .= "X-Mailer: PHP\n";
$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
$headers .= "Return-Path: $theiremail\n";
$headers .= "Reply-To: $theiremail\n";
$message = "";
$message .= $_POST['comments'];
$subject = "A comment from yoursite.com";

mail("webmaster@yoursite.com", $subject, $message, $headers);
header("Location: thankyou.html");
?>

thankyou.html

<html>
<head><title></title></head>
<body>
<p>Thank you for your submission.
</body>
</html>

All this script does is check to see if the fields are empty, though it doesn't check comments. More validation is probably required.

warning, I wrote this in about 5 mins and didn't try it to see if it works so if anyone sees any errors please correct me.