php and cyrus

Kendrick Vargas ken at hudat.com
Mon Nov 25 21:47:35 EST 2002


Here's a small attachment you can use as an include file. You can use it 
as such from PHP:

$server = new IMAP_Server();
$server->createMailbox("user.whatever");
$server->deleteMailbox("user.whatever");

Both functions return true or false on success or failure (respectively). 
You can edit the file and set the proper variables, or you can pass them 
in when you create the object.
			-peace

On Mon, 25 Nov 2002, Gregory Chagnon wrote:

> Hi-
> I tried something like that before, but I get this error:
> 
> Warning: Couldn't open stream {192.168.1.200} in 
> /var/www/html/user/mail2.php on line 7
> 
> Any ideas?  Thanks all.
> -Greg
> 
> 
> 
> 
> 
> 
> >From: Veigar Freyr Jökulsson <veigar at tviund.is>
> >To: "Gregory Chagnon" 
> ><gregchagnon at hotmail.com>,<info-cyrus at lists.andrew.cmu.edu>
> >Subject: Re: php and cyrus
> >Date: Tue, 26 Nov 2002 00:40:42 -0000
> >
> > > Hi-
> > > Does anyone know if there is a way to create a cyrus user and mailboxes
> > > through php?  I've looked around at imap_createmailbox() but am unlcear 
> >on
> > > how to use it.  Thanks!!
> > > -Greg
> > >
> >
> >You could do something like this, $uid is the login name you want to 
> >create:
> >
> >         $IMAPhost = "123.123.123.123";
> >         $IMAPquota = "10240",
> >         $username = "cyrus";
> >         $password = "password";
> >         $imapconn = imap_open("{" . $IMAPhost . "}", "$username",
> >"$password",OP_HALFOPEN);
> >         if ($imapconn) {
> >                 imap_createmailbox($imapconn, "{" . $IMAPhost .
> >"}user.".$uid ) or die("can't connect: ".imap_last_error());
> >                 imap_set_quota($imapconn, "user.$uid", $IMAPquota) or
> >die("can't connect: ".imap_last_error());
> >         imap_close($imapconn);
> >         }
> >
> >
> >--
> >Veigar Freyr Jökulsson
> >veigar at tviund.is
> 
> 
> _________________________________________________________________
> Tired of spam? Get advanced junk mail protection with MSN 8. 
> http://join.msn.com/?page=features/junkmail
> 
> 

-- 
Let he who is without clue kiss my ass
-------------- next part --------------
<?PHP

class IMAP_Server {

   # The following is the imap stream. 
   var $stream = false;

   var $imap = array("host" => "mail.hudat.com",
                     "port" => "143",
                     "user" => "cyrus",
                     "pass" => "cyradm");

   var $Error  = "";

   function IMAP_Server ($imap_spec = "") {

      if ( (! empty($imap_spec)) && is_array($imap_spec) ) {
         $this->imap = $imap_spec;
      }

      # First thing we do is open up our stream...
      $this->stream = imap_open("{" . $this->imap["host"] . ":" . $this->imap["port"] . "}", $this->imap["user"], $this->imap["pass"], OP_HALFOPEN);

      if ( $this->stream !== false ) {
         return(true);
      } else {
         $this->Error = imap_last_error();
         return(false);
      }

   }

   function createMailbox ($mailbox) {

      $existing_boxes = imap_listmailbox($this->stream, "{" . $this->imap["host"] . ":" . $this->imap["port"] . "}", imap_utf7_encode($mailbox));
      if ( ! empty($existing_boxes) ) {
         $this->Error = "createMailbox(): mailbox allready exists.";
         return(false);
      }

      if ( imap_createmailbox($this->stream, imap_utf7_encode("{" . $this->imap["host"] . ":" . $this->imap["port"] . "}" . $mailbox)) ) {
         return(true);
      } else {
         $this->Error = imap_last_error();
         return(false);
      }

   }

   function deleteMailbox ($mailbox) {

      $existing_boxes = imap_listmailbox($this->stream, "{" . $this->imap["host"] . ":" . $this->imap["port"] . "}", imap_utf7_encode($mailbox));
      if ( empty($existing_boxes) ) {
         $this->Error = "deleteMailbox(): mailbox does not exist.";
         return(false);
      }

      # Now we add the delete ACL to cyrus feature...
      if ( ! imap_setacl($this->stream, imap_utf7_encode($mailbox), $this->imap["user"], "cd") ) {
         $this->Error = imap_last_error();
         return(false);
      }

      if ( imap_deletemailbox($this->stream, imap_utf7_encode("{" . $this->imap["host"] . ":" . $this->imap["port"] . "}" . $mailbox)) ) {
         return(true);
      } else {
         $this->Error = imap_last_error();
         return(false);
      }

   }

}

?>


More information about the Info-cyrus mailing list