User Tools

Site Tools


Small utilite for show mail aliases tree

DB structure

table aliases {
  username;
  domain;
  sendto;
}

table boxes {
  username;
  domain;
}

Code

alias2tree.php
#!/usr/local/bin/php
<?php
 
function print_mail ($dbcon, $email, $level) {
 
  if ($level > 8) {
      print str_repeat("|\t", $level)."| *** too many level for $email ***\n";
  } else {
 
    $exist = 0;
 
    list($username, $domain) = explode("@", $email);
 
    $query = "select distinct username, domain from boxes where username = '$username' and domain = '$domain' and is_active = 'Y';";
    $mb_table = mysqli_query($dbcon, $query);
 
    if (mysqli_num_rows($mb_table) > 0) {
      while ($mb_row = mysqli_fetch_assoc($mb_table)) {
        $mb = $mb_row['username'].'@'.$mb_row['domain'];
        print str_repeat("|\t", $level)."*** ".$mb."\n";
      }
      $exist++;
    }
    mysqli_free_result($mb_table);
 
    $query = "select distinct username, domain, sendto from aliases where username = '$username' and domain = '$domain' and is_active = 'Y';";
    $table = mysqli_query($dbcon, $query);
 
    if (mysqli_num_rows($table) > 0) {
      print str_repeat("|\t", $level)."==> ".$email." :\n";
      while ($row = mysqli_fetch_assoc($table)) {
        $sendto = $row['sendto'];
 
        print_mail($dbcon, $sendto, $level+1);
 
      }
      mysqli_free_result($table);
      print str_repeat("|\t", $level)."\n";
      $exist++;
    }
    if ($exist = 0) {
      print str_repeat("|\t", $level)."!!! $email\n";
    }
  }
}
 
 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
 
$con = mysqli_connect("127.0.0.1","root") or die("Could not connect: " . mysql_error());
printf("Connected successfully\n");
 
mysqli_select_db($con, 'mail2') or die('Could not select database');
printf("Select db successfully\n");
 
 
$query = "select distinct username, domain from aliases where is_active = 'Y';";
 
$table = mysqli_query($con, $query);
$count_rows = mysqli_num_rows($table);
printf("count rows = %d\n", $count_rows);
 
while ($row = mysqli_fetch_assoc($table)) {
    $username = $row['username'];
    $domain = $row['domain'];
    $email = $username.'@'.$domain;
 
    $q = "select username, domain from aliases where sendto = '$email' and is_active = 'Y' limit 3;";
    $t = mysqli_query($con, $q);
    if (mysqli_num_rows($t) == 0) {
//        print $email." =>\n";
        print_mail($con, $email, 0);
 
    }
    mysqli_free_result($t);
}
 
mysqli_free_result($table);
mysqli_close($con);
 
//EOF

Output example

==> all.nw@nw.domain.com :
|	*** a.kuznetsov@domain.com
|	==> arkh.do@nw.domain.com :
|	|	==> arkh.salon@nw.domain.com :
|	|	|	*** atrium-sev@nw.domain.com
|	|	|	*** bum-arkh@nw.domain.com
|	|	|	*** dirarkhangelsk@nw.domain.com
|	|	|	*** dombita-arkh@nw.domain.com
|	|	|	*** forum-arkh@nw.domain.com
|	|	|	*** m.imperia-arkh@nw.domain.com
|	|	|	*** o.potemkina@domain.com
|	|	|	*** omega-sev@nw.domain.com
|	|	|	*** urs1-arkh@nw.domain.com
|	|	
|	|	*** buhg-arkh@nw.domain.com
|	|	*** dirarkhangelsk@nw.domain.com
|	|	*** dispserv-arkh@nw.domain.com
|	|	*** it-arkh@nw.domain.com
|	|	*** manserv-arkh@nw.domain.com
|	|	*** nachsklad-arkh@nw.domain.com
|	|	*** nss-arkh@nw.domain.com
|	|	*** urs1-arkh@nw.domain.com

*** etika@domain.com
==> etika@domain.com :
|	*** ethics@domain.com
|	==> ethics@domain.com :
|	|	*** e.epifanceva@domain.com
|	|	*** vitaliy@domain.com
|	

==> ezhergin_rj@domain.com :
|	*** ezhergin1@domain.com
|	*** ezhergin2@domain.com
|	*** ezhergin3@domain.com

==> ftp@domain.com :
|	==> root@domain.com :
|	|	*** sysmaster@domain.com
|	

==> ftp-bugs@rnd.domain.com :
|	==> ftp@rnd.domain.com :
|	|	==> root@rnd.domain.com :
|	|	|	*** cr3w@rnd.domain.com
|	|	|	==> cr3w@rnd.domain.com :
|	|	|	|	*** roman1c@rnd.domain.com
|	|	|	|	*** v.kuchma@rnd.domain.com
|	|	|	
|	|	
|	

==> g.slavov@nw.domain.com :
|	*** office@nw.domain.com