Form to Email PHP Script
Oct 15, 10:06 PM By Eoghan O'Brien.I have been asked by a number of PHP beginners for a script to send an email from a form so I decided to write a simple script to point people to instead of writing it out every time. Here is my Form to E-mail script for your viewing pleasure…....
Firstly, your html page lets say contact.html
Html Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Contact Page</title>
</head>
<body>
<form method="post" action="sendform.php">
<label for="name">Name:</label>
<input type="text" name="name" /><br />
<label for="email">E-Mail:</label>
<input type="text" name="email" /><br />
<label for="comments">Comments:</label>
<textarea rows="4" cols="20" name="comments">
</textarea>
<input type="submit" name="sendinfo" value="Send" />
</form>
</body>
</html>
Next, you make your sendform.php page
PHP Code:
<?php
$name=stripslashes(trim($_POST['name']));
$email=stripslashes(trim($_POST['email']));
$comments=stripslashes(trim($_POST['comments']));
if (empty($name)) {
echo "Please enter your name";
}
if (empty($email)) {
echo "Please enter your email";
}
if (empty($comments)) {
echo "Please enter your comments";
}
if ($name && $email && $comments) {
$msg="Feedback from My Site\n\n";
$msg.="Name: $name\n";
$msg.="E-mail: $email\n";
$msg.="Comments: $comments";
$to="addreess@domain.com";
$subject="Site Feedback from $name";
$mailheaders="From: $email";
mail($to, $subject, $msg, $mailheaders);
echo "<p>Thank you $name, Your information has been sent!</p>";
}
?>