Home arrow PHP arrow Page 5 - 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 - 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);
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