您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

PHP操作IMAP服务器的类_PHP

2024/4/16 12:32:31发布8次查看
imap
我认为是相当经典的,帮过我大忙,可以实现很多php的imap函数不能实现的功能。以前在广州站贴过,那时北京站还没php版,现在再贴一个吧。:)
host = $imap_host;
$this->port = $imap_port;
$this->mbox = ;
$this->list = array();
$this->admin = $imap_admin;
$this->pass = $imap_pw;
$this->fp = 0;
$this->line = ;
$this->error_msg = ;
}
/*
#
# socketlogin on server via telnet-connection!
#
*/
function imap_login() {
$this->fp = fsockopen($this->host, $this->port, &$errno, &$errstr);
$this->error_msg=$errstr;
if(!$this->fp) {
echo
errorno: ($errno)
errstr: ($errstr)
\n;
} else {
$this->command(. login \$this->admin\ \$this->pass\);
}
return $errno;
}
/*
#
# socketlogout from server via telnet-connection!
#
*/
function imap_logout() {
$this->command(. logout);
fclose($this->fp);
}
/*
#
# sending command to server via telnet-connection!
#
*/
function command($line) {
/* print ($line
); */
$result = array();
$i=0; $f=0;
$returntext=;
$r = fputs($this->fp,$line\n);
while (!((strstr($returntext,. ok)||(strstr($returntext,. no))||(strstr($returntext,. bad)))))
{
$returntext=$this->getline();
/* print ($returntext
); */
if ($returntext)
{
if (!((strstr($returntext,. ok)||(strstr($returntext,. no))||(strstr($returntext,. bad)))))
{
$result[$i]=$returntext;
}
$i++;
}
}
if (strstr($returntext,. bad)||(strstr($returntext,. no)))
{
$result[0]=$returntext;
$this->error_msg = $returntext;
if (( strstr($returntext,. no quota) ))
{
}
else
{
print
error: unexpected imap-server-error
sent command: $line
server returned:
;
for ($i=0; $i print $result[$i]
;
}
print
;
}
}
return $result;
}
/*
#
# reading from server via telnet-connection!
#
*/
function getline() {
$this->line = fgets($this->fp, 256);
return $this->line;
}
/*
#
# quota functions
#
*/
// getting quota
function getquota($mb_name) {
$output=$this->command(. getquota \$mb_name\);
if (strstr($output[0],. no))
{
$ret[used] = not-set;
$ret[qmax] = not-set;
}
else
{
$realoutput = str_replace(), , $output[0]);
$tok_list = split( ,$realoutput);
$si_used=sizeof($tok_list)-2;
$si_max=sizeof($tok_list)-1;
$ret[used] = str_replace(),,$tok_list[$si_used]);
$ret[qmax] = $tok_list[$si_max];
}
return $ret;
}
// setting quota
function setmbquota($mb_name, $quota) {
$this->command(. setquota \$mb_name\ (storage $quota));
}
/*
#
# mailbox functions
#
*/
function createmb($mb_name, $mb_partition=) {
$this->command(. create \$mb_name\ $mb_partition);
}
function deletemb($mb_name) {
$this->command(. setacl \$mb_name\ $this->admin d);
$this->command(. delete \$mb_name\);
}
function renamemb($mb_name, $newmbname) {
$all=lrswipcda;
$this->setacl($mb_name, $this->admin,$all);
$this->command(. rename \$mb_name\ \$newmbname\);
$this->deleteacl($newmbname, $this->admin);
}
function renameuser($from_mb_name, $to_mb_name) {
$all=lrswipcda; $find_out=array(); $split_res=array(); $owner=; $oldowner=;
/* anlegen und kopieren der inbox */
$this->createmb($to_mb_name);
$this->setacl($to_mb_name, $this->admin,$all);
$this->copymailsfromfolder($from_mb_name, $to_mb_name);
/* quotas uebernehmen */
$quota=$this->getquota($from_mb_name);
$oldquota=trim($quota[qmax]);
if (strcmp($oldquota,not-set)!=0) {
$this->setmbquota($to_mb_name, $oldquota);
}
/* den rest umbenennen */
$username=str_replace(.,/,$from_mb_name);
$split_res=explode(., $to_mb_name);
if (strcmp($split_res[0],user)==0) {
$owner=$split_res[1];
}
$split_res=explode(., $from_mb_name);
if (strcmp($split_res[0],user)==0) {
$oldowner=$split_res[1];
}
$find_out=$this->getfolders($username);
for ($i=0; $i
if (strcmp($find_out[$i],$username)!=0) {
$split_res=split($username,$find_out[$i]);
$split_res[1]=str_replace(/,.,$split_res[1]);
$this->renamemb((str_replace(/,.,$find_out[$i])), ($to_mb_name.$split_res[1]));
if ($owner) {
$this->setacl(($to_mb_name.$split_res[1]),$owner,$all);
}
if ($oldowner) {
$this->deleteacl(($to_mb_name.$split_res[1]),$oldowner);
}
};
}
$this->deleteacl($to_mb_name, $this->admin);
$this->imap_logout();
$this->imap_login();
$this->deletemb($from_mb_name);
}
function copymailsfromfolder($from_mb_name, $to_mb_name) {
$com_ret=array();
$find_out=array();
$all=lrswipcda;
$mails=0;
$this->setacl($from_mb_name, $this->admin,$all);
$com_ret=$this->command(. select $from_mb_name);
for ($i=0; $i if (strstr( $com_ret[$i], exists))
{
$findout=explode( , $com_ret[$i]);
$mails=$findout[1];
}
}
if ( $mails != 0 ) {
$com_ret=$this->command(. copy 1:$mails $to_mb_name);
for ($i=0; $i print $com_ret[$i]
;
}
}
$this->deleteacl($from_mb_name, $this->admin);
}
/*
#
# acl functions
#
*/
function setacl($mb_name, $user, $acl) {
$this->command(. setacl \$mb_name\ \$user\ $acl);
}
function deleteacl($mb_name, $user) {
$result=$this->command(. deleteacl \$mb_name\ \$user\);
}
function getacl($mb_name) {
$aclflag=1; $tmp_pos=0;
$output = $this->command(. getacl \$mb_name\);
$output = explode( , $output[0]);
$i=count($output)-1;
while ($i>3) {
if (strstr($output[$i],'')) {
$i++;
}
if (strstr($output[$i-1],'')) {
$aclflag=1;
$lauf=$i-1;
$spacestring=$output[$lauf];
$tmp_pos=$i;
$i=$i-2;
while ($aclflag!=0)
{
$spacestring=$output[$i]. .$spacestring;
if (strstr($output[$i],'')) { $aclflag=0; }
$i--;
}
$spacestring=str_replace(\,,$spacestring);
if ($i>2) {
$ret[$spacestring] = $output[$tmp_pos];
}
}
else
{
$ret[$output[$i-1]] = $output[$i];
$i = $i - 2;
}
}
return $ret;
}
/*
#
# folder functions
#
*/
function getfolders($username){
$username=str_replace(/,.,$username);
$output = $this->command(. list \$username\ *);
for ($i=0; $i $splitfolder=split(\,$output[$i]);
$output[$i]=str_replace(.,/,$splitfolder[3]);
}
return $output;
}
function egetfolders($username){
$lastfolder=split(/,$username);
$position=count($lastfolder)-1;
$last=$lastfolder[$position];
$username=str_replace(/,.,$username);
$output = $this->command(. list \$username\ *);
for ($i=0; $i $splitfolder=split(\,$output[$i]);
$currentfolder=split(\.,$splitfolder[3]);
$current=$currentfolder[$position];
// echo
folder:($) currentfolder:($splitfolder[3]) current:($current) last:($last) position:($position)
;
if (strcmp($current,$last)==0){
$newoutput[$i]=str_replace(.,/,$splitfolder[3]);
}
}
return $newoutput;
}
/*
#
# folder-output functions
#
*/
function generatefolderlist($folder_array, $username)
{
print ;
for ($l=0; $l{
echo urlencode($username),
&folder=,
urlencode($folder_array[$l]),
\>/$folder_array[$l]
\n;
};
print
;
}
function getusers($char) {
$users = array();
$this->imap_login();
$output=$this->getfolders(user.$char);
$this->imap_logout();
$j = 0;
$prev = 0;
for ($i=0; $i $username = split(/, $output[$i],-1);
$this->debug(($username[1]),
$users[$prev]));
if ((isset($username)) && (isset($users))) {
if (strcmp($username[1], $users[$prev])) {
$users[$j] = $username[1];
$j++;
}
}
if ($j != 0) { $prev = $j - 1; }
}
return $users;
}
function debug($message) {
// echo $message
;
}
} //klassen ende
?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录