Home arrow PHP arrow Page 4 - Create Your Own Mail Script With PHP and IMAP
PHP

Create Your Own Mail Script With PHP and IMAP


Thanks to PHP, we can check our email account remotely using PHP and its imap_xxx functions, which allow us to communicate with mail servers via IMAP, POP3 or NNTP protocols. In this article Mitchell shows us how to create a completely web-based email checking script which can also delete, send and reply to emails... all using only PHP's IMAP functions.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 196
March 25, 2002
TABLE OF CONTENTS:
  1. · Create Your Own Mail Script With PHP and IMAP
  2. · What is IMAP?
  3. · IMAP functions
  4. · Creating our email script
  5. · The ShowEmail function and more
  6. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials