PHP: Lesson 5 Send Email using a PHP Script

A Tutorial


Written 2010 by Will Johnson for Fast Forward Technologies
Email Fast Forward Technologies at
or post your comments for public view far below.
Creative Commons Attribution 3.0 License
The Knol Self-Tweeter on the left, is designed to work only for an article's author.

Follow Fast Forward Technologies on Twitter!
or use my Knol Public activity feed

Buy your own Knol Self-Tweeter for only two dollars! I will email it to you, when I receive payment.
What the Knol Self-Twitter does exactly is explained at this link.

<-- Start at PHP: Lesson 1 "Hello World"
<-- Back to PHP: Lesson 4 Grab a Web Page into PHP
--> Forward to PHP: Lesson 6 Simple Guestbook using PHP

Back in Lesson 3, we briefly mentioned HTML forms, where you can have the reader, fill in details about themselves like an email address, name, phone number, remarks and then submit that form to... something.

The most common somethings are email routines.  First I'll show you the simplest kind of email routine which does not need any sort of computer programming at all!  But then I'll show you a better one.

The simplest kind just uses the HTML directive mailto.  This works if and only if the reader has set up on their computer, what their default email client is.  Not all readers have done this, not all know how to, and even some of the ones who know how to, have done it wrong.  It happens!  Meanwhile you lose potential clients because they get annoyed at you!

Here is that simple HTML form:

<form method="post" action="">
    Name: <input type="text"  size="10" maxlength="40" name = "name"><br/>
    Email: <input type="text" size="10" maxlength="30" name="email"> <br />

    <textarea rows="5" cols="20" wrap="virtual" name="comments">
        Enter Comments Here

    <input type = "Submit">

This form has fields to allow the reader to enter their name, email address and some remarks, and displays a "Submit" button.  When they click the Submit button, the form executes whatever is in the "action" directive.  In this case it executes the command "".  This will open the reader's default email client, fill in the email and then they have to mail it!  All in all, not a very useful device, but it's the most popular on the web because it's sooooooo easy for a beginner to set it up.

But, the user sees a URL-encoded string in the body of their email, which isn't very pretty, and that's what you get in your inbox which also and still isn't very pretty.  So we're going to fix those issues, right now, using PHP.

First, instead of the action "mailto", we're going to make the action a script that we're going to write, so let's call it WebComments.php.  So our first form line then becomes:

<form method="post" action="WebComments.php">

This type of action directive would only work, if your Contact form and your WebComments.php are in the same directory on your website.  If they are in different directories you will need to specify the full path such as

<form method="post" action="">

We're not going to change anything else on the original form, now let's see what the PHP script would look like.  So create a file called WebComments.php and this is what you're going to put in it:

$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];

So far what this has done, is simply pull the passed-in arguments: name, email, comments, from the form; and loaded them into variable names in PHP, so we can play with them in some way.  We can format them in various ways, manipulate the answers and so on.  Every webserver has the SendMail program loaded on it, that's what we're going to use here, with the "mail" function below. The "\n" directive to SendMail means "New Line", so it's like the <br> in HTML, it breaks the line with a carriage return.

Let's do the simplest thing since we're learning:

$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];

$to = "";
$subject = "Comments from Your Website";
$body = "Hi,\n\n My name is".$name."\n";
$body = $body."My email is ".$email."\n";
$body = $body."My comments are : ".$comments;
if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
 } else {
  echo("<p>Message delivery failed...</p>");

That's it!  Now you will get web remarks sent to your inbox, and the user won't see weird messages on their screen that they don't understand.

<-- Start at PHP: Lesson 1 "Hello World"
<-- Back to PHP: Lesson 4 Grab a Web Page into PHP
--> Forward to PHP: Lesson 6 Simple Guestbook using PHP


5th Lesson

Good. You are posting useful knols.

Narayana Rao - 22 Feb 2010