Check your email with PHP and IMAP
At some point I wanted to check if there was any unread messages in a couple mailboxes from inside an website administration panel, so I ended up with a simple script like the one described here. What we want is to fetch the all the messages in the INBOX of each e-mail account for the past week, using the IMAP functions of PHP. Also, we are going to mark the unread messages with a different style.
WARNING: This is only an example on how to use some PHP IMAP functions.
It is NOT recommended to store your passwords in an unencoded php file on a shared server.
Create an array of your mailboxes
<?
// Configure your imap mailboxes
$mailboxes = array(
array(
'label' => 'Gmail',
'mailbox' => '{imap.gmail.com:993/imap/ssl}INBOX',
'username' => 'yourusername@gmail.com',
'password' => 'yourpassword'
),
array(
'label' => 'My Cpanel website',
'mailbox' => '{mail.yourdomain.com:143/notls}INBOX',
'username' => 'info+yourdomain.com',
'password' => 'yourpassword'
),
array(
'label' => 'Another mail account',
'mailbox' => '{mail.yourdomain.com:143/notls}INBOX',
'username' => 'info@yourdomain.com',
'password' => 'yourpassword'
)
);
?>
Fetch this week's messages from each mailbox
<div id="mailboxes">
<?
foreach ($mailboxes as $current_mailbox) {
?>
<div class="mailbox">
<h2><?=$current_mailbox['label']?></h2>
<?
// Open an IMAP stream to our mailbox
$stream = @imap_open($current_mailbox['mailbox'], $current_mailbox['username'], $current_mailbox['password']);
if (!$stream) {
?>
<p>Could not connect to: <?=$current_mailbox['label']?>. Error: <?=imap_last_error()?></p>
<?
} else {
// Get our messages from the last week
$emails = imap_search($stream, 'SINCE '. date('d-M-Y',strtotime("-1 week")));
// Instead of searching for this week's messages, you could search
// for all the messages in your inbox using: $emails = imap_search($stream, 'ALL');
if (!count($emails)){
?>
<p>No e-mails found.</p>
<?
} else {
// If we've got some email IDs, sort them from new to old and show them
rsort($emails);
foreach($emails as $email_id){
// Fetch the email's overview and show subject, from and date.
$overview = imap_fetch_overview($stream,$email_id,0);
?>
<div class="email_item clearfix <?=$overview[0]->seen?'read':'unread'?>"> <!-- add a different class for seperating read and unread e-mails -->
<span class="subject"><?=$overview[0]->subject?></span>
<span class="from"><?=$overview[0]->from?></span>
<span class="date"><?=$overview[0]->date?></span>
</div>
<?
}
}
// Close our imap stream.
imap_close($stream);
}
?>
</div>
<?
} // end foreach
?>
</div>
Decoding MIME extensions
Add the following function in order to decode headers that may contain non ASCII text. For example "=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=" should print "Keld Jørn Simonsen"
<?
// A function to decode MIME message header extensions and get the text
function decode_imap_text($str){
$result = '';
$decode_header = imap_mime_header_decode($str);
foreach ($decode_header AS $obj) {
$result .= htmlspecialchars(rtrim($obj->text, "\t"));
}
return $result;
}
?>
and change the output part to:
<div class="email_item clearfix <?=$overview[0]->seen?'read':'unread'?>">
<span class="subject"><?=decode_imap_text($overview[0]->subject)?></span>
<span class="from"><?=decode_imap_text($overview[0]->from)?></span>
<span class="date"><?=$overview[0]->date?></span>
</div>
That's all - regarding PHP. Try putting it all together, adding some CSS and some icons for read/unread messages, and you will come out with something like this demo page, or download all the files of the demo.
What else?
You could go a step further and get the message body with imap_fetchbody(), or add the functionality to mark a message as "read" or "unread". This could be as simple as this:
imap_setflag_full($stream,$email_id, "\\Seen"); // Set the flag "Seen" imap_clearflag_full($stream,$email_id,'\\Unseen'); // Clear the flag "Unseen"







Motyar • January 17, 2012 05:00
thank you for sharing.
Your theme of site is just COOL.
Tony-o • October 28, 2012 07:04
Zain • October 31, 2012 14:13
Mangalasamy • November 20, 2012 17:52
saeed • January 2, 2013 19:28
Moullablad • January 18, 2013 12:12
nimahkh • January 25, 2013 16:54
when is remove @ from imap_open function , i see fatall error .. and when i write it , nothing show me. whats the problem?
zameer • February 7, 2013 11:47
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@vss-mobi.co.in and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Lucas de Carvalho • March 6, 2013 06:05
So, can you show how/where I can use "imap_fetchbody()" to show the body of the message as well or simply say where I must put this to works?
Thank you in advance.
Vikas • March 13, 2013 09:26
Thanks a lot.
Can some body tell me the port of MSN for fetching emails, like google has 993 in this script
American Mail & Parcel • April 15, 2013 16:41
طراحی سایت • April 23, 2013 15:00
J • May 1, 2013 22:49