Search The ForumSearch   RegisterRegister  LoginLogin

AfterLogic WebMail Pro

 AfterLogic Forum : AfterLogic WebMail Pro
Subject Topic: Automatic Mail Check Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
sublimin0
Newbie
Newbie


Joined: 14 May 2008
Online Status: Offline
Posts: 3
Posted: 14 May 2008 at 8:44am | IP Logged Quote sublimin0

Post Preview
Close Window
Hello,

I am looking to create a cron job that logs a user in and synchronises their folders. Here is a code snippet:

foreach ( $_account_ids as $ID ) {
     $client_id = $client_domain;
      
     $_SESSION['admin_id'] = 2;
     $_SESSION['client_id'] = $client_domain;
      
     require_once(WM_ROOTPATH.'inte gr.php');
     $Integr = new CIntegration();
      
     $query = mysqli_query($DB_LINK, "SELECT * FROM mailBee_accounts WHERE account_id = '$ID'");
     $account = mysqli_fetch_assoc($query);
     if (! $Integr->UserExists($account['account_email'], $account['account_email'])) {
            die("user $ID doesn't exists in $client_db");
     }
     $Integr->UserLoginByEmail($ account['account_email'], $account['account_email'], START_PAGE_IS_MAILBOX, $account['account_password']);
      
     require_once(WM_ROOTPATH.'comm on/inc_constants.php');
     require_once(WM_ROOTPATH.'clas s_mailprocessor.php');
     require_once(WM_ROOTPATH.'clas s_settings.php');
      
     $settings =& Settings::CreateInstance();
     if (!$settings->IncludeLang()) {
            die("failed to include language for $client_db");
     }
      
     $account = &Account::LoadFromDb($ID);
     $processor = &new MailProcessor($account);
     $folders = &$processor->GetFolders();
      
     if (! $processor->Synchronize($folders)) {
            die("failed to sync folders for $ID at $client_db\n" . getGlobalError() . "\n");
     }
                  
     unset($folders);
     unset($processor);
     unset($account);
     unset($settings);
}

Although I don't think I need to $Integr->UserLoginByEmail I am including it until I have debugged this script. Currently the error I get is this (I have added a stack trace):

File: /web_path/mailBee/class_commandcreator.php Line: 1892
array_map(): Argument #2 should be an array
Error No: 2
#0 [internal function]: errorHandler(2, 'array_map(): Ar...', '/var/www/vhosts...', 1892, Array)
#1 /web_path/mailBee/class_commandcreator.php(1892): array_map(Array, 'UID2412-1195828...')
#2 /web_path/mailBee/class_dbstorage.php(2511): CommandCreator->DeleteReadsRecordsByUid(6, 'UID2412-1195828...')
#3 /web_path/mailBee/class_pop3storage.php(247): DbStorage->DeleteReadsRecordsByUids(Array)
#4 /web_path/mailBee/class_pop3storage.php(126): Pop3Storage->_synchronizeFolderWithOpenDbConnection(Objec t(Folder), Object(MySqlStorage))
#5 /web_path/mailBee/class_mailprocessor.php(73): Pop3Storage->Synchronize(Object(FolderCollection))
#6 /root/pm_ckmail.php(193): MailProcessor->Synchronize(Object(FolderCollection))
#7 {main}

Warning: array_map(): Argument #2 should be an array in /web_path/mailBee/class_commandcreator.php on line 1892

The problem appears to be that array_map in class_commandcreator @ 1892 is being passed a string instead of an array

$uids = array_map(array(&$this, '_escapeString'), $uids);

the $uids variable is passed from class_dbstorage, where the array is imploded to a string

$strUids = implode(",", $uids);

that string contains many many UIDs:

UID2412-1195828556,UID2413-1195828556,UID2414-1195828556,UID 2415-1195828556,UID2416-1195828556,UID2417-1195828556,UID241 8-1195828556,UID2419-1195828556,UID2420-1195828556,UID2421-1 195828556,UID2422-1195828556,UID2423-1195828556,UID2424-1195 828556,UID2425-1195828556,UID2426-1195828556,UID2427-1195828 556,UID2428-1195828556 etc.

So my question is: is this the correct way to attempt an auto mail check? I just want to copy the messages from the mail server to the database every 5 minutes or so. I am not sure if this warning affects my script, but the mails are not getting into the database. Any feedback appreciated :)

Many thanks,


Andrew Martin

Close Window

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide
Back to Top View sublimin0's Profile Search for other posts by sublimin0
 
Andrew
AfterLogic Support
AfterLogic Support


Joined: 28 April 2006
Location: United States
Online Status: Offline
Posts: 1189
Posted: 15 May 2008 at 4:34am | IP Logged Quote Andrew

File class_commandcreator.php, modify the function as follows:

Code:
/**
* @param int $accountId
* @param array $uids
* @return string
*/
function DeleteReadsRecordsByUid($accountId, $uids)
{
        $uids = array_map(array(&$this, '_escapeString'), $uids);
        $uids = implode(',', $uids);
        $sql = 'DELETE FROM %sawm_reads WHERE id_acct = %d AND str_uid IN (\'%s\')';

        return sprintf($sql, $this->_settings->DbPrefix, $accountId, $uids);
}



File class_dbstorage.php, modify the function as follows:

Code:
/**
* @param array $uids
* @return bool
*/
function DeleteReadsRecordsByUids($uids)
{
        return  $this->_dbConnection->Execute($this->_commandCreato r->DeleteReadsRecordsByUid($this->Account->Id,  $uids));
}


Below is the code of working cron job for checking mail for multiple accounts:

Code:
<?php
       
        if (!defined('WM_ROOTPATH')) define('WM_ROOTPATH', (dirname(__FILE__).'/'));

        require_once(WM_ROOTPATH.'common/inc_constants.php');
        require_once(WM_ROOTPATH.'class_mailprocessor.php');
        require_once(WM_ROOTPATH.'class_settings.php');
        require_once(WM_ROOTPATH.'integr.php');

       
        $client_db = 2;
        $_account_ids = array(2, 3);
       
        foreach ($_account_ids as $ID)
        {
                 $Integr = new CIntegration();
                 $Integr->GetAccountById($ID);
                 
                 if (!$Integr->Account)
                 {
                           $errorString = $Integr->GetErrorString();
                           if ($errorString)
                           {
                                   die($errorString);
                           }
                           else
                           {
                                   die("user $ID doesn't exist in $client_db");
                           }
                 }

                  //$Integr->UserLoginByEmail($Integr->Account->Email , $Integr->Account->MailIncLogin, START_PAGE_IS_MAILBOX, $Integr->Account->MailIncPassword);
                 
                 $settings =& Settings::CreateInstance();
                 if (!$settings || !$settings->isLoad || !$settings->IncludeLang())
                 {
                           die("failed to load settings or include language for $client_db");
                 }
                 
                 $processor = &new MailProcessor($Integr->Account);
                 $folders = &$processor->GetFolders();
                 
                 if (!$processor->Synchronize($folders))
                 {
                           die("failed to sync folders for $ID at $client_db\n" . getGlobalError() . "\n");
                 }

                 unset($folders);
                 unset($processor);
                 unset($account);
                 unset($settings);
        }
?>


Best regards,
Andrew
Back to Top View Andrew's Profile Search for other posts by Andrew
 
sublimin0
Newbie
Newbie


Joined: 14 May 2008
Online Status: Offline
Posts: 3
Posted: 16 May 2008 at 2:33am | IP Logged Quote sublimin0

Andrew,

Thanks for your helpful reply, the script now works for the first 4 mailboxes I attempt to query. After the 4th mailbox (regardless of which boxes I query first) all requests return:

Can't connect to POP3 server, check POP3 server settings.

It appears that all variables are unset/reinitialised so I don't think it's a variable conflict. I have tried putting a delay between mailbox sync attempts/reattempts, and also:

if ( !$processor->Disconnect) {
              echo ("failed to disconnect for $client_db | " . getGlobalError() . "\n");
}


However I cannot disconnect from any of the requests (even the successful ones) which leads me to think that this disconnect is already performed - however I cannot find the location of this call. I have also attempted to increase the debugging info in libs/class_pop3.php:

    function close(){

        $response = "";
        $cmd = "QUIT";
        if(!$this->_logging($cmd)) {
             setGlobalError('!$this->_logging($cmd)');
                return FALSE;
        }
           if(!$this->_putline($cmd)) {
               setGlobalError('!$this->_putline( $cmd)');
                return FALSE;
           }
        if($this->state == "AUTHORIZATION"){
             $this->state = "DISCONNECTED";
        }elseif($this->state == "TRANSACTION"){
             $this->state = "UPDATE";
        }

        $response = $this->_getnextstring();

        if(!$this->_logging($response)) {
               setGlobalError('!$this->_logging( $response)');
                return FALSE;
           }
          
           if(substr($response,0,1) != "+"){
             $this->error = "POP3 close() - Error: ".$response;
             setGlobalError($this->error);
             return FALSE;
        }
        $this->socket = FALSE;

        $this->_cleanup();

        return TRUE;
    }

I still get no debug info back from getGlobalError().

We are using qmail 1.03.

Many thanks,


Andrew Martin
Back to Top View sublimin0's Profile Search for other posts by sublimin0
 
Andrew
AfterLogic Support
AfterLogic Support


Joined: 28 April 2006
Location: United States
Online Status: Offline
Posts: 1189
Posted: 16 May 2008 at 3:09am | IP Logged Quote Andrew

Could you please enable logging in WebMail Admin Console (mailadm) / Debug Settings / Enable Logging, reproduce the issue and provide us with the log file for examination?

Best regards,
Andrew
Back to Top View Andrew's Profile Search for other posts by Andrew
 
sublimin0
Newbie
Newbie


Joined: 14 May 2008
Online Status: Offline
Posts: 3
Posted: 16 May 2008 at 4:56am | IP Logged Quote sublimin0

Hello,

The log file for this event is almost 5000 lines - can I send this privately please?

Thanks,


Andrew
Back to Top View sublimin0's Profile Search for other posts by sublimin0
 
Andrew
AfterLogic Support
AfterLogic Support


Joined: 28 April 2006
Location: United States
Online Status: Offline
Posts: 1189
Posted: 16 May 2008 at 5:13am | IP Logged Quote Andrew

Sure.

Best regards,
Andrew
Back to Top View Andrew's Profile Search for other posts by Andrew
 
rob
Groupie
Groupie


Joined: 03 September 2008
Location: Canada
Online Status: Offline
Posts: 60
Posted: 03 September 2008 at 2:19pm | IP Logged Quote rob

what was the result of this thread? Did the auto-check function end up performing what it needs to do?

We are also in need of automating checking for email across every pop account configured.

Please let me know.
Back to Top View rob's Profile Search for other posts by rob
 
Andrew
AfterLogic Support
AfterLogic Support


Joined: 28 April 2006
Location: United States
Online Status: Offline
Posts: 1189
Posted: 04 September 2008 at 1:41am | IP Logged Quote Andrew

Quote:
Did the auto-check function end up performing what it needs to do?


Yes.

To learn more, please check you mail, we've sent you more detailed reply.

Best regards,
Andrew
Back to Top View Andrew's Profile Search for other posts by Andrew
 
QAC
Newbie
Newbie


Joined: 05 September 2008
Location: Australia
Online Status: Offline
Posts: 2
Posted: 05 September 2008 at 12:19am | IP Logged Quote QAC

Also keen to know what is required to automate this process. Please advise.
Back to Top View QAC's Profile Search for other posts by QAC
 
Andrew
AfterLogic Support
AfterLogic Support


Joined: 28 April 2006
Location: United States
Online Status: Offline
Posts: 1189
Posted: 05 September 2008 at 12:34am | IP Logged Quote Andrew

In case of Unix/Linux system, this can be done via Cron daemon.

For Windows platform: Start / Programs / Accessories / System Tools / Scheduled Tasks.

You can learn how to run a PHP script without a web server here.

Best regards,
Andrew
Back to Top View Andrew's Profile Search for other posts by Andrew
 
ash.eldritch
Newbie
Newbie


Joined: 02 January 2009
Location: Japan
Online Status: Offline
Posts: 9
Posted: 07 January 2009 at 7:19pm | IP Logged Quote ash.eldritch

Has anyone converted this code into .NET? I'd appreciate it being posted if so. Otherwise I'll have to do it again myself.

While this script can be used to retrieve messages from the mail server and drop them into the database, the additional piece of the puzzle is to have the mail client automatically poll the server for new mail periodically.

The easiest way to do this is to add the following line to the bottom of webmail.aspx, right after the WebMail.DataSource.Get...

setInterval(function() { WebMail.Screens[1]._checkMail.Start() }, 180000);

...This will check every three minutes. It's a bit of a hack, as there may be no guarantee that Screens[1] will always be the right screen, so it should find the right screen more intelligently. The interval should also be reset if the user checks mail manually, which would involve modifying the BuildToolBar function in screen.messages-list.js. If anyone gets around to doing this or any other improvements please post them here.

One other consideration is that this may well go off and your mail servers, which you don't need and probably don't want if you're running a scheduled/cron job in the background to do that anyway. I'm not sure how you would turn that off. Anyone?

Cheers
Ash
Back to Top View ash.eldritch's Profile Search for other posts by ash.eldritch
 
roberto
Newbie
Newbie


Joined: 03 January 2010
Online Status: Offline
Posts: 1
Posted: 03 January 2010 at 5:09am | IP Logged Quote roberto

Hi,

The script doesn't work with new 4.8.x version.

How to improve it?

Best regards
Back to Top View roberto's Profile Search for other posts by roberto
 

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