Search The ForumSearch   RegisterRegister  LoginLogin

AfterLogic WebMail Pro

 AfterLogic Forum : AfterLogic WebMail Pro
Subject Topic: How to count unread emails PHP Api? Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
glenelkins
Newbie
Newbie


Joined: 25 August 2020
Location: United Kingdom
Online Status: Offline
Posts: 33
Posted: 16 October 2020 at 3:28am | IP Logged Quote glenelkins

The documentation on the PHP api is not very clear, i'm looking for a way to be able to get a count of all unread emails across all accounts if logging in as super admin.
Back to Top View glenelkins's Profile Search for other posts by glenelkins
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6038
Posted: 16 October 2020 at 4:44am | IP Logged Quote Igor

Something like this should do it.

Code:
include './system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreModule = \Aurora\System\Api::GetModule("Core");
$oMailModule = \Aurora\System\Api::GetModule("Mail");

$ul = $oCoreModule->getUserList();
$ulItems = $ul["Items"];

foreach ($ulItems as $key=>$item) {
     $sEmail = $item["PublicId"];
     $oUser = $oCoreModule->GetUserByPublicId($sEmail);
     $iUserId = $oUser->EntityId;
     $oAccount = $oCoreModule->GetAccountUsedToAuthorize($sEmail);
     $iAcctId = $oAccount->EntityId;
     $aFolders = $oMailModule->GetRelevantFoldersInformation( $iAcctId, array("INBOX"), true);
     echo "<p><b>".$sEmail."</b><br />Total: ".$aFolders["Counts"]["INBOX"][0]."<br />Unread: ".$aFolders["Counts"]["INBOX"][1]."</p>";
}


Hope this helps. We'll probably add this sample to Developer's Guide section of the documentation.

UPDATE 22 October 2020:

This sample is now available in the product documentation at:

Getting number of unread mails in user accounts

--
Regards,
Igor, Afterlogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
glenelkins
Newbie
Newbie


Joined: 25 August 2020
Location: United Kingdom
Online Status: Offline
Posts: 33
Posted: 29 October 2020 at 1:57pm | IP Logged Quote glenelkins

Is there a way to get the count after a specific date? So ignore all emails before that date?

Another quick question . When pulling the emails, say the account details become invalid for whatever reson, in the code above is there a way to gracefully catch an auth error? Does any of it throw an exception?
Back to Top View glenelkins's Profile Search for other posts by glenelkins
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6038
Posted: 30 October 2020 at 12:26am | IP Logged Quote Igor

Quote:
Is there a way to get the count after a specific date? So ignore all emails before that date?


In IMAP, that can only be done by running Search, our PHP API has GetMessages method for that, its main code is found in modules/Mail/Managers/Main/Manager.php file, getMessageList method.

Quote:
Another quick question . When pulling the emails, say the account details become invalid for whatever reson, in the code above is there a way to gracefully catch an auth error? Does any of it throw an exception?


Indeed there would be an exception, you can catch it as follows:

Code:
...
$iAcctId = $oAccount->EntityId;
try {
     $aFolders = $oMailModule->GetRelevantFoldersInformation( $iAcctId, array("INBOX"), true);
     echo "<p><b>".$sEmail."</b><br />Total: ".$aFolders["Counts"]["INBOX"][0]."<br />Unread: ".$aFolders["Counts"]["INBOX"][1]."</p>";
}
catch(Exception $e)
{
     echo "<p><b>".$sEmail."</b> could not be accessed.";
}
...


--
Regards,
Igor, Afterlogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
glenelkins
Newbie
Newbie


Joined: 25 August 2020
Location: United Kingdom
Online Status: Offline
Posts: 33
Posted: 07 November 2020 at 1:29am | IP Logged Quote glenelkins

Igor wrote:
Quote:
Is there a way to get the count after a specific date? So ignore all emails before that date?


In IMAP, that can only be done by running Search, our PHP API has GetMessages method for that, its main code is found in modules/Mail/Managers/Main/Manager.php file, getMessageList method.

Quote:
Another quick question . When pulling the emails, say the account details become invalid for whatever reson, in the code above is there a way to gracefully catch an auth error? Does any of it throw an exception?


Indeed there would be an exception, you can catch it as follows:

Code:
...
$iAcctId = $oAccount->EntityId;
try {
     $aFolders = $oMailModule->GetRelevantFoldersInformation( $iAcctId, array("INBOX"), true);
     echo "<p><b>".$sEmail."</b><br />Total: ".$aFolders["Counts"]["INBOX"][0]."<br />Unread: ".$aFolders["Counts"]["INBOX"][1]."</p>";
}
catch(Exception $e)
{
     echo "<p><b>".$sEmail."</b> could not be accessed.";
}
...


--
Regards,
Igor, Afterlogic Support


Wow ok, this would be insanely slow though having to pull all messages snd seeing if they’re read or not in a loop.
Back to Top View glenelkins's Profile Search for other posts by glenelkins
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6038
Posted: 08 November 2020 at 10:45pm | IP Logged Quote Igor

Fetching information on messages doesn't require downloading messages themselves, message flags contain all the sufficient information and it's just a few bytes per message.

--
Regards,
Igor, Aterlogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
glenelkins
Newbie
Newbie


Joined: 25 August 2020
Location: United Kingdom
Online Status: Offline
Posts: 33
Posted: 09 November 2020 at 3:12am | IP Logged Quote glenelkins

Igor wrote:
Fetching information on messages doesn't require downloading messages themselves, message flags contain all the sufficient information and it's just a few bytes per message.

--
Regards,
Igor, Aterlogic Support



Do you have some example code? The documents are a little difficult to follow. Baciall i need to turn the following code into looping the messages and checking if the date received > X date and unread.


\Aurora\System\Api::Init(true);

            /** @var \Aurora\Modules\Core\Module $apiCore */
            $apiCore = Api::GetModuleDecorator('Core');

            /** @var \Aurora\Modules\Mail\Module $apiMail */
            $apiMail = Api::GetModuleDecorator('Mail');


            /** @var EmailAccount $emailAccount */
            foreach ($accounts as $emailAccount) {

               $oAccount = $apiCore->GetAccountUsedToAuthorize($emailAccount->getEmail());
               if($oAccount && isset($oAccount->EntityId)) {
                    $oAccountId = $oAccount->EntityId;
                    try {
                        $folders = $apiMail->GetRelevantFoldersInformation($oAccountId, ['INBOX'], true);
                        
                        if (isset($folders['Counts']['INBOX']) && isset($folders['Counts']['INBOX'][1])) {
                            $totalUnreadMail = $totalUnreadMail + (int)$folders['Counts']['INBOX'][1];
                        }
                    }catch (\Exception $exception){
                        // do nothing
                    }
               }


            }



I've got it to the point where i can loop over the Message objects but i don't see anything in your documentation that shows a method to get whether the message is seen or not? https://afterlogic.com/webmail-pro-8-api/class-Aurora.Modules.Mail.Classes.Message.html
Back to Top View glenelkins's Profile Search for other posts by glenelkins
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6038
Posted: 09 November 2020 at 3:32am | IP Logged Quote Igor

I really don't think looping through messages is required. Instead, I would run IMAP search looking for Unseen messages after a specific date. I'll see if we may be able to provide such a sample sometime soon.

--
Regards,
Igor, Afterlogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 
Igor
AfterLogic Support
AfterLogic Support


Joined: 24 June 2008
Location: United States
Online Status: Offline
Posts: 6038
Posted: 09 November 2020 at 5:12am | IP Logged Quote Igor

For a single account, you'll need to do the following:

Code:
$sEmail = "user@domain.com";

include './system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreModule = \Aurora\System\Api::GetModule("Core");
$oMailModule = \Aurora\System\Api::GetModule("Mail");

$oAccount = $oCoreModule->GetAccountUsedToAuthorize($sEmail);
$rSearch = $oMailModule->GetMessagesInfo($oAccount->EntityId, "INBOX", "has:unseen date:2020.11.01/");
echo count($rSearch);


where "has:unseen date:2020.11.01/" means searching for unread messages starting from 1 Nov 2020.

Hope it helps.

--
Regards,
Igor, Afterlogic Support
Back to Top View Igor's Profile Search for other posts by Igor
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide