PHP
  Home arrow PHP arrow Page 4 - Create Your Own Mail Script With PHP and I...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Create Your Own Mail Script With PHP and IMAP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 107
    2002-03-25

    Table of Contents:
  • Create Your Own Mail Script With PHP and IMAP
  • What is IMAP?
  • IMAP functions
  • Creating our email script
  • The ShowEmail function and more
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PHP Articles
    More By Mitchell Harper


       · An excellent imap tutorial for beginners which gets them started with coding imap...
       · I just think the tutorials is not comprehensive enough for one to follow through....
       · I fully agree because It looks to complicated for me.I just want to install my...
     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT