PHP Accessing an IMAP Mailbox

This week I started playing around with PHP and a honeypot mailbox I had created some time ago. The following PHP is what I have been using thus far – plenty more work to do:


$hostname = '{}INBOX';
$username = 'honeypot';
$password = 'topsecret';

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Mail server as user: ' . imap_last_error());
$count = imap_num_msg($inbox);
print "\nNumber of messages is:".$count;
$emails = imap_search($inbox,'ALL');
if($emails) {
$output = '';
foreach($emails as $email_number) {

/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
//()Root Message Part (multipart/related)
//(1) The text parts of the message (multipart/alternative)
//(1.1) Plain text version (text/plain)
//(1.2) HTML version (text/html)
//(2) The background stationary (image/gif)
$message = imap_fetchbody($inbox,$email_number,1);
$header = imap_headerinfo($inbox, $email_number);
// We only want to check external emails
//pull out the email address of the sender for blacklisting
if (preg_match("<[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})>", $address_sender, $matches)) {
print "\nSender: ".$matches[0];

$decoded_message=quoted_printable_decode ( $message );
//pull out any links in the email for blacklisting - this still needs some work
if (preg_match("((https?|ftp|gopher|telnet|file|notes|ms-help):((\/\/)|(\\\\))+[\w\d:#@%\/;$()~_?\+-=\\\.&]*)",$decoded_message, $link_matches)){

/* output the email header information */
//$overview[0]->seen ? 'read' : 'unread')
/* close the connection */


Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>