From f1287578f3eb79c46c2aef8b55cc2b85c7115d72 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 9 Nov 2018 18:18:17 +0100 Subject: [PATCH] Work on emailcollector --- htdocs/admin/emailcollector_card.php | 64 +++++++++++++------ .../class/emailcollector.class.php | 58 ++++++++++------- .../install/mysql/migration/8.0.0-9.0.0.sql | 1 + .../llx_emailcollector_emailcollector.sql | 1 + htdocs/langs/en_US/admin.lang | 4 +- 5 files changed, 84 insertions(+), 44 deletions(-) diff --git a/htdocs/admin/emailcollector_card.php b/htdocs/admin/emailcollector_card.php index 3280492701e..f9ba71cc858 100644 --- a/htdocs/admin/emailcollector_card.php +++ b/htdocs/admin/emailcollector_card.php @@ -103,25 +103,6 @@ if (empty($reshook)) include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php'; } - -if ($action == 'confirm_collect') -{ - dol_include_once('/emailcollector/class/emailcollector.class.php'); - - $res = $object->doCollect(); - - if ($res == 0) - { - setEventMessages($object->output, null, 'mesgs'); - } - else - { - setEventMessages($object->error, null, 'errors'); - } - - $action = ''; -} - if (GETPOST('addfilter','alpha')) { $emailcollectorfilter = new EmailCollectorFilter($db); @@ -190,6 +171,25 @@ if ($action == 'deleteoperation') } } +if ($action == 'confirm_collect') +{ + dol_include_once('/emailcollector/class/emailcollector.class.php'); + + $res = $object->doCollect(); + + if ($res == 0) + { + setEventMessages($object->output, null, 'mesgs'); + } + else + { + setEventMessages($object->error, null, 'errors'); + } + + $action = ''; +} + + /* @@ -373,7 +373,29 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea */ $morehtmlref .= ''; - dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref); + $morehtml = $langs->trans("NbOfEmailsInInbox").' : '; + + $sourcedir = $object->source_directory; + $targetdir = ($object->target_directory ? $object->target_directory : ''); // Can be '[Gmail]/Trash' or 'mytag' + + $connectstringserver = $object->getConnectStringIMAP(); + $connectstringsource = $connectstringserver.imap_utf7_encode($sourcedir); + $connectstringtarget = $connectstringserver.imap_utf7_encode($targetdir); + + $connection = imap_open($connectstringsource, $object->user, $object->password); + if (! $connection) + { + $morehtml .= 'Failed to open IMAP connection '.$connectstringsource; + } + else + { + //$morehtmlstatus .= imap_num_msg($connection).'
'; + $morehtml .= imap_num_msg($connection); + } + + imap_close($connection); + + dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref.'
'.$morehtml.'
', '', 0, '', '', 0, ''); print '
'; print '
'; @@ -474,7 +496,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea print ''; print '
'; - print '
'; + print '
'; // End
print '

