PHP
  Home arrow PHP arrow Page 3 - 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  
Moblin 
IBM® developerWorks 
Sun Developer Network 
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 / 91
    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 - IMAP functions


    (Page 3 of 6 )

    The IMAP library has 61 functions. Luckily we only need to use a handful of these to create a fully functional email script. The functions we will be using in this article are shown and detailed below:
    • imap_open: Opens an IMAP stream to a mailbox.
    • imap_headers: Returns headers for all emails in a mailbox.
    • imap_headerinfo: Reads the header of an email.
    • imap_delete: Mark an email for deletion from current mailbox.
    • imap_expunge: Delete all emails marked for deletion.
    • imap_mail: Sends an email message to a recipient.
    • imap_body: Reads the body of an email from a mailbox.
    As mentioned earlier, we're going to create a script that allows us to check our email account on the move using IMAP. Before we create the script however, let's start with the basics. Create a new file called testimap.php and enter the following code into it:

    <?php

    $server = "mail.com";

    $user = "john";

    $pass = "mypass";

    $conn = @imap_open("\{$server/imap}INBOX", $user, $pass)

    or die("Connection to server failed");

    ?>


    Replace the $server variable with the host name of your mail server. Replace $user with your mailbox account name and also replace $pass with the password for your mailbox. To connect to our mail server we use the imap_open function, passing in our mailbox, user ID and password. The format of the mailbox parameter looks like this:

    {host_name[:port number]/imap}INBOX

    The port number part of the mailbox parameter is optional. If we refer back to the testimap.php file above, our mailbox parameter looks like this:

    {mail.com/imap}MAILBOX

    In our example we've left out the port number. We could've optionally specified the port number like this:

    {mail.com:25/imap}INBOX

    The imap_open function returns an IMAP stream on success and FALSE on failure. We use this returned IMAP stream with various other imap_xxx function calls to manipulate our mailbox, send emails, etc.

    To retrieve a list of emails in our mailbox, we can use the following code:

    $headers = @imap_headers($conn)

    or die("Couldn't get emails");

    $numEmails = sizeof($headers);

    echo "You have $numEmails in your mailbox";


    In the example above I've called the imap_headers function, passing in our IMAP stream as the only parameter. The imap_headers function returns an array of headers. Each of these headers contains the details of one email in our mailbox, and we can use the values of these headers to show who these emails have come from, their subjects, date received, etc. These headers do not however contain the body of each email message, and we must use the imap_body function to get them.

    Now that we've got an array of headers representing each email in our mailbox, we can loop through them to display the details of each message with the imap_headerinfo function, like this:

    for($i = 1; $i < $numEmails+1; $i++)

    {

    $mailHeader = @imap_headerinfo($conn, $i);

    $from = $mailHeader->fromaddress;

    $subject = strip_tags($mailHeader->subject);

    $date = $mailHeader->date;

    echo "Email from $from, subject $subject, date $date<br>";

    }


    As you can see from the code above, we use the imap_headerinfo function to get the header details for each message. We extract the from address, subject and date for each message and use the echo command to output them to the browser.

    Now that we've displayed each message in our mailbox, we can use the imap_close function to close the IMAP stream we created earlier with imap_open:

    imap_close($conn);

    On this page we've taken a look at the basic imap_xxx functions that are required to open an IMAP stream, retrieve headers and display the details of each email from these headers. On the next page we're going to put everything we've learnt so far to good use and create the email script I've been mentioning.

    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....
     

    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-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT