Mail Class PHP
Views:
This class was written for PHP 4. Users were spread over two different, independent imap servers and this code used the Edirectory PHP Class to figure out which one their mails were stored on.
<?php
/* Copyriht Kieran Whitbread 2005 */
/* This code is released under the following license: http://opensource.org/licenses/lgpl-2.1.php GNU Lesser General Public License */
require_once 'edir.classes.inc.php';
class UserMail
{
var $eDirectorySearch = NULL; // an eDirecorySearch object
var $ImapServer = NULL; // an ImapServer object
var $MailServer = NULL; // The mail server this user's account belongs to
var $MailServerDetails = array(
'groupwise' => array(
'host' => 'polymorph.varndean.ac.uk',
'imapurl' => '{polymorph.varndean.ac.uk/novalidate-cert/norsh}',
'webmailurl' => 'https://staff.varndean.ac.uk/gw/webacc',
),
'netmail' => array(
'host' => 'mail.varndean.ac.uk',
'imapurl' => '{mail.varndean.ac.uk/novalidate-cert/norsh}',
'webmailurl' => 'https://mail.varndean.ac.uk/',
),
);
var $UserAttributes = array('mail', 'cn', 'ngwobjectid');
var $Username = NULL;
var $Password = NULL;
function __construct($Username, $Password)
{
$this->SetUsername($Username);
$this->SetPassword($Password);
$this->eDirectorySearch = new eDirectorySearch('cn='.$this->Username, 'o=vsfc', $this->UserAttributes);
$this->eDirectorySearch->Search();
$this->eDirectorySearch->NextResult();
if ($this->eDirectorySearch->GetAttribute('ngwobjectid'))
{
$this->MailServer = 'groupwise';
}
else
{
$this->MailServer = 'netmail';
}
$this->ImapServer = new ImapServer($this->MailServerDetails[$this->MailServer]['imapurl']);
$this->ImapServer->SetUsername($this->Username);
$this->ImapServer->SetPassword($this->Password);
if ($this->MailServer == 'groupwise')
{
$this->ImapServer->AddIgnoredFolder('Calendar');
$this->ImapServer->AddIgnoredFolder('Checklist');
}
$this->ImapServer->Connect();
}
function __destruct()
{
unset($this->ImapServer);
unset($this->eDirectorySearch);
}
function GetFolders()
{
return $this->ImapServer->GetFolders();
}
function GetUnreadMailCount()
{
return $this->ImapServer->GetUnreadMailCount();
}
function GetWebmailUrl()
{
return $this->MailServerDetails[$this->MailServer]['webmailurl'];
}
function SetPassword($Password)
{
$this->Password = $Password;
}
function SetUsername($Username)
{
$this->Username = $Username;
}
function UserMail($Username, $Password)
{
$this->__construct($Username, $Password);
}
}
class ImapServer
{
var $Connection = NULL;
var $ConnectionOptions = OP_HALFOPEN;
var $Folders = array();
var $IgnoredFolders = array('Trash', 'SPAM', 'Sent', 'Drafts');
var $IgnoreFolders = true;
var $Password = NULL;
var $TimeOut = NULL;
var $Url = NULL;
var $Username = NULL;
function __construct($ConnectionUrl)
{
$this->Url = $ConnectionUrl;
}
function __destruct()
{
$this->Close();
}
function AddIgnoredFolder($Folder)
{
if(is_string($Folder) || is_numeric($Folder))
{
$this->IgnoredFolders[] = $Folder;
return true;
}
else
{
return false;
}
}
function Close()
{
imap_close($this->Connection);
}
function Connect()
{
$this->Connection = imap_open($this->Url, $this->Username, $this->Password, $this->ConnectionOptions);
if ($this->Connection) return true;
else return false;
}
function FindFolders()
{
if ($this->Connection)
{
$this->Folders = imap_list($this->Connection, $this->Url, '*');
}
}
function GetErrorMessage()
{
return imap_last_error();
}
function GetFolders()
{
if (!$this->Folders) $this->FindFolders();
return $this->Folders;
}
function GetIgnoreFolders()
{
return $this->IgnoreFolders;
}
function GetTimeOut()
{
return $this->TimeOut;
}
function GetUnreadMailCount()
{
if ($this->Connection)
{
if (!$this->Folders) $this->FindFolders();
if (!$this->Folders)
{
error_log(get_class($this).'->GetUnreadMailCount() - file '.__FILE__.
', line '.__LINE__.': Unable to get a list of folders to search through.'
);
return false;
}
$count = 0;
foreach ($this->Folders as $Folder)
{
if (!$this->IgnoreFolders
|| ($this->IgnoreFolders
&& !in_array(preg_replace('/\{.*\}(.*)/', '$1', $Folder), $this->IgnoredFolders, false)))
{
if ($status = imap_status($this->Connection, $Folder, SA_UNSEEN))
{
$count = $count + $status->unseen;
}
else
{
error_log(get_class($this).'->GetUnreadMailCount() - file '.__FILE__.
', line '.__LINE__.': Error while parsing mailbox (error occured when accessing the '.
$Folder,' folder).'
);
return false;
}
}
}
return $count;
}
}
function GetUrl()
{
return $this->Url;
}
function GetUsername()
{
return $this->Username;
}
function ImapServer($ConnectionUrl)
{
$this->__construct($ConnectionUrl);
}
function SetConnectionOptions($Options)
{
$this->ConnectionOptions = $Options;
}
function SetIgnoredFolders($Folders)
{
if (is_array($Folders))
{
$this->IgnoredFolders = $Folders;
return true;
}
else
{
return false;
}
}
function SetIgnoreFolders($Status)
{
if (is_bool($Status))
{
$this->IgnoreFolders = $Status;
return true;
}
else
{
return false;
}
}
function SetPassword($Password)
{
$this->Password = $Password;
}
function SetTimeOut($TimeOut)
{
$this->TimeOut = $TimeOut;
imap_timeout(1, $this->TimeOut); //Open
imap_timeout(2, $this->TimeOut); //Read
imap_timeout(3, $this->TimeOut); //Write
//(Not Implemented in PHP's Imap Extention??) imap_timeout(4, $this->TimeOut); //Close
}
function SetUrl($ConnectionUrl)
{
$this->Connection = NULL;
$this->Url = $ConnectionUrl;
}
function SetUsername($Username)
{
$this->Username = $Username;
}
}
?>
