PHP
  Home arrow PHP arrow Page 5 - 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 
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 / 92
    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 - The ShowEmail function and more


    (Page 5 of 6 )

    ShowEmail connects to our IMAP server using imap_open and retrieves both the header and body details of one specific email. As we saw earlier, imap_headerinfo returns the header details of an email as an array, like this:

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

    ...

    $from = $mailHeader->fromaddress;

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

    $date = $mailHeader->date;


    The body of the email is then retrieved with a call to imap_body. Because our email script only displays plain text emails, we use strip_tags to remove all HTML formatting from the email:

    $body = nl2br(strip_tags(imap_body($conn, $id)));

    We then output all of these details to a form, including a button that lets us reply to this specific email:

    <form name="frmLogin" action="<?php echo $PHP_SELF; ?>" method="post">

    <input type="hidden" name="what" value="reply">

    <input type="hidden" name="to" value="<?php echo $from; ?>">

    <input type="hidden" name="subject" value="Re: <?php echo $subject; ?>">

    <input type="hidden" name="message" value="<?php echo str_replace("<br />", "", str_replace("\r\n", "\r\n>>", $body)) ?>">

    <h2><?php echo $subject; ?></h2>

    <hr noshade size="1" color="#20158F">

    <b>From:</b> <?php echo str_replace("<", "&lt;", $from); ?><br>

    <b>Date:</b> <?php echo $date; ?><br>

    <hr noshade size="1" color="#20158F">

    <?php echo $body; ?>

    <hr noshade size="1" color="#20158F">

    <input type="submit" name="submit" value="Reply >>">

    </form>


    As you can see, this form passes the value "reply" for the hidden form variable "what". The to, subject and message fields are also passed as hidden form variables, which in turn calls the ShowComposeForm function, which displays a HTML form in exactly the same was as the ShowEmail function.

    Although this might sound a little confusing, the ShowComposeForm function is also used to compose a new email. The parameters passed to the function are the values for each text box in the form: if we don't pass any parameters then we're creating a new form. If we do, then we're replying to a message. Here's the function signature of ShowComposeForm:

    function ShowComposeForm($to = "", $subject = "", $message = "")

    The ShowComposeForm function includes a hidden form variable, what, which is set to "send", triggering a call to the SendMessage function when the form is submitted.

    The function signature of SendMessage looks like this:

    SendMessage($to, $subject, $message, $cc, $bcc);

    SendMessage uses the imap_open function to connect to our mail server. If the connection was successful, it then uses imap_mail to send an email message to the intended recipient, like this:

    imap_mail($to, $subject, $message, "From: $user@$host", $cc, $bcc);

    One last function that I will mention is our DeleteEmails function. This function calls imap_delete as well as imap_explunge to remove email messages from our mailbox based on their ID. The ID's for the DeleteEmails function are passed as an array from the form generated by the ShowInbox function:

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

    ...

    <td width="5%"">

    <input type="checkbox" name="emails[]" value="<?php echo $i; ?>">

    </td>


    The imap_delete function accepts two parameters: an IMAP stream object and the ID of the email to delete:

    for($i = 0; $i < sizeof($emails); $i++)

    {

    @imap_delete($conn, (int)$emails[$i]);

    }


    Once we've called imap_delete for each email, we must call the imap_expuge function to actually remove the messages from the server:

    @imap_expunge($conn);

    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 1 hosted by Hostway
    Stay green...Green IT