Read email from cpanel with imap in php


<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>

<style>
div.toggler { border:1px solid #ccc; background:url(gmail2.jpg) 10px 12px #eee no-repeat; cursor:pointer; padding:10px 32px; }
div.toggler .subject { font-weight:bold; }
div.read { color:#666; }
div.toggler .from, div.toggler .date { font-style:italic; font-size:11px; }
div.body { padding:10px 20px; }
</style>

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script&gt;
<script type=”text/javascript”>
$(document).ready(function(){
$(“.toggler “).click(function(){
$(“.body”).toggle();
});
});
</script>

</head>

<body>

<?php
ini_set(“max_execution_time”,360);

/* connect to server */
$hostname = ‘{domain.com:143/notls}INBOX’;
$username = ‘amit@domain.com’;
$password = ‘amitmondal’;

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die(‘Cannot connect to domain:’ . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,’ALL’);

/* if emails are returned, cycle through each… */
if($emails) {

$output = ”;

/* put the newest emails on top */
rsort($emails);

echo “Number of email:”.imap_num_msg($inbox);

/* for every email… */
foreach($emails as $email_number) {

$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1);

$output.= ‘<div class=”toggler ‘.($overview[0]->seen ? ‘read’ : ‘unread’).'”>’;
$output.= ‘<span class=”subject”>’.$overview[0]->subject.'</span> ‘;
$output.= ‘<span class=”from”>’.$overview[0]->from.'</span>’;
$output.= ‘<span class=”date”>on ‘.$overview[0]->date.'</span>’;
$output.= ‘</div>’;

$output.= ‘<div class=”body”>’.$message.'</div>’;
} //end of for loop
echo $output;
} //end of if statement

/* close the connection */
imap_close($inbox);

?>

</body>
</html>

Advertisements

14 Replies to “Read email from cpanel with imap in php”

  1. Excellent article. I was checking continuously this blog and I am quite impressed! Very helpful info specially the first part. I care for such information much. I was seeking this certain information for a long time. Thank you and good luck.

  2. Nice article. I was checking continuously this blog and I am quite impressed! Very helpful information specially the last part. I care for such info much. I was looking for this particular information for a very long time. Thank you and best of luck.

  3. The code in this article uses non standard quotemarks, this is VERY bad practice, almost every apache setup will fault you on these quote marks, you should always use ” and ‘. Not their special character alternatives like in your post here. Just a heads up

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s