Create Your Own Mail Script With PHP and IMAP - Creating our email script
(Page 4 of 6 )
There are a huge number of email applications on the market, however most of them expose the same core functionality:
- You can check your mailbox on any mail server.
- You can create and send emails.
- You can reply to emails.
- You can delete emails from your mailbox.
The script we're about to discuss implements each of the bullet points shown above. We will run through the code step by step, describing various function calls where necessary.
Our script contains several functions and a switch statement. The switch statement works with the $what variable, which can be either a query string or form posted variable. Each time our script is called, a different function is executed, depending on the value of $what:
switch($what)
{
case "login":
ProcessLogin();
break;
case "inbox":
ShowInbox();
break;
case "delete":
DeleteEmails();
break;
case "compose":
ShowComposeForm();
break;
case "send":
SendMessage($to, $subject, $message, $cc, $bcc);
break;
case "read":
ShowEmail();
break;
case "reply":
ShowComposeForm($to, $subject, $message);
break;
default:
ShowLoginForm();
}As you can see, the default option calls the ShowLoginForm function, which allows us to enter our email address and password. The ShowLoginForm function simply displays a HTML form which includes a hidden form variable, $what, which is set to "login".
The ProcessLogin function is called when $what is equal to "login". It validates the email address from the login form using the ereg function and creates some session variables containing our mailbox, username, host and password:
$validEmail = ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $email);
$validPass = ($password == "" ? false: true);
$portString = (is_numeric($port) == true ? ":$port" : "");
if(!$validEmail || !$validPass)
{
// Email or password are bad
die("<h2>Invalid Login Credentials</h2>");
}
else
{
// Email and password are fine, get the user and hosts
$arr = split("@", $email);
$user = $arr[0];
$host = $arr[1];
$mailbox = "\{$host$portString/imap}INBOX";
// Set the login credentials as session variables
setcookie("mailbox", $mailbox);
setcookie("user", $user);
setcookie("host", $host);
setcookie("password", $password);
header("Location: $PHP_SELF?what=inbox");
}As you can see, we use the split function to separate the users email address into the $user and $host variables. We also create the $mailbox variable from the host name and port number, which may or may not be specified in the login form. The user is then redirected to the same scripting using $PHP_SELF, passing "inbox" as the $what query string variable. This results in the ShowInbox function being called.
The ShowInbox function connects to our mail server using imap_open (which we saw in our first example) and retrieves the headers for each email message in our mailbox. These headers are then extracted into variables and a HTML table is created that lists the details of each email. Each email is also hyper linked to the script, setting the query variables $what to "read", and $id to the ID of the email, which is the index of that particular email in the array returned from a call to imap_headers:
<td width="50%">
<a href="<?php echo $PHP_SELF; ?>?what=read&id=<?php echo $i; ?>">
<?php echo $subject; ?>
</a>
</td>When $what is "read", the ShowEmail function is called.
Next: The ShowEmail function and more >>
More PHP Articles
More By Mitchell Harper