'; diff --git a/htdocs/emailcollector/class/emailcollector.class.php b/htdocs/emailcollector/class/emailcollector.class.php index e4c0d4f588f..b68639ebf5e 100644 --- a/htdocs/emailcollector/class/emailcollector.class.php +++ b/htdocs/emailcollector/class/emailcollector.class.php @@ -90,7 +90,8 @@ class EmailCollector extends CommonObject //'actiontodo' => array('type'=>'varchar(255)', 'label'=>'ActionToDo', 'visible'=>1, 'enabled'=>1, 'position'=>106), 'target_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxTargetDirectory', 'visible'=>1, 'enabled'=>1, 'position'=>110, 'notnull'=>0, 'comment'=>"Where to store messages once processed"), 'datelastresult' => array('type'=>'datetime', 'label'=>'DateLastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>121, 'notnull'=>-1,), - 'lastresult' => array('type'=>'varchar(255)', 'label'=>'LastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>122, 'notnull'=>-1,), + 'codelastresult' => array('type'=>'varchar(16)', 'label'=>'CodeLastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>122, 'notnull'=>-1,), + 'lastresult' => array('type'=>'varchar(255)', 'label'=>'LastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>123, 'notnull'=>-1,), 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'visible'=>0, 'enabled'=>1, 'position'=>61, 'notnull'=>-1,), 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'visible'=>0, 'enabled'=>1, 'position'=>62, 'notnull'=>-1,), 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,), @@ -717,23 +718,16 @@ class EmailCollector extends CommonObject $this->fetchActions(); $sourcedir = $this->source_directory; - $targetdir = ($this->target_directory ? $server.$this->target_directory : ''); // Can be '[Gmail]/Trash' or 'mytag' + $targetdir = ($this->target_directory ? $this->target_directory : ''); // Can be '[Gmail]/Trash' or 'mytag' - // Connect to IMAP - $flags ='/service=imap'; // IMAP - $flags.='/ssl'; // '/tls' - $flags.='/novalidate-cert'; - //$flags.='/readonly'; - //$flags.='/debug'; - - $connectstringserver = '{'.$this->host.':993'.$flags.'}'; - $connectstring = $connectstringserver.imap_utf7_encode($sourcedir); + $connectstringserver = $this->getConnectStringIMAP(); + $connectstringsource = $connectstringserver.imap_utf7_encode($sourcedir); $connectstringtarget = $connectstringserver.imap_utf7_encode($targetdir); - $connection = imap_open($connectstring, $this->user, $this->password); + $connection = imap_open($connectstringsource, $this->user, $this->password); if (! $connection) { - $this->error = 'Failed to open IMAP connection '.$connectstring; + $this->error = 'Failed to open IMAP connection '.$connectstringsource; return -3; } @@ -743,23 +737,24 @@ class EmailCollector extends CommonObject { if (empty($rule['status'])) continue; - if ($rule['key'] == 'to') $search=($search?' ':'').'TO "'.str_replace('"', '', $rule['rulevalue']).'"'; - if ($rule['key'] == 'bcc') $search=($search?' ':'').'BCC'; - if ($rule['key'] == 'cc') $search=($search?' ':'').'CC'; - if ($rule['key'] == 'from') $search=($search?' ':'').'FROM "'.str_replace('"', '', $rule['rulevalue']).'"'; - if ($rule['key'] == 'subject') $search=($search?' ':'').'SUBJECT "'.str_replace('"', '', $rule['rulevalue']).'"'; - if ($rule['key'] == 'body') $search=($search?' ':'').'BODY "'.str_replace('"', '', $rule['rulevalue']).'"'; - if ($rule['key'] == 'seen') $search=($search?' ':'').'SEEN'; - if ($rule['key'] == 'unseen') $search=($search?' ':'').'UNSEEN'; + if ($rule['type'] == 'to') $search.=($search?' ':'').'TO "'.str_replace('"', '', $rule['rulevalue']).'"'; + if ($rule['type'] == 'bcc') $search.=($search?' ':'').'BCC'; + if ($rule['type'] == 'cc') $search.=($search?' ':'').'CC'; + if ($rule['type'] == 'from') $search.=($search?' ':'').'FROM "'.str_replace('"', '', $rule['rulevalue']).'"'; + if ($rule['type'] == 'subject') $search.=($search?' ':'').'SUBJECT "'.str_replace('"', '', $rule['rulevalue']).'"'; + if ($rule['type'] == 'body') $search.=($search?' ':'').'BODY "'.str_replace('"', '', $rule['rulevalue']).'"'; + if ($rule['type'] == 'seen') $search.=($search?' ':'').'SEEN'; + if ($rule['type'] == 'unseen') $search.=($search?' ':'').'UNSEEN'; } if (empty($targetdir)) // Use last date as filter if there is no targetdir defined. { $fromdate=0; - if ($this->datelastresult) $fromdate = $this->datelastresult; + if ($this->datelastresult && $this->codelastresult == 'OK') $fromdate = $this->datelastresult; if ($fromdate > 0) $search.=($search?' ':'').'SINCE '.dol_print_date($fromdate - 1,'dayhourrfc'); } dol_syslog("search string = ".$search); + //var_dump($search);exit; $nbemailprocessed=0; $nbactiondone=0; @@ -841,4 +836,23 @@ class EmailCollector extends CommonObject return $error; } + + /** + * Return the connectstring to use with IMAP connection function + * + * @return string + */ + function getConnectStringIMAP() + { + // Connect to IMAP + $flags ='/service=imap'; // IMAP + $flags.='/ssl'; // '/tls' + $flags.='/novalidate-cert'; + //$flags.='/readonly'; + //$flags.='/debug'; + + $connectstringserver = '{'.$this->host.':993'.$flags.'}'; + + return $connectstringserver; + } } diff --git a/htdocs/install/mysql/migration/8.0.0-9.0.0.sql b/htdocs/install/mysql/migration/8.0.0-9.0.0.sql index 05106e475cd..8f51d9f00c4 100644 --- a/htdocs/install/mysql/migration/8.0.0-9.0.0.sql +++ b/htdocs/install/mysql/migration/8.0.0-9.0.0.sql @@ -153,6 +153,7 @@ CREATE TABLE llx_emailcollector_emailcollector( source_directory varchar(255) NOT NULL, target_directory varchar(255), datelastresult datetime, + codelastresult varchar(16), lastresult varchar(255), note_public text, note_private text, diff --git a/htdocs/install/mysql/tables/llx_emailcollector_emailcollector.sql b/htdocs/install/mysql/tables/llx_emailcollector_emailcollector.sql index 2db5e693427..750c2b24e84 100644 --- a/htdocs/install/mysql/tables/llx_emailcollector_emailcollector.sql +++ b/htdocs/install/mysql/tables/llx_emailcollector_emailcollector.sql @@ -27,6 +27,7 @@ CREATE TABLE llx_emailcollector_emailcollector( source_directory varchar(255) NOT NULL, target_directory varchar(255), datelastresult datetime, + codelastresult varchar(16), lastresult varchar(255), note_public text, note_private text, diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index d9be14b3cb7..259d8920744 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -1827,11 +1827,13 @@ DateLastResult=Date last collect LastResult=Last result EmailCollectorConfirmCollectTitle=Email collect confirmation EmailCollectorConfirmCollect=Do you want to run the collect for this collector now ? -NoNewEmailToProcess=No new email to process +NoNewEmailToProcess=No new email (matching filters) to process NothingProcessed=Nothing done XEmailsDoneYActionsDone=%s emails analyzed, %s record/actions done by collector RecordEvent=Record event CreateLeadAndThirdParty=Create lead (and thirdparty if necessary) +CodeLastResult=Result code of last collect +NbOfEmailsInInbox=Number of email in source directory ##### Resource #### ResourceSetup=Configuration du module Resource UseSearchToSelectResource=Use a search form to choose a resource (rather than a drop-down list).