View Full Version : Using GREP to display the subject of a mail message

08-27-2006, 01:44 AM
Okay, I've been looking around trying to find a way to do this, and I can't figure it out so far. I'm still a real novice at PHP coding, and I'm pretty much stuck at this point. I have a PHP file that will automatically parse a directory listing for any given directory into a nice and pretty format, but what I'm using it for requires some additional functionality that I can't quite figure out.

I want to list the contents of the mail folders on a linux system (no problem with that part), but I want the page to display the subject of the mail files rather than the actual file names. It will also need to display the "from" information from the mail message. The only way that I can think to do this is using the Unix GREP command. I've found ways to make the PHP code actually GREP the file, but I can't figure out how to actually show exactly what I want.

The basic functionality of the page is like this at the moment:

$arsize = sizeof($files);
for($i=0;$i<$arsize;$i++) {
if (strrpos($files[$i], '.'))
($ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1)));
else ($ext = '');

if($filetypes[$ext]) {
$icon = $filetypes[$ext];

$filename = $files[$i];
if(strlen($filename)>43) {
$filename = substr($files[$i], 0, 40) . '...';

$fileurl = $leadon . $files[$i];
if($forcedownloads) {
$fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($leadon) . '&download=' . urlencode($files[$i]);
<tr class="<?=$class;?>" id="listing">
<td width="24" align="center" valign="middle">
<input type="checkbox" />
<td width="16" align="center" valign="middle">
<a href="<?=$fileurl;?>"><img src="dlf/<?=$icon;?>" alt="<?=$files[$i];?>" /></a>
<td width="300" align="left" valign="middle">
<a href="<?=$fileurl;?>"><strong><?=$filename;?></strong></a>
<td width="170" align="right" valign="middle">
<td width="250" align="right" valign="middle">
<?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?>
I'm thinking I should be able to use some code to GREP the actual files to find the literal string "Subject:", but then I'm not sure how to actually pull what should BE the subject into a variable to display. I found the following code floating around on the Net for doing the GREP (although I'm not entirely sure I understand it all, but I'm gonna do some research on that part in a bit):

$handle = popen('grep --max-count=1 Subject: /path/to/file.txt', 'r');
$output = fread($handle, 2096);
I figure I can use this to find the line where the subject of the message is, so the trick becomes how to get that subject setup into a variable for display in the table. Then, of course, I'm going to need to do the same basic thing for the "From:" part of the message as well.

Any suggestions anyone may have on how to do this would be greatly appreciated. Thank you for your time.