New imap by oauth2 works, but somes features left

This commit is contained in:
Faustin 2022-08-16 17:23:57 +02:00
parent 0f467d1c42
commit 3bf8730c55
5 changed files with 232 additions and 149 deletions

View File

@ -224,7 +224,7 @@ if ($action == 'deleteoperation') {
} }
} }
if ($action == 'collect') { if ($action == 'confirm_collect') {
dol_include_once('/emailcollector/class/emailcollector.class.php'); dol_include_once('/emailcollector/class/emailcollector.class.php');
$res = $object->doCollectOneCollector(); $res = $object->doCollectOneCollector();

View File

@ -218,7 +218,6 @@ if ($mode == 'setup' && $user->admin) {
$tokenobj = null; $tokenobj = null;
// Token // Token
require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php'; require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
// Dolibarr storage // Dolibarr storage
$storage = new DoliStorage($db, $conf); $storage = new DoliStorage($db, $conf);
try { try {

View File

@ -51,6 +51,8 @@ use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
use Webklex\PHPIMAP\Exceptions\InvalidWhereQueryCriteriaException; use Webklex\PHPIMAP\Exceptions\InvalidWhereQueryCriteriaException;
use Webklex\PHPIMAP\Exceptions\GetMessagesFailedException; use Webklex\PHPIMAP\Exceptions\GetMessagesFailedException;
use OAuth\Common\Storage\DoliStorage;
use OAuth\Common\Consumer\Credentials;
/** /**
* Class for EmailCollector * Class for EmailCollector
*/ */
@ -123,11 +125,13 @@ class EmailCollector extends CommonObject
'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1, 'help'=>'Example: My Email collector', 'csslist'=>'tdoverflowmax150'), 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1, 'help'=>'Example: My Email collector', 'csslist'=>'tdoverflowmax150'),
'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>60, 'notnull'=>-1, 'csslist'=>'small'), 'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>60, 'notnull'=>-1, 'csslist'=>'small'),
'host' => array('type'=>'varchar(255)', 'label'=>'EMailHost', 'visible'=>1, 'enabled'=>1, 'position'=>90, 'notnull'=>1, 'searchall'=>1, 'comment'=>"IMAP server", 'help'=>'Example: imap.gmail.com', 'csslist'=>'tdoverflow125'), 'host' => array('type'=>'varchar(255)', 'label'=>'EMailHost', 'visible'=>1, 'enabled'=>1, 'position'=>90, 'notnull'=>1, 'searchall'=>1, 'comment'=>"IMAP server", 'help'=>'Example: imap.gmail.com', 'csslist'=>'tdoverflow125'),
// 'port' => array('type'=>'varchar(10)', 'label'=>'EMailHostPort', 'visible'=>1, 'enabled'=>1, 'position'=>91, 'notnull'=>1, 'searchall'=>0, 'comment'=>"IMAP server port", 'help'=>'Example: 993', 'csslist'=>'tdoverflow125', 'default'=>'993'), 'port' => array('type'=>'varchar(10)', 'label'=>'EMailHostPort', 'visible'=>1, 'enabled'=>1, 'position'=>91, 'notnull'=>1, 'searchall'=>0, 'comment'=>"IMAP server port", 'help'=>'Example: 993', 'csslist'=>'tdoverflow125', 'default'=>'993'),
'hostcharset' => array('type'=>'varchar(16)', 'label'=>'HostCharset', 'visible'=>-1, 'enabled'=>1, 'position'=>92, 'notnull'=>0, 'searchall'=>0, 'comment'=>"IMAP server charset", 'help'=>'Example: "UTF-8" (May be "US-ASCII" with some Office365)', 'default'=>'UTF-8'), 'hostcharset' => array('type'=>'varchar(16)', 'label'=>'HostCharset', 'visible'=>-1, 'enabled'=>1, 'position'=>92, 'notnull'=>0, 'searchall'=>0, 'comment'=>"IMAP server charset", 'help'=>'Example: "UTF-8" (May be "US-ASCII" with some Office365)', 'default'=>'UTF-8'),
'login' => array('type'=>'varchar(128)', 'label'=>'Login', 'visible'=>-1, 'enabled'=>1, 'position'=>101, 'notnull'=>-1, 'index'=>1, 'comment'=>"IMAP login", 'help'=>'Example: myaccount@gmail.com'), 'login' => array('type'=>'varchar(128)', 'label'=>'Login', 'visible'=>-1, 'enabled'=>1, 'position'=>101, 'notnull'=>-1, 'index'=>1, 'comment'=>"IMAP login", 'help'=>'Example: myaccount@gmail.com'),
'password' => array('type'=>'password', 'label'=>'Password', 'visible'=>-1, 'enabled'=>1, 'position'=>102, 'notnull'=>-1, 'comment'=>"IMAP password", 'help'=>'WithGMailYouCanCreateADedicatedPassword'), 'acces_type' => array('type'=>'integer', 'label'=>'accessType', 'visible'=>-1, 'enabled'=>1, 'position'=>102, 'notnull'=>0, 'index'=>1, 'comment'=>"IMAP login type", 'arrayofkeyval'=>array('0'=>'loginPassword', '1'=>'oauthToken'), 'default'=>'0'),
'source_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxSourceDirectory', 'visible'=>-1, 'enabled'=>1, 'position'=>103, 'notnull'=>1, 'default' => 'Inbox', 'help'=>'Example: INBOX'), 'oauth_service' => array('type'=>'varchar(128)', 'label'=>'oauthService', 'visible'=>-1, 'enabled'=>1, 'position'=>103, 'notnull'=>0, 'index'=>1, 'comment'=>"IMAP login oauthService", 'arrayofkeyval'=>array()),
'password' => array('type'=>'password', 'label'=>'Password', 'visible'=>-1, 'enabled'=>1, 'position'=>103, 'notnull'=>-1, 'comment'=>"IMAP password", 'help'=>'WithGMailYouCanCreateADedicatedPassword'),
'source_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxSourceDirectory', 'visible'=>-1, 'enabled'=>1, 'position'=>104, 'notnull'=>1, 'default' => 'Inbox', 'help'=>'Example: INBOX'),
//'filter' => array('type'=>'text', 'label'=>'Filter', 'visible'=>1, 'enabled'=>1, 'position'=>105), //'filter' => array('type'=>'text', 'label'=>'Filter', 'visible'=>1, 'enabled'=>1, 'position'=>105),
//'actiontodo' => array('type'=>'varchar(255)', 'label'=>'ActionToDo', 'visible'=>1, 'enabled'=>1, 'position'=>106), //'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, 'help'=>"EmailCollectorTargetDir"), 'target_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxTargetDirectory', 'visible'=>1, 'enabled'=>1, 'position'=>110, 'notnull'=>0, 'help'=>"EmailCollectorTargetDir"),
@ -206,6 +210,8 @@ class EmailCollector extends CommonObject
public $hostcharset; public $hostcharset;
public $login; public $login;
public $password; public $password;
public $acces_type;
public $oauth_service;
public $source_directory; public $source_directory;
public $target_directory; public $target_directory;
public $maxemailpercollect; public $maxemailpercollect;
@ -247,6 +253,27 @@ class EmailCollector extends CommonObject
$this->fields['entity']['enabled'] = 0; $this->fields['entity']['enabled'] = 0;
} }
// List of oauth services
$oauthservices = array();
foreach ($conf->global as $key => $val) {
if (!empty($val) && preg_match('/^OAUTH_.*_ID$/', $key)) {
$key = preg_replace('/^OAUTH_/', '', $key);
$key = preg_replace('/_ID$/', '', $key);
if (preg_match('/^.*-/', $key)) {
$name = preg_replace('/^.*-/', '', $key);
} else {
$name = $langs->trans("NoName");
}
$provider = preg_replace('/-.*$/', '', $key);
$provider = ucfirst(strtolower($provider));
$oauthservices[$key] = $name." (".$provider.")";
}
}
$this->fields['oauth_service']['arrayofkeyval'] = $oauthservices;
// Unset fields that are disabled // Unset fields that are disabled
foreach ($this->fields as $key => $val) { foreach ($this->fields as $key => $val) {
if (isset($val['enabled']) && empty($val['enabled'])) { if (isset($val['enabled']) && empty($val['enabled'])) {
@ -959,7 +986,7 @@ class EmailCollector extends CommonObject
*/ */
public function doCollectOneCollector() public function doCollectOneCollector()
{ {
global $conf, $langs, $user; global $db, $conf, $langs, $user;
global $hookmanager; global $hookmanager;
//$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log'; //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
@ -977,27 +1004,82 @@ class EmailCollector extends CommonObject
$now = dol_now(); $now = dol_now();
// if (empty($this->host)) { if (empty($this->host)) {
// $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMailHost')); $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMailHost'));
// return -1; return -1;
// } }
// if (empty($this->login)) { if (empty($this->login)) {
// $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Login')); $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Login'));
// return -1; return -1;
// } }
$this->fetchFilters(); $this->fetchFilters();
$this->fetchActions(); $this->fetchActions();
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
$keyforsupportedoauth2array = $this->oauth_service;
if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
$keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
} else {
$keyforprovider = '';
}
$keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
$keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
$OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : ''));
require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
$debugtext = "Host: ".$this->host."<br>Port: ".$this->port."<br>Login: ".$this->login."<br>Password: ".$this->password."<br>access type: ".$this->acces_type."<br>oauth service: ".$this->oauth_service."<br>Max email per collect: ".$this->maxemailpercollect;
$storage = new DoliStorage($db, $conf);
try {
$tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
$expire = true;
// Is token expired or will token expire in the next 30 seconds
// if (is_object($tokenobj)) {
// $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
// }
// Token expired so we refresh it
if (is_object($tokenobj) && $expire) {
$credentials = new Credentials(
getDolGlobalString('OAUTH_'.$this->oauth_service.'_ID'),
getDolGlobalString('OAUTH_'.$this->oauth_service.'_SECRET'),
getDolGlobalString('OAUTH_'.$this->oauth_service.'_URLAUTHORIZE')
);
$serviceFactory = new \OAuth\ServiceFactory();
$oauthname = explode('-', $OAUTH_SERVICENAME);
// ex service is Google-Emails we need only the first part Google
$apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
// We have to save the token because Google give it only once
$refreshtoken = $tokenobj->getRefreshToken();
$tokenobj = $apiService->refreshAccessToken($tokenobj);
$tokenobj->setRefreshToken($refreshtoken);
$storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
}
$tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
if (is_object($tokenobj)) {
$token = $tokenobj->getAccessToken();
} else {
$this->error = "Token not found";
return -1;
}
} catch (Exception $e) {
// Return an error if token not found
$this->error = $e->getMessage();
dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
return -1;
}
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
$cm = new ClientManager(); $cm = new ClientManager();
$client = $cm->make([ $client = $cm->make([
'host' => 'smtp.gmail.com', 'host' => $this->host,
'port' => 993, 'port' => 993,
'encryption' => 'ssl', 'encryption' => 'ssl',
'validate_cert' => true, 'validate_cert' => true,
'protocol' => 'imap', 'protocol' => 'imap',
'username' => 'boitel.faustin@gmail.com', 'username' => $this->login,
'password' => 'ya29.A0AVA9y1siEZ-DibQtRUpD-FKBsCxAdm_m70o9SET8F8X9_VBlOk2RErA70mXNgrGScmShhGR1QTcHbYuROT1KyMUQiFOe_VsyTqiFVt9ry41VTLnCICybvMvvfcHcXQjo4AGhofK8xB2qWVJPHhvOdJABHexmaCgYKATASATASFQE65dr8Pgiz_lRG76Aea35N2cFGFw0163', 'password' => $token,
'authentication' => "oauth", 'authentication' => "oauth",
]); ]);
@ -1041,13 +1123,8 @@ class EmailCollector extends CommonObject
//$search='ALL'; //$search='ALL';
} }
if ($conf->global->MAIN_IMAP_USE_PHPIMAP) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
$criteria = array(array('UNDELETED')); // Seems not supported by some servers $criteria = array(array('UNDELETED')); // Seems not supported by some servers
// $searchhead = '';
// $searchfilterdoltrackid = 0;
// $searchfilternodoltrackid = 0;
// $searchfilterisanswer = 0;
// $searchfilterisnotanswer = 0;
foreach ($this->filters as $rule) { foreach ($this->filters as $rule) {
if (empty($rule['status'])) { if (empty($rule['status'])) {
continue; continue;
@ -1222,9 +1299,9 @@ class EmailCollector extends CommonObject
$nbactiondone = 0; $nbactiondone = 0;
$charset = ($this->hostcharset ? $this->hostcharset : "UTF-8"); $charset = ($this->hostcharset ? $this->hostcharset : "UTF-8");
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
try { try {
$Query = $client->getFolders()[0]->messages()->where($criteria);//->all(); $Query = $client->getFolders()[0]->messages()->where($criteria);
} catch (InvalidWhereQueryCriteriaException $e) { } catch (InvalidWhereQueryCriteriaException $e) {
$this->error = $e->getMessage(); $this->error = $e->getMessage();
$this->errors[] = $this->error; $this->errors[] = $this->error;
@ -1240,31 +1317,6 @@ class EmailCollector extends CommonObject
dol_syslog("EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR); dol_syslog("EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
return -1; return -1;
} }
// for debug
$found = count($arrayofemail);
print_r($criteria);
print "<br>".$found." mails found<br>";
for ($i = 0; $i < $found; $i++) {
$sub = $arrayofemail[$found - 1 - $i]->getSubject();
$message = $arrayofemail[$found - 1 - $i]->getHTMLBody();
$header = $arrayofemail[$found - 1 - $i]->getHeader();
$flags = $arrayofemail[$found - 1 - $i]->getFlags()->keys();
$attributes = $arrayofemail[$found - 1 - $i]->getAttributes();
print '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br>';
print '<br>'.$i.'<br>';
print "FLAGS: ".$flags."<br>Header:";
print_r($header->getAttributes());
print "<br>Sub header:".$header->getAttributes()['message-id']."<br>";
print $sub."<br>";
print $message."<br>";
print "FROM:".$attributes['subject']."<br>";
// print_r($attributes)."<br><br><br>";
print '<br>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><br><br><br>';
}
} else { } else {
// Scan IMAP inbox // Scan IMAP inbox
$arrayofemail = imap_search($connection, $search, null, $charset); $arrayofemail = imap_search($connection, $search, null, $charset);
@ -1279,8 +1331,6 @@ class EmailCollector extends CommonObject
} }
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loop on each email found // Loop on each email found
if (!$error && !empty($arrayofemail) && count($arrayofemail) > 0) { if (!$error && !empty($arrayofemail) && count($arrayofemail) > 0) {
// Loop to get part html and plain // Loop to get part html and plain
@ -1310,8 +1360,8 @@ class EmailCollector extends CommonObject
// GET header and overview datas // GET header and overview datas
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
$header = $imapemail->getHeader(); $header = $imapemail->getHeader()->raw;
$overview = $imapemail->getAttributes(); $overview = $imapemail->getAttributes();
} else { } else {
$header = imap_headerinfo($connection, $imapemail); $header = imap_headerinfo($connection, $imapemail);
@ -1319,19 +1369,11 @@ class EmailCollector extends CommonObject
} }
// print $header;
// var_dump($overview);
// Process $header of email
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
$headers = $header->getAttributes();
} else {
$header = preg_replace('/\r\n\s+/m', ' ', $header); // When a header line is on several lines, merge lines $header = preg_replace('/\r\n\s+/m', ' ', $header); // When a header line is on several lines, merge lines
$matches = array(); $matches = array();
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $header, $matches); preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $header, $matches);
$headers = array_combine($matches[1], $matches[2]); $headers = array_combine($matches[1], $matches[2]);
}
if (!empty($headers['in-reply-to']) && empty($headers['In-Reply-To'])) { if (!empty($headers['in-reply-to']) && empty($headers['In-Reply-To'])) {
$headers['In-Reply-To'] = $headers['in-reply-to']; $headers['In-Reply-To'] = $headers['in-reply-to'];
} }
@ -1350,10 +1392,9 @@ class EmailCollector extends CommonObject
dol_syslog("** Process email ".$iforemailloop." References: ".$headers['References']." Subject: ".$headers['Subject']); dol_syslog("** Process email ".$iforemailloop." References: ".$headers['References']." Subject: ".$headers['Subject']);
//print "Process mail ".$iforemailloop." Subject: ".dol_escape_htmltag($headers['Subject'])." References: ".dol_escape_htmltag($headers['References'])." In-Reply-To: ".dol_escape_htmltag($headers['In-Reply-To'])."<br>\n";
$err = "** Process email ".$iforemailloop." References: ".$headers['References']." Subject: ".$headers['Subject'];
$this->error = $langs->trans($err, $langs->transnoentitiesnoconv('EMailHost'));
return -1;
// If there is a filter on trackid // If there is a filter on trackid
if ($searchfilterdoltrackid > 0) { if ($searchfilterdoltrackid > 0) {
if (empty($headers['References']) || !preg_match('/@'.preg_quote($host, '/').'/', $headers['References'])) { if (empty($headers['References']) || !preg_match('/@'.preg_quote($host, '/').'/', $headers['References'])) {
@ -1427,6 +1468,11 @@ class EmailCollector extends CommonObject
$this->db->begin(); $this->db->begin();
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
dol_syslog("msgid=".$overview['message_id']." date=".dol_print_date($overview['udate'], 'dayrfc', 'gmt')." from=".$overview['from']." to=".$overview['to']." subject=".$overview['subject']);
$overview['subject'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $overview['subject']);
} else {
dol_syslog("msgid=".$overview[0]->message_id." date=".dol_print_date($overview[0]->udate, 'dayrfc', 'gmt')." from=".$overview[0]->from." to=".$overview[0]->to." subject=".$overview[0]->subject); dol_syslog("msgid=".$overview[0]->message_id." date=".dol_print_date($overview[0]->udate, 'dayrfc', 'gmt')." from=".$overview[0]->from." to=".$overview[0]->to." subject=".$overview[0]->subject);
$overview[0]->subject = $this->decodeSMTPSubject($overview[0]->subject); $overview[0]->subject = $this->decodeSMTPSubject($overview[0]->subject);
@ -1435,13 +1481,24 @@ class EmailCollector extends CommonObject
// Removed emojis // Removed emojis
$overview[0]->subject = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $overview[0]->subject); $overview[0]->subject = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $overview[0]->subject);
}
// GET IMAP email structure/content // GET IMAP email structure/content
global $htmlmsg, $plainmsg, $charset, $attachments; global $htmlmsg, $plainmsg, $charset, $attachments;
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
if ($imapemail->hasHTMLBody()) {
$htmlmsg = $imapemail->getHTMLBody();
}
if ($imapemail->hasTextBody()) {
$plainmsg = $imapemail->getTextBody();
}
if ($imapemail->hasAttachments()) {
$attachments = $imapemail->getAttachments();
}
} else {
$this->getmsg($connection, $imapemail); $this->getmsg($connection, $imapemail);
}
//print $plainmsg; //print $plainmsg;
//var_dump($plainmsg); exit; //var_dump($plainmsg); exit;
@ -1505,6 +1562,17 @@ class EmailCollector extends CommonObject
//print $messagetext; //print $messagetext;
//exit; //exit;
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
$fromstring = $overview['from'];
$sender = $overview['sender'];
$to = $overview['to'];
$sendtocc = $overview['cc'];
$sendtobcc = $overview['bcc'];
$date = $overview['date'];
$msgid = str_replace(array('<', '>'), '', $overview['message_id']);
$subject = $overview['subject'];
} else {
$fromstring = $overview[0]->from; $fromstring = $overview[0]->from;
$sender = $overview[0]->sender; $sender = $overview[0]->sender;
@ -1515,6 +1583,8 @@ class EmailCollector extends CommonObject
$msgid = str_replace(array('<', '>'), '', $overview[0]->message_id); $msgid = str_replace(array('<', '>'), '', $overview[0]->message_id);
$subject = $overview[0]->subject; $subject = $overview[0]->subject;
//var_dump($msgid);exit; //var_dump($msgid);exit;
}
$reg = array(); $reg = array();
if (preg_match('/^(.*)<(.*)>$/', $fromstring, $reg)) { if (preg_match('/^(.*)<(.*)>$/', $fromstring, $reg)) {
@ -2184,7 +2254,6 @@ class EmailCollector extends CommonObject
} elseif ($operation['type'] == 'project') { } elseif ($operation['type'] == 'project') {
// Create project / lead // Create project / lead
$projecttocreate = new Project($this->db); $projecttocreate = new Project($this->db);
$alreadycreated = $projecttocreate->fetch(0, '', '', $msgid); $alreadycreated = $projecttocreate->fetch(0, '', '', $msgid);
if ($alreadycreated == 0) { if ($alreadycreated == 0) {
if ($thirdpartystatic->id > 0) { if ($thirdpartystatic->id > 0) {
@ -2268,6 +2337,7 @@ class EmailCollector extends CommonObject
$projecttocreate->ref = $defaultref; $projecttocreate->ref = $defaultref;
} }
if ($errorforthisaction) { if ($errorforthisaction) {
$errorforactions++; $errorforactions++;
} else { } else {
@ -2391,8 +2461,10 @@ class EmailCollector extends CommonObject
$this->errors = $tickettocreate->errors; $this->errors = $tickettocreate->errors;
} else { } else {
if ($attachments) { if ($attachments) {
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
$destdir = $conf->ticket->dir_output.'/'.$tickettocreate->ref; $destdir = $conf->ticket->dir_output.'/'.$tickettocreate->ref;
if (!dol_is_dir($destdir)) { if (!dol_is_dir($destdir)) {
return -1;
dol_mkdir($destdir); dol_mkdir($destdir);
$this->getmsg($connection, $imapemail, $destdir); $this->getmsg($connection, $imapemail, $destdir);
} }
@ -2401,6 +2473,7 @@ class EmailCollector extends CommonObject
} }
} }
} }
}
} elseif ($operation['type'] == 'candidature') { } elseif ($operation['type'] == 'candidature') {
// Create candidature // Create candidature
$candidaturetocreate = new RecruitmentCandidature($this->db); $candidaturetocreate = new RecruitmentCandidature($this->db);
@ -2529,6 +2602,7 @@ class EmailCollector extends CommonObject
} }
} }
if (!$errorforactions) { if (!$errorforactions) {
$nbactiondoneforemail++; $nbactiondoneforemail++;
} }
@ -2536,6 +2610,7 @@ class EmailCollector extends CommonObject
// Error for email or not ? // Error for email or not ?
if (!$errorforactions) { if (!$errorforactions) {
if (empty($conf->global->MAIN_IMAP_USE_PHPIMAP) || $this->acces_type != 1) {
if ($targetdir) { if ($targetdir) {
dol_syslog("EmailCollector::doCollectOneCollector move message ".$imapemail." to ".$connectstringtarget, LOG_DEBUG); dol_syslog("EmailCollector::doCollectOneCollector move message ".$imapemail." to ".$connectstringtarget, LOG_DEBUG);
$res = imap_mail_move($connection, $imapemail, $targetdir, 0); $res = imap_mail_move($connection, $imapemail, $targetdir, 0);
@ -2548,10 +2623,12 @@ class EmailCollector extends CommonObject
} else { } else {
dol_syslog("EmailCollector::doCollectOneCollector message ".$imapemail." to ".$connectstringtarget." was set to read", LOG_DEBUG); dol_syslog("EmailCollector::doCollectOneCollector message ".$imapemail." to ".$connectstringtarget." was set to read", LOG_DEBUG);
} }
}
} else { } else {
$errorforemail++; $errorforemail++;
} }
unset($objectemail); unset($objectemail);
unset($projectstatic); unset($projectstatic);
unset($thirdpartystatic); unset($thirdpartystatic);
@ -2584,10 +2661,13 @@ class EmailCollector extends CommonObject
$output = $langs->trans('NoNewEmailToProcess'); $output = $langs->trans('NoNewEmailToProcess');
} }
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) {
$client->disconnect();
} else {
imap_expunge($connection); // To validate any move imap_expunge($connection); // To validate any move
imap_close($connection); imap_close($connection);
}
$this->datelastresult = $now; $this->datelastresult = $now;
$this->lastresult = $output; $this->lastresult = $output;
$this->debuginfo = 'IMAP search string used : '.$search; $this->debuginfo = 'IMAP search string used : '.$search;

