Sending Email with PHP
A Contact Us page with a HTML form to contact the
webmaster adds a nice personal touch to any website. Users feel
"connected" to the owner and it makes them seems more down-to-earth.
PHP
comes with a handy function called, would you believe it, mail(). Here
you will learn how to use it in it's most simple form to send a basic
email.
Please note that if your server doesn't have PHP installed, this will not work. That means Google-Pages too.
Now, to get started. Instructions are given in the comments.
Save the following as sendEmail.php
<?php
// Did someone click submit?
if($_POST['submit'];)
{
// Get our form variables.
$subject = $_POST['subject'];
$email = $_POST['email'];
$msg = $_POST['comments'];
// Send the email with the mail function.
// Replace example@domain.com with your email address!
if( mail("example@domain.com", "$subject", "$email", "$msg") ){
// We have lift-off!
echo "Email sent successfully!";
} else {
echo "Oops! Mail couldn't be sent.";
}
}
?>
You
can now make a HTML page that lets a user send you an email, by
including the following HTML code in your page. (The following code
goes in between the
<form method="post" action="sendEmail.php">
Subject:
<input type="text" name="subject" size="20">
Email:
<input type="text" name="email" size="20">
Comments:
<textarea cols="20" rows="5" name="comments">
</form>
Ankur Kothari is the Director of the newly developed Lipidity Corporation,
which provides free website design. Ankur has a deep knowledge and
mastery of PHP, and has been programming since he was twelve years old.