Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
 
Go Back   Zimbra - Forums > Zimbra Collaboration Suite > Administrators

Welcome to the Zimbra - Forums!
Welcome, if you would like to post a comment please register. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-21-2008, 09:35 PM
New Member
 
Posts: 4
Default [SOLVED] qview.php - php script to handle quarantined emails

[possible repost: my previous post was flagged as spam, so i'll post with the php in the thread instead of as an attachment]

Until the RFE comes through, I created a simple php script to work with the quarantined email in a web interface. It's a first draft and I spent an hour working on it, so I won't be surprised if there might be some issues with it Not ideal but I think it's better than hunting around in the quarantine folder.

Also, currently there is not security provided at the moment. So anyone could get to this page. .htaccess or a web function to tie into the ldap system in zimbra should do the trick.

At the moment, you can view the raw email content or download the quarantine file but not release it back into the mail system to a recipient. The downloaded file will have a .eml extension so a email client should be able to render it.

Hope this helps others out!

Instructions:
  • Copy and paste the code below. Put it in a file called qview.php in /opt/zimbra/httpd/htdocs in your zimbra installation.
  • Access it http://<your zimbra server url>:7780/qview.php

Code:
PHP Code:
<?php

// Code by durango99
// 10/21/2008
// 

$getflag $_GET['getflag'];

$getfile $_GET['getfile'];
$getfile urldecode($getfile);

$dirname "/opt/zimbra/data/amavisd/quarantine/";

//download it
if ($getflag == 1){

    
header('Content-Description: File Transfer');
    
header('Content-Type: application/octet-stream');
    
header('Content-Disposition: attachment; filename='.basename($getfile).'.eml');
    
header('Content-Transfer-Encoding: binary');
    
header('Expires: 0');
    
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    
header('Pragma: public');
    
header('Content-Length: ' filesize($getfile));
    
ob_clean();
    
flush();
    
readfile($getfile);

    die;

}


?>

<html>
<title>
Qview - Quarantine viewer
</title>

<head>
<style type="text/css">
body { 
font-family: arial;
font-size: normal;
 }

table { 
font-family: arial;
font-size: small;
 }
 
</style>
</head>

<body>
<?

echo "<p>Current Quarantine View:</p>\n";

//view it
if ($getflag == 2){


    $file = file_get_contents ($getfile); 

    echo "<pre>$file</pre>";
    
    die;
    
}


$qfiles = dirList($dirname);

echo "<table border=1>\n";

echo "<tr>
<td>#</td>
<td>From:</td>
<td>To</td>
<td>Subject</td>
<td>View</td>
<td><font color=red><b>(CAREFUL!)</font></b></td>
</tr>";

$i=1;
foreach ($qfiles as $qfile){

$YourFile = $dirname . $qfile;
$file = file_get_contents ($YourFile); 

echo "<tr><td>\n";

echo "$i";


echo "</td><td>\n";

preg_match( "/^From:(.*)$/m", $file, $fromline );
echo ($fromline[1]);

echo "</td><td>\n";

preg_match( "/^To:(.*)$/m", $file, $toline );
echo ($toline[1]);

echo "</td><td>\n";

preg_match( "/^Subject:(.*)$/m", $file, $subjline );
echo ($subjline[1]);

echo "</td><td>\n";

$ue=str_replace('+','%2B', $YourFile);
$ue=urlencode($ue);

echo "<a href=qview.php?getfile=$ue&getflag=2 target=_blank><font size=1em>$qfile</font></a>";

echo "</td><td>\n";

echo "<a href=qview.php?getfile=$ue&getflag=1 target=_blank><font color=grey>download</font></a>";

echo "</td></tr>\n";

$i++;

}

echo "</table>\n";


function dirList ($directory) 
{

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = $file;
    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}

?>
</body>
</html>
Reply With Quote
  #2 (permalink)  
Old 10-21-2008, 10:06 PM
Zimbra Consultant
 
Posts: 5,814
Default

Very nice:
__________________
-Mike Morse (MCode151)

ZCS-to-ZCS Migrations & Moves | Admin Tools & Tidbits » ZimbraBlog.com | ZimbraCommunity.com
Reply With Quote
  #3 (permalink)  
Old 10-22-2008, 10:57 AM
New Member
 
Posts: 4
Default

Quote:
Originally Posted by mmorse View Post
Very nice:
Thanks Mike for the kind words and providing a screenshot of the interface. Glad it's working for you.

I'll probably add a date column and a sort feature so it's easier to read.
Reply With Quote
  #4 (permalink)  
Old 12-01-2008, 09:25 AM
Senior Member
 
Posts: 57
Default

Is this php ment to show daily results ? It would be nice to see feedback for more then just daily captures
Reply With Quote
  #5 (permalink)  
Old 12-01-2008, 10:01 AM
Special Member
 
Posts: 134
Default

Nice script, I've checked your code now, but Is this code for each user he can mange his quarantine mails or it must be check through the admin consol?

Regards
Reply With Quote
  #6 (permalink)  
Old 12-01-2008, 10:05 AM
Moderator
 
Posts: 5,806
Default

The script is for all users ... time permitting I would like to try and write a zimlet for this, but need to see how to write the backend code.
__________________
SplatNIX IT Services :: Innovation through Collaboration™


http://www.messagefortress.com
Reply With Quote
  #7 (permalink)  
Old 12-01-2008, 10:10 AM
Special Member
 
Posts: 134
Default

I want to know how to write the backend code in order to write a zmilets, with reference to my experience with the development world, I have to many ideas to be written by the zimlet in order to utilize each user separated.

I wish studing this.. and I hope there is an document for the beginers.

Yours Sincerely,
Reply With Quote
  #8 (permalink)  
Old 11-04-2009, 06:17 AM
Senior Member
 
Posts: 59
Default Added sort by date

Thanks for the script durango99. We've slightly adapted it to our needs. We've added a date column and display the array sorted by date.
PHP Code:
<?php

// Code by durango99
// 10/21/2008
// Adapted by Hivos
// 11/02/2009

$getflag $_GET['getflag'];

$getfile $_GET['getfile'];
$getfile urldecode($getfile);

$dirname "";//"/opt/zimbra/data/amavisd/quarantine/";

//download it
if ($getflag == 1){

    
header('Content-Description: File Transfer');
    
header('Content-Type: application/octet-stream');
    
header('Content-Disposition: attachment; filename='.basename($getfile).'.eml');
    
header('Content-Transfer-Encoding: binary');
    
header('Expires: 0');
    
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    
header('Pragma: public');
    
header('Content-Length: ' filesize($getfile));
    
ob_clean();
    
flush();
    
readfile($getfile);

    die;

}


?>

<html>
<title>
Zimbra quarantine viewer
</title>

<head>
<style type="text/css">
body { 
font-family: arial;
font-size: normal;
 }

table { 
font-family: arial;
font-size: small;
 }
 
</style>
</head>

<body>
<?php

echo "<p>Current Quarantine View:</p>\n";

//view it
if ($getflag == 2){


    
$file file_get_contents ($getfile); 

    echo 
"<pre>$file</pre>";
    
    die;
    
}


$qfiles dirList($dirname);

echo 
"<table border=1>\n";

echo 
"<tr>
<td>#</td>
<td>Date:</td>
<td>From:</td>
<td>To</td>
<td>Subject</td>
<td>View</td>
<td><font color=red><b>(CAREFUL!)</font></b></td>
</tr>"
;

$i=1;
foreach (
$qfiles as $qfile){
    
$YourFile $dirname $qfile;
    
$file file_get_contents ($YourFile); 

    echo 
"<tr><td>\n";
    echo 
"$i";

    echo 
"</td><td>\n";
    
preg_match"/^Date:(.*)$/m"$file$date );
    echo 
"$date[1]";

    echo 
"</td><td>\n";
    
preg_match"/^From:(.*)$/m"$file$fromline );
    echo (
$fromline[1]);

    echo 