View File

@ -68,6 +68,8 @@ ALTER TABLE llx_ticket ADD COLUMN ip varchar(250);
ALTER TABLE llx_societe ADD last_main_doc VARCHAR(255) NULL AFTER model_pdf; ALTER TABLE llx_societe ADD last_main_doc VARCHAR(255) NULL AFTER model_pdf;
ALTER TABLE llx_emailcollector_emailcollector ADD COLUMN port varchar(10) DEFAULT '993'; ALTER TABLE llx_emailcollector_emailcollector ADD COLUMN port varchar(10) DEFAULT '993';
ALTER TABLE llx_emailcollector_emailcollector ADD COLUMN acces_type integer DEFAULT 0;
ALTER TABLE llx_emailcollector_emailcollector ADD COLUMN oauth_service varchar(128) DEFAULT NULL;
ALTER TABLE llx_bank ADD COLUMN position integer DEFAULT 0; ALTER TABLE llx_bank ADD COLUMN position integer DEFAULT 0;

View File

@ -25,6 +25,8 @@ CREATE TABLE llx_emailcollector_emailcollector(
port varchar(10) DEFAULT '993', port varchar(10) DEFAULT '993',
hostcharset varchar(16) DEFAULT 'UTF-8', hostcharset varchar(16) DEFAULT 'UTF-8',
login varchar(128), login varchar(128),
acces_type integer DEFAULT 0,
oauth_service varchar(128),
password varchar(128), password varchar(128),
source_directory varchar(255) NOT NULL, source_directory varchar(255) NOT NULL,
target_directory varchar(255), target_directory varchar(255),