Creating a Mail Client, Part 1

Posted by Nic on April 29, 2010.

This tutorial is Part 1 of a series on creating a Mail Client. In Part 1, I will show you how to send mail. You may download and print this tutorial as necessary.

Download the tutorial here(pdf)

Part 1: Sending Mail

Creating a mail client, to send AND receive mail, might be a daunting task for the average PHP-er. This tutorial should simply the process. To start, we are going to create a form to send mail.
Four things are needed when creating a functional form to send mail:

  1. A Recipient
  2. A Subject
  3. The message
  4. Optional (but recommended) headers (including to sender’s address and/or name)

Once you have those four things ready, you can use

mail()

or

imap_mail()

to send the message. Mail() sends an SMTP message and imap_mail uses the IMAP mail protocol. For this tutorial, we will use imap_mail().

Step 1: Creating the Form

Let’s start will a text field for the recipient, subject, and headers and a textarea for the message. For input boxes to be accessible with the PHP constant $_POST, you need to set up the name attribute. The id attribute is optional. I include it because I also program in Javascript. Additionally, your form’s action attribute must be the file in which your php code will be stored. For the method attribute we will use the post value so that the user input is not posted in the address box on submission.

<form action=script.php method=post>
To: <input type=text name=to id=to />
Subject: <input type=text name=subject id=subject />
Headers: <input type=text name=headers id=headers value=From:  />
Message: <textarea id=message name=message rows=5 cols=50>  </textarea>
</form>

Step 2: Form Adjustments

Now that the form is created, let’s make some minor adjustments so that everything is lined up. We’ll use tables for this task

<form action=script.php method=post>
<table>
<tr> <td>To: </td> <td> <input type=text  name=to id=to /> </td> </tr>
<tr> <td>Subject: </td> <td> <input type=text  name=subject id=subject /> </td> </tr>
<tr> <td>Headers: </td> <td> <input type=text  name=headers id=headers value=From:  /> </td> </tr>
<tr> <td>Message: </td> <td> <textarea  id=message name=message rows=5 cols=50> </textarea> </td>  </tr>
</table> </form>

Step 3: Creating the Action Script

To give the form some functionality we want to create an action script. If you used the code from above, save you script file as script.php
Remember that all of the form variables are loaded into the constant $_POST[] array. Because the functions are predefined, we just need to initialize variables to pass the information by value.

 <php
$recipient = $_POST[to];
$subject = $_POST[subject];
$headers = $_POST[headers];
$message = $_POST[message];
imap_mail($recipient, $subject, $message, $headers);
echo "Your mail has been sent to " .  $recipient;
?> 

This action script is very easy, once the form input has been placed into variables we just need to call the imap_mail() function and it does all of the IMAP server work for us…magic.

  • Share/Bookmark

Liked this post? Subscribe to Programming Adventures feed.

Leave a Reply