"</td><td>\n";
    
preg_match"/^To:(.*)$/m"$file$toline );
    echo (
$toline[1]);

    echo 
"</td><td>\n";
    
preg_match"/^Subject:(.*)$/m"$file$subjline );
    echo (
$subjline[1]);

    echo 
"</td><td>\n";
    
$ue=str_replace('+','%2B'$YourFile);
    
$ue=urlencode($ue);
    echo 
"<a href=qview.php?getfile=$ue&getflag=2 target=_blank><font size=1em>$qfile</font></a>";

    echo 
"</td><td>\n";
    echo 
"<a href=qview.php?getfile=$ue&getflag=1 target=_blank><font color=grey>download</font></a>";
    echo 
"</td></tr>\n";

    
$i++;

}

echo 
"</table>\n";


function 
dirList($directory)
{
    
$results glob"/opt/zimbra/data/amavisd/quarantine/*" );

    
// Sort files by modified time, latest to earliest
    // Use SORT_ASC in place of SORT_DESC for earliest to latest
    
array_multisort(
    
array_map'filemtime'$results ),
    
SORT_NUMERIC,
    
SORT_DESC,
    
$results
    
);
    return 
$results;
}
?>
Reply With Quote
Reply


Thread Tools
Display Modes


Similar Threads

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

Zimbrablog.com




 

Search Engine Optimization by vBSEO 3.1.0