WebMail Pro 7 documentation

Getting number of all or unread mails

Aside from being able to log into WebMail Pro account programmatically, you might want to provide information on WebMail account somewhere on your web site page - for instance, total number of mails in Inbox as well as number of unread messages. Current version of WebMail Pro offers a very simple way to do that, we'll just need to extend the above example a bit: first we log user in, to get access to their email account - and then we fetch the information we need.

<?php

	include_once __DIR__.'/../libraries/afterlogic/api.php';
	if (class_exists('CApi') && CApi::IsValid())
	{
		$sEmail = 'user@domain.com';
		$sPassword = '12345';
		$sFolder = 'INBOX';

		try
		{
			$oApiIntegratorManager = CApi::Manager('integrator');
			$oAccount = $oApiIntegratorManager->LoginToAccount($sEmail, $sPassword);
			if ($oAccount)
			{
				$oApiMailManager = CApi::Manager('mail');
				$aData = $oApiMailManager->getFolderInformation($oAccount, $sFolder);

				echo '<b>'.$oAccount->Email.':</b><br />';
				if (is_array($aData) && 4 === count($aData))
				{
					echo '<pre>';
					echo 'Folder:   '.$sFolder."\n";
					echo 'Count:    '.$aData[0]."\n";
					echo 'Unread:   '.$aData[1]."\n";
					echo 'UidNext:  '.$aData[2]."\n";
					echo 'Hash:     '.$aData[3];
					echo '</pre>';
				}
			}
			else
			{
				echo $oApiIntegratorManager->GetLastErrorMessage();
			}
		}
		catch (Exception $oException)
		{
			echo $oException->getMessage();
		}
	}
	else
	{
		echo 'WebMail API isn\'t available';
	}

As you can see, the code requires a folder name along with account credentials, so you can use the same approach for other folder, assuming you're aware of its exact IMAP name. And aside from total/unread message count, FolderCounts method of mail manager returns a few more details on the folder, which might come in handy as well.

The above sample only returns information on some particular folder. If you need to obtain data on all the folders and subfolders available in IMAP account, you can use the following sample for it:

<?php

include_once __DIR__.'/../libraries/afterlogic/api.php';
if (class_exists('CApi') && CApi::IsValid())
{
    $sEmail = 'user@domain.com';
    $sPassword = 'YourPassWord';

    try
    {
	$oApiIntegratorManager = CApi::Manager('integrator');
	$oAccount = $oApiIntegratorManager->LoginToAccount($sEmail, $sPassword);
	if ($oAccount)
	{
	    echo '<b>'.$oAccount->Email.':</b><br /><br />';

	    $oApiMailManager = CApi::Manager('mail');
	    $oFolders = $oApiMailManager->getFolders($oAccount);
	    $aFolders = array();
	    $oFolders->foreachWithSubFolders(function ($oFolder) use (&$aFolders) {
		    $aFolders[] = $oFolder->getFullName();
		}
	    );

	    foreach ($aFolders as $sFolder) 
	    {
		$aData = $oApiMailManager->getFolderInformation($oAccount, $sFolder);

		if (is_array($aData) && 4 === count($aData))
		{
		    echo 'Folder: <b>'.$sFolder."</b><br />";
		    echo 'Unread: '.$aData[1].' of '.$aData[0].'<br /><br />';
		}
	    }
	}
	else
	{
		echo $oApiIntegratorManager->GetLastErrorMessage();
	}
    }
    catch (Exception $oException)
    {
	echo $oException->getMessage();
    }
}
else
{
    echo 'WebMail API isn\'t available';
}