PHP is a server-side embedded scripting language used to create dynamic web pages.
In a HTML page, PHP code is enclosed within special PHP tags. When a visitor opens the page, the server processes the PHP code and then sends the output in HTML to the visitor's browser.
Simple PHP email script
First, make the form page email.html or whatever name you want.
you can embed this form in any page.
<head><title>Mail sender</title></head>
<body>
<form method="post" action="email.php">
Name:<br><input name="name" type="text" size="30"><br><br>
Email:<br><input name="email" type="text" size="30"><br><br>
Comments:<br><input name="subject" type="text" size="30"><br><br>
Message:<br><textarea name="msg" cols="25" rows="6"></textarea><br><br>
<input type="reset" value="Reset"> <input type="submit" value="Send">
</form>
</body>
</html> <html>
The form contains the necessary text fields Email, Subject, Message, and the Send button. The line
<form action="email.php" method="POST">
tells the browser to submit the data to the mail.php page for processing.
The email.php page has the following code in it.
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
$to = "you@where-ever.com"; // change to your email address
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];
$d = date('l dS \of F Y h:i:s A');
$sub = "form to mail";
$headers = "From: $name <$email>\n";
$headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
$mes = "Subject: ".$subject."\n";
$mes .= "Message: ".$msg."\n" ;
$mes .= "Name: ".$name."\n";
$mes .= 'Email: '.$email."\n";
$mes .= 'Date & Time: '.$d;
if (empty($name) || empty($email) || empty($subject) || empty($msg))
{
echo " <h3>Sorry all fields are required.</h3>";
}
elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
print " <h3>Sorry the email address you entered is not valid.</h3>";
}
else
{
mail($to, $sub, $mes, $headers);
print " Thank you for your email, we will get back to you as soon as possible";
}
?>
</body>
</html>
You can use this script to create a Contact Us page on your website. Your visitors will be able to send you a message, but your email address won't be shown on the page and spam bots, that search pages looking for email addresses, won't find it.
Just remove
you@where-ever.com field from the form and replace it with your email.