1082 global $db, $conf, $langs, $user;
1083 global $hookmanager;
1087 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
1088 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
1089 require_once DOL_DOCUMENT_ROOT.
'/includes/webklex/php-imap/vendor/autoload.php';
1092 dol_syslog(
"EmailCollector::doCollectOneCollector start for id=".$this->
id.
" - ".$this->
ref, LOG_INFO);
1094 $langs->loadLangs(array(
"project",
"companies",
"mails",
"errors",
"ticket",
"agenda",
"commercial"));
1099 $this->debuginfo =
'';
1103 $searchfilterdoltrackid = 0;
1104 $searchfilternodoltrackid = 0;
1105 $searchfilterisanswer = 0;
1106 $searchfilterisnotanswer = 0;
1107 $searchfilterreplyto = 0;
1108 $searchfilterexcludebodyarray = array();
1109 $searchfilterexcludesubjectarray = array();
1110 $operationslog =
'';
1115 if (empty($this->host)) {
1116 $this->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
'EMailHost'));
1119 if (empty($this->login)) {
1120 $this->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
'Login'));
1123 if (empty($this->source_directory)) {
1124 $this->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
'MailboxSourceDirectory'));
1131 $targetdir = ($this->target_directory ? $this->target_directory :
'');
1133 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
1134 if ($this->acces_type == 1) {
1136 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1137 $keyforsupportedoauth2array = $this->oauth_service;
1138 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1139 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1141 $keyforprovider =
'';
1143 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1144 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1146 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
1148 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1154 $storage =
new DoliStorage($db, $conf, $keyforprovider);
1157 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1165 if (is_object($tokenobj) && $expire) {
1166 $credentials =
new Credentials(
1171 $serviceFactory = new \OAuth\ServiceFactory();
1172 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1174 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1176 $refreshtoken = $tokenobj->getRefreshToken();
1177 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1178 $tokenobj->setRefreshToken($refreshtoken);
1179 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1181 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1182 if (is_object($tokenobj)) {
1183 $token = $tokenobj->getAccessToken();
1185 $this->error =
"Token not found";
1190 $this->error = $e->getMessage();
1191 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1195 $cm =
new ClientManager();
1196 $client = $cm->make([
1197 'host' => $this->host,
1198 'port' => $this->port,
1199 'encryption' => !empty($this->imap_encryption) ? $this->imap_encryption :
false,
1200 'validate_cert' =>
true,
1201 'protocol' =>
'imap',
1202 'username' => $this->login,
1203 'password' => $token,
1204 'authentication' =>
"oauth",
1208 $cm =
new ClientManager();
1209 $client = $cm->make([
1210 'host' => $this->host,
1211 'port' => $this->port,
1212 'encryption' => !empty($this->imap_encryption) ? $this->imap_encryption :
false,
1213 'validate_cert' =>
true,
1214 'protocol' =>
'imap',
1215 'username' => $this->login,
1216 'password' => $this->password,
1217 'authentication' =>
"login",
1223 }
catch (ConnectionFailedException $e) {
1224 $this->error = $e->getMessage();
1225 $this->errors[] = $this->error;
1226 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1230 $host = dol_getprefix(
'email');
1233 if (!function_exists(
'imap_open')) {
1234 $this->error =
'IMAP function not enabled on your PHP';
1237 $sourcedir = $this->source_directory;
1240 $connectstringsource = $connectstringserver.imap_utf7_encode($sourcedir);
1241 $connectstringtarget = $connectstringserver.imap_utf7_encode($targetdir);
1243 $connection = imap_open($connectstringsource, $this->login, $this->password);
1245 $this->error =
'Failed to open IMAP connection '.$connectstringsource.
' '.imap_last_error();
1250 $host = dol_getprefix(
'email');
1259 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
1261 $criteria = array(array(
'UNDELETED'));
1262 foreach ($this->filters as $rule) {
1263 if (empty($rule[
'status'])) {
1268 if (strpos($rule[
'rulevalue'],
'!') === 0) {
1272 $rule[
'rulevalue'] = substr($rule[
'rulevalue'], 1);
1275 if ($rule[
'type'] ==
'from') {
1276 $tmprulevaluearray = explode(
'*', $rule[
'rulevalue']);
1277 if (count($tmprulevaluearray) >= 2) {
1278 foreach ($tmprulevaluearray as $tmprulevalue) {
1279 array_push($criteria, array($not.
"FROM" => $tmprulevalue));
1282 array_push($criteria, array($not.
"FROM" => $rule[
'rulevalue']));
1285 if ($rule[
'type'] ==
'to') {
1286 $tmprulevaluearray = explode(
'*', $rule[
'rulevalue']);
1287 if (count($tmprulevaluearray) >= 2) {
1288 foreach ($tmprulevaluearray as $tmprulevalue) {
1289 array_push($criteria, array($not.
"TO" => $tmprulevalue));
1292 array_push($criteria, array($not.
"TO" => $rule[
'rulevalue']));
1295 if ($rule[
'type'] ==
'bcc') {
1296 array_push($criteria, array($not.
"BCC" => $rule[
'rulevalue']));
1298 if ($rule[
'type'] ==
'cc') {
1299 array_push($criteria, array($not.
"CC" => $rule[
'rulevalue']));
1301 if ($rule[
'type'] ==
'subject') {
1302 if (strpos($rule[
'rulevalue'],
'!') === 0) {
1304 $searchfilterexcludesubjectarray[] = preg_replace(
'/^!/',
'', $rule[
'rulevalue']);
1306 array_push($criteria, array(
"SUBJECT" => $rule[
'rulevalue']));
1309 if ($rule[
'type'] ==
'body') {
1310 if (strpos($rule[
'rulevalue'],
'!') === 0) {
1312 $searchfilterexcludebodyarray[] = preg_replace(
'/^!/',
'', $rule[
'rulevalue']);
1314 array_push($criteria, array(
"BODY" => $rule[
'rulevalue']));
1317 if ($rule[
'type'] ==
'header') {
1318 array_push($criteria, array($not.
"HEADER" => $rule[
'rulevalue']));
1330 if ($rule[
'type'] ==
'seen') {
1331 array_push($criteria, array($not.
"SEEN"));
1333 if ($rule[
'type'] ==
'unseen') {
1334 array_push($criteria, array($not.
"UNSEEN"));
1336 if ($rule[
'type'] ==
'unanswered') {
1337 array_push($criteria, array($not.
"UNANSWERED"));
1339 if ($rule[
'type'] ==
'answered') {
1340 array_push($criteria, array($not.
"ANSWERED"));
1342 if ($rule[
'type'] ==
'smaller') {
1343 array_push($criteria, array($not.
"SMALLER"));
1345 if ($rule[
'type'] ==
'larger') {
1346 array_push($criteria, array($not.
"LARGER"));
1350 if ($rule[
'type'] ==
'withtrackingidinmsgid') {
1351 $searchfilterdoltrackid++; $searchhead .=
'/Message-ID.*@'.preg_quote($host,
'/').
'/';
1353 if ($rule[
'type'] ==
'withouttrackingidinmsgid') {
1354 $searchfilterdoltrackid++; $searchhead .=
'/Message-ID.*@'.preg_quote($host,
'/').
'/';
1356 if ($rule[
'type'] ==
'withtrackingid') {
1357 $searchfilterdoltrackid++; $searchhead .=
'/References.*@'.preg_quote($host,
'/').
'/';
1359 if ($rule[
'type'] ==
'withouttrackingid') {
1360 $searchfilternodoltrackid++; $searchhead .=
'! /References.*@'.preg_quote($host,
'/').
'/';
1363 if ($rule[
'type'] ==
'isanswer') {
1364 $searchfilterisanswer++; $searchhead .=
'/References.*@.*/';
1366 if ($rule[
'type'] ==
'isnotanswer') {
1367 $searchfilterisnotanswer++; $searchhead .=
'! /References.*@.*/';
1370 if ($rule[
'type'] ==
'replyto') {
1371 $searchfilterreplyto++; $searchhead .=
'/Reply-To.*'.preg_quote($rule[
'rulevalue'],
'/').
'/';
1375 if (empty($targetdir)) {
1377 if ($this->datelastok) {
1378 $fromdate = $this->datelastok;
1380 if ($fromdate > 0) {
1382 array_push($criteria, array(
"SINCE" => date(
'j-M-Y', $fromdate - 1)));
1387 dol_syslog(
"IMAP search string = ".var_export($criteria,
true));
1388 $search = var_export($criteria,
true);
1391 $search =
'UNDELETED';
1392 foreach ($this->filters as $rule) {
1393 if (empty($rule[
'status'])) {
1401 if (strpos($rule[
'rulevalue'],
'!') === 0) {
1405 $rule[
'rulevalue'] = substr($rule[
'rulevalue'], 1);
1408 if ($rule[
'type'] ==
'from') {
1409 $tmprulevaluearray = explode(
'*', $rule[
'rulevalue']);
1410 if (count($tmprulevaluearray) >= 2) {
1411 foreach ($tmprulevaluearray as $tmprulevalue) {
1412 $search .= ($search ?
' ' :
'').$not.
'FROM "'.str_replace(
'"',
'', $tmprulevalue).
'"';
1415 $search .= ($search ?
' ' :
'').$not.
'FROM "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1418 if ($rule[
'type'] ==
'to') {
1419 $tmprulevaluearray = explode(
'*', $rule[
'rulevalue']);
1420 if (count($tmprulevaluearray) >= 2) {
1421 foreach ($tmprulevaluearray as $tmprulevalue) {
1422 $search .= ($search ?
' ' :
'').$not.
'TO "'.str_replace(
'"',
'', $tmprulevalue).
'"';
1425 $search .= ($search ?
' ' :
'').$not.
'TO "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1428 if ($rule[
'type'] ==
'bcc') {
1429 $search .= ($search ?
' ' :
'').$not.
'BCC';
1431 if ($rule[
'type'] ==
'cc') {
1432 $search .= ($search ?
' ' :
'').$not.
'CC';
1434 if ($rule[
'type'] ==
'subject') {
1435 if (strpos($rule[
'rulevalue'],
'!') === 0) {
1437 $searchfilterexcludesubjectarray[] = preg_replace(
'/^!/',
'', $rule[
'rulevalue']);
1439 $search .= ($search ?
' ' :
'').
'SUBJECT "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1442 if ($rule[
'type'] ==
'body') {
1443 if (strpos($rule[
'rulevalue'],
'!') === 0) {
1445 $searchfilterexcludebodyarray[] = preg_replace(
'/^!/',
'', $rule[
'rulevalue']);
1448 $search .= ($search ?
' ' :
'').
'BODY "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1451 if ($rule[
'type'] ==
'header') {
1452 $search .= ($search ?
' ' :
'').$not.
'HEADER '.$rule[
'rulevalue'];
1464 if ($rule[
'type'] ==
'seen') {
1465 $search .= ($search ?
' ' :
'').$not.
'SEEN';
1467 if ($rule[
'type'] ==
'unseen') {
1468 $search .= ($search ?
' ' :
'').$not.
'UNSEEN';
1470 if ($rule[
'type'] ==
'unanswered') {
1471 $search .= ($search ?
' ' :
'').$not.
'UNANSWERED';
1473 if ($rule[
'type'] ==
'answered') {
1474 $search .= ($search ?
' ' :
'').$not.
'ANSWERED';
1476 if ($rule[
'type'] ==
'smaller') {
1477 $search .= ($search ?
' ' :
'').$not.
'SMALLER "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1479 if ($rule[
'type'] ==
'larger') {
1480 $search .= ($search ?
' ' :
'').$not.
'LARGER "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1484 if ($rule[
'type'] ==
'withtrackingidinmsgid') {
1485 $searchfilterdoltrackid++; $searchhead .=
'/Message-ID.*@'.preg_quote($host,
'/').
'/';
1487 if ($rule[
'type'] ==
'withouttrackingidinmsgid') {
1488 $searchfilterdoltrackid++; $searchhead .=
'/Message-ID.*@'.preg_quote($host,
'/').
'/';
1490 if ($rule[
'type'] ==
'withtrackingid') {
1491 $searchfilterdoltrackid++; $searchhead .=
'/References.*@'.preg_quote($host,
'/').
'/';
1493 if ($rule[
'type'] ==
'withouttrackingid') {
1494 $searchfilternodoltrackid++; $searchhead .=
'! /References.*@'.preg_quote($host,
'/').
'/';
1497 if ($rule[
'type'] ==
'isanswer') {
1498 $searchfilterisanswer++; $searchhead .=
'/References.*@.*/';
1500 if ($rule[
'type'] ==
'isnotanswer') {
1501 $searchfilterisnotanswer++; $searchhead .=
'! /References.*@.*/';
1504 if ($rule[
'type'] ==
'replyto') {
1505 $searchfilterreplyto++; $searchhead .=
'/Reply-To.*'.preg_quote($rule[
'rulevalue'],
'/').
'/';
1509 if (empty($targetdir)) {
1511 if ($this->datelastok) {
1512 $fromdate = $this->datelastok;
1514 if ($fromdate > 0) {
1515 $search .= ($search ?
' ' :
'').
'SINCE '.date(
'j-M-Y', $fromdate - 1);
1524 $nbemailprocessed = 0;
1527 $charset = ($this->hostcharset ? $this->hostcharset :
"UTF-8");
1529 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
1533 $f = $client->getFolders(
false, $this->source_directory);
1534 $Query = $f[0]->messages()->where($criteria);
1535 }
catch (InvalidWhereQueryCriteriaException $e) {
1536 $this->error = $e->getMessage();
1537 $this->errors[] = $this->error;
1538 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1541 $this->error = $e->getMessage();
1542 $this->errors[] = $this->error;
1543 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1550 $Query->leaveUnread();
1552 $arrayofemail = $Query->limit($this->maxemailpercollect)->setFetchOrder(
"asc")->get();
1555 $this->error = $e->getMessage();
1556 $this->errors[] = $this->error;
1557 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1562 $arrayofemail = imap_search($connection, $search, SE_UID, $charset);
1564 if ($arrayofemail ===
false) {
1566 $mapoferrrors = imap_errors();
1567 if ($mapoferrrors !==
false) {
1569 $this->error =
"Search string not understood - ".join(
',', $mapoferrrors);
1570 $this->errors[] = $this->error;
1575 $arrayofemailtodelete = array();
1578 if (!$error && !empty($arrayofemail) && count($arrayofemail) > 0) {
1595 dol_syslog(
"Start of loop on email", LOG_INFO, 1);
1598 foreach ($arrayofemail as $imapemail) {
1599 if ($nbemailprocessed > 1000) {
1606 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
1607 $header = $imapemail->getHeader()->raw;
1608 $overview = $imapemail->getAttributes();
1610 $header = imap_fetchheader($connection, $imapemail, FT_UID);
1611 $overview = imap_fetch_overview($connection, $imapemail, FT_UID);
1614 $header = preg_replace(
'/\r\n\s+/m',
' ', $header);
1617 preg_match_all(
'/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $header, $matches);
1618 $headers = array_combine($matches[1], $matches[2]);
1621 if (!empty($headers[
'in-reply-to']) && empty($headers[
'In-Reply-To'])) {
1622 $headers[
'In-Reply-To'] = $headers[
'in-reply-to'];
1624 if (!empty($headers[
'references']) && empty($headers[
'References'])) {
1625 $headers[
'References'] = $headers[
'references'];
1627 if (!empty($headers[
'message-id']) && empty($headers[
'Message-ID'])) {
1628 $headers[
'Message-ID'] = $headers[
'message-id'];
1630 if (!empty($headers[
'subject']) && empty($headers[
'Subject'])) {
1631 $headers[
'Subject'] = $headers[
'subject'];
1639 dol_syslog(
"** Process email ".$iforemailloop.
" References: ".$headers[
'References'].
" Subject: ".$headers[
'Subject']);
1642 $trackidfoundintorecipienttype =
'';
1643 $trackidfoundintorecipientid = 0;
1646 if (preg_match(
'/\+(thi|ctc|use|mem|sub|proj|tas|con|tic|pro|ord|inv|spro|sor|sin|leav|stockinv|job|surv|salary)([0-9]+)@/', $emailto, $reg)) {
1647 $trackidfoundintorecipienttype = $reg[1];
1648 $trackidfoundintorecipientid = $reg[2];
1649 } elseif (preg_match(
'/\+emailing-(\w+)@/', $emailto, $reg)) {
1650 $trackidfoundintorecipienttype =
'emailing';
1651 $trackidfoundintorecipientid = $reg[1];
1655 if ($searchfilterdoltrackid > 0) {
1656 if (empty($trackidfoundintorecipienttype)) {
1657 if (empty($headers[
'References']) || !preg_match(
'/@'.preg_quote($host,
'/').
'/', $headers[
'References'])) {
1658 $nbemailprocessed++;
1659 dol_syslog(
" Discarded - No suffix in email recipient and no Header References found matching signature of application so with a trackid");
1664 if ($searchfilternodoltrackid > 0) {
1665 if (!empty($trackidfoundintorecipienttype) || (!empty($headers[
'References']) && preg_match(
'/@'.preg_quote($host,
'/').
'/', $headers[
'References']))) {
1666 $nbemailprocessed++;
1667 dol_syslog(
" Discarded - Suffix found into email or Header References found and matching signature of application so with a trackid");
1672 if ($searchfilterisanswer > 0) {
1673 if (empty($headers[
'In-Reply-To'])) {
1674 $nbemailprocessed++;
1675 dol_syslog(
" Discarded - Email is not an answer (no In-Reply-To header)");
1681 if (preg_match(
'/Re\s*:\s+/i', $headers[
'Subject'])) {
1688 $nbemailprocessed++;
1689 dol_syslog(
" Discarded - Email is not an answer (no RE prefix in subject)");
1693 if ($searchfilterisnotanswer > 0) {
1694 if (!empty($headers[
'In-Reply-To'])) {
1698 if (preg_match(
'/Re\s*:\s+/i', $headers[
'Subject'])) {
1704 $nbemailprocessed++;
1705 dol_syslog(
" Discarded - Email is an answer");
1713 $thirdpartystatic =
new Societe($this->db);
1714 $contactstatic =
new Contact($this->db);
1715 $projectstatic =
new Project($this->db);
1717 $nbactiondoneforemail = 0;
1719 $errorforactions = 0;
1720 $thirdpartyfoundby =
'';
1721 $contactfoundby =
'';
1722 $projectfoundby =
'';
1723 $ticketfoundby =
'';
1724 $candidaturefoundby =
'';
1727 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
1728 dol_syslog(
"msgid=".$overview[
'message_id'].
" date=".
dol_print_date($overview[
'date'],
'dayrfc',
'gmt').
" from=".$overview[
'from'].
" to=".$overview[
'to'].
" subject=".$overview[
'subject']);
1731 $overview[
'subject'] = preg_replace(
'/[\x{10000}-\x{10FFFF}]/u',
"\xEF\xBF\xBD", $overview[
'subject']);
1733 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);
1740 $overview[0]->subject = preg_replace(
'/[\x{10000}-\x{10FFFF}]/u',
"\xEF\xBF\xBD", $overview[0]->subject);
1743 global $htmlmsg, $plainmsg, $charset, $attachments;
1745 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
1746 if ($imapemail->hasHTMLBody()) {
1747 $htmlmsg = $imapemail->getHTMLBody();
1749 if ($imapemail->hasTextBody()) {
1750 $plainmsg = $imapemail->getTextBody();
1752 if ($imapemail->hasAttachments()) {
1753 $attachments = $imapemail->getAttachments()->all();
1758 $this->
getmsg($connection, $imapemail);
1771 $operationslog .=
'<br>Discarded - Email body is not valid utf8';
1772 dol_syslog(
" Discarded - Email body is not valid utf8");
1776 if (!empty($searchfilterexcludebodyarray)) {
1777 foreach ($searchfilterexcludebodyarray as $searchfilterexcludebody) {
1778 if (preg_match(
'/'.preg_quote($searchfilterexcludebody,
'/').
'/ms', $messagetext)) {
1779 $nbemailprocessed++;
1780 $operationslog .=
'<br>Discarded - Email body contains string '.$searchfilterexcludebody;
1781 dol_syslog(
" Discarded - Email body contains string ".$searchfilterexcludebody);
1843 $replytostring =
'';
1845 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
1846 $fromstring = $overview[
'from'];
1849 $sender = $overview[
'sender'];
1850 $to = $overview[
'to'];
1851 $sendtocc = empty($overview[
'cc']) ?
'' : $overview[
'cc'];
1852 $sendtobcc = empty($overview[
'bcc']) ?
'' : $overview[
'bcc'];
1853 $date = $overview[
'date'];
1854 $msgid = str_replace(array(
'<',
'>'),
'', $overview[
'message_id']);
1855 $subject = $overview[
'subject'];
1857 $fromstring = $overview[0]->from;
1860 $sender = $overview[0]->sender;
1861 $to = $overview[0]->to;
1862 $sendtocc = $overview[0]->cc;
1863 $sendtobcc = $overview[0]->bcc;
1864 $date = $overview[0]->udate;
1865 $msgid = str_replace(array(
'<',
'>'),
'', $overview[0]->message_id);
1866 $subject = $overview[0]->subject;
1870 if (!empty($searchfilterexcludesubjectarray)) {
1871 foreach ($searchfilterexcludesubjectarray as $searchfilterexcludesubject) {
1872 if (preg_match(
'/'.preg_quote($searchfilterexcludesubject,
'/').
'/ms', $subject)) {
1873 $nbemailprocessed++;
1874 $operationslog .=
'<br>Discarded - Email subject contains string '.$searchfilterexcludesubject;
1875 dol_syslog(
" Discarded - Email subject contains string ".$searchfilterexcludesubject);
1882 if (preg_match(
'/^(.*)<(.*)>$/', $fromstring, $reg)) {
1884 $fromtext = $reg[1];
1886 $from = $fromstring;
1889 if (preg_match(
'/^(.*)<(.*)>$/', $replytostring, $reg)) {
1891 $replytotext = $reg[1];
1893 $replyto = $replytostring;
1896 $fk_element_id = 0; $fk_element_type =
'';
1900 $contactid = 0; $thirdpartyid = 0; $projectid = 0; $ticketid = 0;
1908 $objectemail =
null;
1911 if (!empty($headers[
'References'])) {
1912 $arrayofreferences = preg_split(
'/(,|\s+)/', $headers[
'References']);
1916 foreach ($arrayofreferences as $reference) {
1918 if (!empty($trackidfoundintorecipienttype)) {
1919 $resultsearchtrackid = -1;
1920 $reg[1] = $trackidfoundintorecipienttype;
1921 $reg[2] = $trackidfoundintorecipientid;
1923 $resultsearchtrackid = preg_match(
'/dolibarr-([a-z]+)([0-9]+)@'.preg_quote($host,
'/').
'/', $reference, $reg);
1924 if (empty($resultsearchtrackid) &&
getDolGlobalString(
'EMAIL_ALTERNATIVE_HOST_SIGNATURE')) {
1925 $resultsearchtrackid = preg_match(
'/dolibarr-([a-z]+)([0-9]+)@'.preg_quote(
getDolGlobalString(
'EMAIL_ALTERNATIVE_HOST_SIGNATURE'),
'/').
'/', $reference, $reg);
1929 if (!empty($resultsearchtrackid)) {
1931 $trackid = $reg[1].$reg[2];
1933 $objectid = $reg[2];
1935 if ($reg[1] ==
'thi') {
1936 $objectemail =
new Societe($this->db);
1938 if ($reg[1] ==
'ctc') {
1939 $objectemail =
new Contact($this->db);
1941 if ($reg[1] ==
'inv') {
1942 $objectemail =
new Facture($this->db);
1944 if ($reg[1] ==
'sinv') {
1947 if ($reg[1] ==
'pro') {
1948 $objectemail =
new Propal($this->db);
1950 if ($reg[1] ==
'ord') {
1951 $objectemail =
new Commande($this->db);
1953 if ($reg[1] ==
'shi') {
1956 if ($reg[1] ==
'spro') {
1959 if ($reg[1] ==
'sord') {
1962 if ($reg[1] ==
'rec') {
1963 $objectemail =
new Reception($this->db);
1965 if ($reg[1] ==
'proj') {
1966 $objectemail =
new Project($this->db);
1968 if ($reg[1] ==
'tas') {
1969 $objectemail =
new Task($this->db);
1971 if ($reg[1] ==
'con') {
1972 $objectemail =
new Contact($this->db);
1974 if ($reg[1] ==
'use') {
1975 $objectemail =
new User($this->db);
1977 if ($reg[1] ==
'tic') {
1978 $objectemail =
new Ticket($this->db);
1980 if ($reg[1] ==
'recruitmentcandidature') {
1983 if ($reg[1] ==
'mem') {
1984 $objectemail =
new Adherent($this->db);
1992 } elseif (preg_match(
'/<(.*@.*)>/', $reference, $reg)) {
1994 if (!is_object($objectemail)) {
1995 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"ticket where email_msgid = '".$this->db->escape($reg[1]).
"'";
1996 $resql = $this->db->query($sql);
1998 $obj = $this->db->fetch_object($resql);
2000 $objectid = $obj->rowid;
2001 $objectemail =
new Ticket($this->db);
2002 $ticketfoundby = $langs->transnoentitiesnoconv(
"EmailMsgID").
' ('.$reg[1].
')';
2009 if (!is_object($objectemail)) {
2010 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"projet where email_msgid = '".$this->db->escape($reg[1]).
"'";
2011 $resql = $this->db->query($sql);
2013 $obj = $this->db->fetch_object($resql);
2015 $objectid = $obj->rowid;
2016 $objectemail =
new Project($this->db);
2017 $projectfoundby = $langs->transnoentitiesnoconv(
"EmailMsgID").
' ('.$reg[1].
')';
2024 if (!is_object($objectemail)) {
2025 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"recruitment_recruitmentcandidature where email_msgid = '".$this->db->escape($reg[1]).
"'";
2026 $resql = $this->db->query($sql);
2028 $obj = $this->db->fetch_object($resql);
2030 $objectid = $obj->rowid;
2032 $candidaturefoundby = $langs->transnoentitiesnoconv(
"EmailMsgID").
' ('.$reg[1].
')';
2041 if (is_object($objectemail)) {
2042 $result = $objectemail->fetch($objectid);
2044 $fk_element_id = $objectemail->id;
2045 $fk_element_type = $objectemail->element;
2047 if ($fk_element_type ==
'facture') {
2048 $fk_element_type =
'invoice';
2051 if (get_class($objectemail) !=
'Societe') {
2052 $thirdpartyid = $objectemail->fk_soc ?? $objectemail->socid;
2054 $thirdpartyid = $objectemail->id;
2057 if (get_class($objectemail) !=
'Contact') {
2058 $contactid = $objectemail->fk_socpeople;
2060 $contactid = $objectemail->id;
2063 if (get_class($objectemail) !=
'Project') {
2064 $projectid = isset($objectemail->fk_project) ? $objectemail->fk_project : $objectemail->fk_projet;
2066 $projectid = $objectemail->id;
2072 if ($projectid > 0) {
2073 $result = $projectstatic->fetch($projectid);
2075 $projectstatic->id = 0;
2077 $projectid = $projectstatic->id;
2079 $projectfoundby =
'trackid ('.$trackid.
')';
2081 if (empty($contactid)) {
2082 $contactid = $projectstatic->fk_contact;
2084 if (empty($thirdpartyid)) {
2085 $thirdpartyid = $projectstatic->fk_soc;
2090 if ($contactid > 0) {
2091 $result = $contactstatic->fetch($contactid);
2093 $contactstatic->id = 0;
2095 $contactid = $contactstatic->id;
2097 $contactfoundby =
'trackid ('.$trackid.
')';
2099 if (empty($thirdpartyid)) {
2100 $thirdpartyid = $contactstatic->fk_soc;
2105 if ($thirdpartyid > 0) {
2106 $result = $thirdpartystatic->fetch($thirdpartyid);
2108 $thirdpartystatic->id = 0;
2110 $thirdpartyid = $thirdpartystatic->id;
2112 $thirdpartyfoundby =
'trackid ('.$trackid.
')';
2117 if (is_object($objectemail)) {
2123 if (empty($contactid)) {
2124 $result = $contactstatic->fetch(0,
null,
'', $from);
2127 dol_syslog(
"We found a contact with the email ".$from);
2128 $contactid = $contactstatic->id;
2129 $contactfoundby =
'email of contact ('.$from.
')';
2130 if (empty($thirdpartyid) && $contactstatic->socid > 0) {
2131 $result = $thirdpartystatic->fetch($contactstatic->socid);
2133 $thirdpartyid = $thirdpartystatic->id;
2134 $thirdpartyfoundby =
'email of contact ('.$from.
')';
2140 if (empty($thirdpartyid)) {
2141 $result = $thirdpartystatic->fetch(0,
'',
'',
'',
'',
'',
'',
'',
'',
'', $from);
2143 dol_syslog(
"We found a thirdparty with the email ".$from);
2144 $thirdpartyid = $thirdpartystatic->id;
2145 $thirdpartyfoundby =
'email ('.$from.
')';
2181 foreach ($this->actions as $operation) {
2182 $errorforthisaction = 0;
2184 if ($errorforactions) {
2187 if (empty($operation[
'status'])) {
2191 $operationslog .=
'<br>* Process operation '.$operation[
'type'];
2194 dol_syslog(
"Execute action ".$operation[
'type'].
" actionparam=".$operation[
'actionparam'].
' thirdpartystatic->id='.$thirdpartystatic->id.
' contactstatic->id='.$contactstatic->id.
' projectstatic->id='.$projectstatic->id);
2195 dol_syslog(
"Execute action fk_element_id=".$fk_element_id.
" fk_element_type=".$fk_element_type);
2197 $actioncode =
'EMAIL_IN';
2199 if ($this->source_directory ==
'Sent') {
2200 $actioncode =
'EMAIL_OUT';
2203 $description = $descriptiontitle = $descriptionmeta = $descriptionfull =
'';
2205 $descriptiontitle = $langs->trans(
"RecordCreatedByEmailCollector", $this->ref, $msgid);
2208 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"MailFrom").($langs->trans(
"MailFrom") !=
'From' ?
' (From)' :
'').
' : '.
dol_escape_htmltag($fromstring));
2210 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"Sender").($langs->trans(
"Sender") !=
'Sender' ?
' (Sender)' :
'').
' : '.
dol_escape_htmltag($sender));
2212 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"MailTo").($langs->trans(
"MailTo") !=
'To' ?
' (To)' :
'').
' : '.
dol_escape_htmltag($to));
2214 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"MailCC").($langs->trans(
"MailCC") !=
'CC' ?
' (CC)' :
'').
' : '.
dol_escape_htmltag($sendtocc));
2218 if ($operation[
'type'] ==
'loadthirdparty' || $operation[
'type'] ==
'loadandcreatethirdparty') {
2219 if (empty($operation[
'actionparam'])) {
2221 $this->error =
"Action loadthirdparty or loadandcreatethirdparty has empty parameter. Must be a rule like 'name=HEADER:^From:(.*);' or 'name=SET:xxx' or 'name=EXTRACT:(body|subject):regex where 'name' can be replaced with 'id' or 'email' to define how to set or extract data. More properties can also be set, for example client=SET:2;";
2222 $this->errors[] = $this->error;
2224 $actionparam = $operation[
'actionparam'];
2225 $idtouseforthirdparty =
'';
2226 $nametouseforthirdparty =
'';
2227 $emailtouseforthirdparty =
'';
2228 $namealiastouseforthirdparty =
'';
2230 $operationslog .=
'<br>Loop on each property to set into actionparam';
2234 foreach ($arrayvaluetouse as $propertytooverwrite => $valueforproperty) {
2238 $regforregex = array();
2240 if (preg_match(
'/^EXTRACT:([a-zA-Z0-9_]+):(.*)$/', $valueforproperty, $regforregex)) {
2241 $sourcefield = $regforregex[1];
2242 $regexstring = $regforregex[2];
2245 if (!empty($sourcefield) && !empty($regexstring)) {
2246 if (strtolower($sourcefield) ==
'body') {
2247 $sourcestring = $messagetext;
2248 } elseif (strtolower($sourcefield) ==
'subject') {
2249 $sourcestring = $subject;
2250 } elseif (strtolower($sourcefield) ==
'header') {
2251 $sourcestring = $header;
2254 if ($sourcestring) {
2255 $regforval = array();
2257 if (preg_match(
'/'.$regexstring.
'/ms', $sourcestring, $regforval)) {
2260 if ($propertytooverwrite ==
'id') {
2261 $idtouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) :
null;
2263 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Found idtouseforthirdparty='.
dol_escape_htmltag($idtouseforthirdparty);
2264 } elseif ($propertytooverwrite ==
'email') {
2265 $emailtouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) :
null;
2267 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Found emailtouseforthirdparty='.
dol_escape_htmltag($emailtouseforthirdparty);
2268 } elseif ($propertytooverwrite ==
'name') {
2269 $nametouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) :
null;
2271 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Found nametouseforthirdparty='.
dol_escape_htmltag($nametouseforthirdparty);
2272 } elseif ($propertytooverwrite ==
'name_alias') {
2273 $namealiastouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) :
null;
2275 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Found namealiastouseforthirdparty='.
dol_escape_htmltag($namealiastouseforthirdparty);
2277 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> We discard this, not a field used to search an existing thirdparty';
2281 if (in_array($propertytooverwrite, array(
'id',
'email',
'name',
'name_alias'))) {
2282 $idtouseforthirdparty =
null;
2283 $nametouseforthirdparty =
null;
2284 $emailtouseforthirdparty =
null;
2285 $namealiastouseforthirdparty =
null;
2287 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Not found. Property searched is critical so we cancel the search.';
2289 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Not found';
2296 $this->error =
'The extract rule to use to load thirdparty for email '.$msgid.
' has an unknown source (must be HEADER, SUBJECT or BODY)';
2297 $this->errors[] = $this->error;
2299 $operationslog .=
'<br>'.$this->error;
2301 } elseif (preg_match(
'/^(SET|SETIFEMPTY):(.*)$/', $valueforproperty, $reg)) {
2305 if ($propertytooverwrite ==
'id') {
2306 $idtouseforthirdparty = $reg[2];
2308 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' We set property idtouseforthrdparty='.
dol_escape_htmltag($idtouseforthirdparty);
2309 } elseif ($propertytooverwrite ==
'email') {
2310 $emailtouseforthirdparty = $reg[2];
2312 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' We set property emailtouseforthrdparty='.
dol_escape_htmltag($emailtouseforthirdparty);
2313 } elseif ($propertytooverwrite ==
'name') {
2314 $nametouseforthirdparty = $reg[2];
2316 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' We set property nametouseforthirdparty='.
dol_escape_htmltag($nametouseforthirdparty);
2317 } elseif ($propertytooverwrite ==
'name_alias') {
2318 $namealiastouseforthirdparty = $reg[2];
2320 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' We set property namealiastouseforthirdparty='.
dol_escape_htmltag($namealiastouseforthirdparty);
2324 $this->error =
'Bad syntax for description of action parameters: '.$actionparam;
2325 $this->errors[] = $this->error;
2330 if (!$errorforactions && ($idtouseforthirdparty || $emailtouseforthirdparty || $nametouseforthirdparty || $namealiastouseforthirdparty)) {
2332 $operationslog .=
'<br>We have this data to search thirdparty: id='.$idtouseforthirdparty.
', email='.$emailtouseforthirdparty.
', name='.$nametouseforthirdparty.
', name_alias='.$namealiastouseforthirdparty;
2334 $tmpobject =
new stdClass();
2335 $tmpobject->element ==
'generic';
2336 $tmpobject->id = $idtouseforthirdparty;
2337 $tmpobject->name = $nametouseforthirdparty;
2338 $tmpobject->name_alias = $namealiastouseforthirdparty;
2339 $tmpobject->email = $emailtouseforthirdparty;
2343 $idtouseforthirdparty = $tmpobject->id;
2344 $nametouseforthirdparty = $tmpobject->name;
2345 $namealiastouseforthirdparty = $tmpobject->name_alias;
2346 $emailtouseforthirdparty = $tmpobject->email;
2348 $operationslog .=
'<br>We try to search existing thirdparty with '.$idtouseforthirdparty.
' '.$emailtouseforthirdparty.
' '.$nametouseforthirdparty.
' '.$namealiastouseforthirdparty;
2350 $result = $thirdpartystatic->fetch($idtouseforthirdparty, $nametouseforthirdparty,
'',
'',
'',
'',
'',
'',
'',
'', $emailtouseforthirdparty, $namealiastouseforthirdparty);
2353 $this->error =
'Error when getting thirdparty with name '.$nametouseforthirdparty.
' (may be 2 record exists with same name ?)';
2354 $this->errors[] = $this->error;
2356 } elseif ($result == 0) {
2357 if ($operation[
'type'] ==
'loadthirdparty') {
2358 dol_syslog(
"Third party with id=".$idtouseforthirdparty.
" email=".$emailtouseforthirdparty.
" name=".$nametouseforthirdparty.
" name_alias=".$namealiastouseforthirdparty.
" was not found");
2361 $resultContact = $contactstatic->fetch(
'',
'',
'', $emailtouseforthirdparty);
2362 if ($resultContact > 0) {
2363 $idtouseforthirdparty = $contactstatic->socid;
2364 $result = $thirdpartystatic->fetch($idtouseforthirdparty);
2366 dol_syslog(
"Third party with id=".$idtouseforthirdparty.
" email=".$emailtouseforthirdparty.
" name=".$nametouseforthirdparty.
" name_alias=".$namealiastouseforthirdparty.
" was found thanks to linked contact search");
2369 $langs->load(
"errors");
2370 $this->error = $langs->trans(
'ErrorFailedToLoadThirdParty', $idtouseforthirdparty, $emailtouseforthirdparty, $nametouseforthirdparty, $namealiastouseforthirdparty);
2371 $this->errors[] = $this->error;
2375 $langs->load(
"errors");
2376 $this->error = $langs->trans(
'ErrorFailedToLoadThirdParty', $idtouseforthirdparty, $emailtouseforthirdparty, $nametouseforthirdparty, $namealiastouseforthirdparty);
2377 $this->errors[] = $this->error;
2379 } elseif ($operation[
'type'] ==
'loadandcreatethirdparty') {
2380 dol_syslog(
"Third party with id=".$idtouseforthirdparty.
" email=".$emailtouseforthirdparty.
" name=".$nametouseforthirdparty.
" name_alias=".$namealiastouseforthirdparty.
" was not found. We try to create it.");
2383 $thirdpartystatic =
new Societe($db);
2384 $thirdpartystatic->name = $nametouseforthirdparty;
2385 if (!empty($namealiastouseforthirdparty)) {
2386 if ($namealiastouseforthirdparty != $nametouseforthirdparty) {
2387 $thirdpartystatic->name_alias = $namealiastouseforthirdparty;
2390 $thirdpartystatic->name_alias = (empty($replytostring) ? (empty($fromtext) ?
'': $fromtext) : $replytostring);
2392 $thirdpartystatic->email = (empty($emailtouseforthirdparty) ? (empty($replyto) ? (empty($from) ?
'' : $from) : $replyto) : $emailtouseforthirdparty);
2395 $errorforthisaction = $this->
overwritePropertiesOfObject($thirdpartystatic, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
2397 if ($thirdpartystatic->client && empty($thirdpartystatic->code_client)) {
2398 $thirdpartystatic->code_client =
'auto';
2400 if ($thirdpartystatic->fournisseur && empty($thirdpartystatic->code_fournisseur)) {
2401 $thirdpartystatic->code_fournisseur =
'auto';
2404 if ($errorforthisaction) {
2407 $result = $thirdpartystatic->create($user);
2410 $this->error = $thirdpartystatic->error;
2411 $this->errors = $thirdpartystatic->errors;
2413 $operationslog .=
'<br>Thirdparty created -> id = '.dol_escape_htmltag($thirdpartystatic->id);
2418 dol_syslog(
"One and only one existing third party has been found");
2420 $operationslog .=
'<br>Thirdparty already exists with id = '.dol_escape_htmltag($thirdpartystatic->id);
2424 } elseif ($operation[
'type'] ==
'loadandcreatecontact') {
2425 if (empty($operation[
'actionparam'])) {
2427 $this->error =
"Action loadandcreatecontact has empty parameter. Must be 'SET:xxx' or 'EXTRACT:(body|subject):regex' to define how to extract data";
2428 $this->errors[] = $this->error;
2430 $contact_static =
new Contact($this->db);
2432 $errorforthisaction = $this->
overwritePropertiesOfObject($contact_static, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
2433 if ($errorforthisaction) {
2436 if (!empty($contact_static->email) && $contact_static->email != $from) $from = $contact_static->email;
2438 $result = $contactstatic->fetch(0,
null,
'', $from);
2441 $this->error =
'Error when getting contact with email ' . $from;
2442 $this->errors[] = $this->error;
2444 } elseif ($result == 0) {
2445 dol_syslog(
"Contact with email " . $from .
" was not found. We try to create it.");
2446 $contactstatic =
new Contact($this->db);
2449 $contactstatic->email = $from;
2450 $operationslog .=
'<br>We set property email='.dol_escape_htmltag($from);
2453 $errorforthisaction = $this->
overwritePropertiesOfObject($contactstatic, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
2455 if ($errorforthisaction) {
2459 if (!empty($contactstatic->country)) {
2460 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
2461 $result =
getCountry(
'', 3, $this->db,
'', 1, $contactstatic->country);
2462 if ($result ==
'NotDefined') {
2464 $this->error =
"Error country not found by this name '" . $contactstatic->country .
"'";
2465 } elseif (!($result > 0)) {
2467 $this->error =
"Error when search country by this name '" . $contactstatic->country .
"'";
2468 $this->errors[] = $this->db->lasterror();
2470 $contactstatic->country_id = $result;
2471 $operationslog .=
'<br>We set property country_id='.dol_escape_htmltag($result);
2473 } elseif (!empty($contactstatic->country_code)) {
2474 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
2475 $result =
getCountry($contactstatic->country_code, 3, $this->db);
2476 if ($result ==
'NotDefined') {
2478 $this->error =
"Error country not found by this code '" . $contactstatic->country_code .
"'";
2479 } elseif (!($result > 0)) {
2481 $this->error =
"Error when search country by this code '" . $contactstatic->country_code .
"'";
2482 $this->errors[] = $this->db->lasterror();
2484 $contactstatic->country_id = $result;
2485 $operationslog .=
'<br>We set property country_id='.dol_escape_htmltag($result);
2489 if (!$errorforactions) {
2491 if (!empty($contactstatic->state)) {
2492 require_once DOL_DOCUMENT_ROOT .
'/core/lib/functions.lib.php';
2493 $result =
dol_getIdFromCode($this->db, $contactstatic->state,
'c_departements',
'nom',
'rowid');
2494 if (empty($result)) {
2496 $this->error =
"Error state not found by this name '" . $contactstatic->state .
"'";
2497 } elseif (!($result > 0)) {
2499 $this->error =
"Error when search state by this name '" . $contactstatic->state .
"'";
2500 $this->errors[] = $this->db->lasterror();
2502 $contactstatic->state_id = $result;
2503 $operationslog .=
'<br>We set property state_id='.dol_escape_htmltag($result);
2505 } elseif (!empty($contactstatic->state_code)) {
2506 require_once DOL_DOCUMENT_ROOT .
'/core/lib/functions.lib.php';
2507 $result =
dol_getIdFromCode($this->db, $contactstatic->state_code,
'c_departements',
'code_departement',
'rowid');
2508 if (empty($result)) {
2510 $this->error =
"Error state not found by this code '" . $contactstatic->state_code .
"'";
2511 } elseif (!($result > 0)) {
2513 $this->error =
"Error when search state by this code '" . $contactstatic->state_code .
"'";
2514 $this->errors[] = $this->db->lasterror();
2516 $contactstatic->state_id = $result;
2517 $operationslog .=
'<br>We set property state_id='.dol_escape_htmltag($result);
2522 if (!$errorforactions) {
2523 $result = $contactstatic->create($user);
2526 $this->error = $contactstatic->error;
2527 $this->errors = $contactstatic->errors;
2529 $operationslog .=
'<br>Contact created -> id = '.dol_escape_htmltag($contactstatic->id);
2536 } elseif ($operation[
'type'] ==
'recordevent') {
2540 $alreadycreated = $actioncomm->fetch(0,
'',
'', $msgid);
2541 if ($alreadycreated == 0) {
2542 if ($projectstatic->id > 0) {
2543 if ($projectfoundby) {
2544 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Project found from '.$projectfoundby);
2547 if ($thirdpartystatic->id > 0) {
2548 if ($thirdpartyfoundby) {
2549 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Third party found from '.$thirdpartyfoundby);
2552 if ($contactstatic->id > 0) {
2553 if ($contactfoundby) {
2554 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Contact/address found from '.$contactfoundby);
2558 $description = $descriptiontitle;
2564 $descriptionfull = $description;
2565 if (empty($conf->global->MAIN_EMAILCOLLECTOR_MAIL_WITHOUT_HEADER)) {
2566 $descriptionfull =
dol_concatdesc($descriptionfull,
"----- Header");
2571 $actioncomm->type_code =
'AC_OTH_AUTO';
2572 $actioncomm->code =
'AC_'.$actioncode;
2573 $actioncomm->label = $langs->trans(
"ActionAC_".$actioncode).
' - '.$langs->trans(
"MailFrom").
' '.$from;
2574 $actioncomm->note_private = $descriptionfull;
2575 $actioncomm->fk_project = $projectstatic->id;
2576 $actioncomm->datep = $date;
2577 $actioncomm->datef = $date;
2578 $actioncomm->percentage = -1;
2579 $actioncomm->socid = $thirdpartystatic->id;
2580 $actioncomm->contact_id = $contactstatic->id;
2581 $actioncomm->socpeopleassigned = (!empty($contactstatic->id) ? array($contactstatic->id =>
'') : array());
2582 $actioncomm->authorid = $user->id;
2583 $actioncomm->userownerid = $user->id;
2585 $actioncomm->email_msgid = $msgid;
2586 $actioncomm->email_from = $fromstring;
2587 $actioncomm->email_sender = $sender;
2588 $actioncomm->email_to = $to;
2589 $actioncomm->email_tocc = $sendtocc;
2590 $actioncomm->email_tobcc = $sendtobcc;
2591 $actioncomm->email_subject = $subject;
2592 $actioncomm->errors_to =
'';
2594 if (!in_array($fk_element_type, array(
'societe',
'contact',
'project',
'user'))) {
2595 $actioncomm->fk_element = $fk_element_id;
2596 $actioncomm->elementid = $fk_element_id;
2597 $actioncomm->elementtype = $fk_element_type;
2598 if (is_object($objectemail) && $objectemail->module) {
2599 $actioncomm->elementtype .=
'@'.$objectemail->module;
2606 $errorforthisaction = $this->
overwritePropertiesOfObject($actioncomm, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
2615 if ($errorforthisaction) {
2618 $result = $actioncomm->create($user);
2621 $this->errors = $actioncomm->errors;
2623 $operationslog .=
'<br>Event created -> id='.dol_escape_htmltag($actioncomm->id);
2627 } elseif ($operation[
'type'] ==
'recordjoinpiece') {
2629 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
2630 foreach ($attachments as $attachment) {
2631 if ($attachment->getName() ===
'undefined') {
2634 $data[$attachment->getName()] = $attachment->getContent();
2638 foreach ($pj as $key => $val) {
2639 $data[$val[
'filename']] =
getFileData($imapemail, $val[
'pos'], $val[
'type'], $connection);
2642 if (count($data) > 0) {
2643 $sql =
"SELECT rowid as id FROM ".MAIN_DB_PREFIX.
"user WHERE email LIKE '%".$this->db->escape($from).
"%'";
2644 $resql = $this->db->query($sql);
2645 if ($this->db->num_rows($resql) == 0) {
2646 $this->errors[] =
"User Not allowed to add documents ({$from})";
2648 $arrayobject = array(
2649 'propale' => array(
'table' =>
'propal',
2650 'fields' => array(
'ref'),
2651 'class' =>
'comm/propal/class/propal.class.php',
2652 'object' =>
'Propal'),
2653 'holiday' => array(
'table' =>
'holiday',
2654 'fields' => array(
'ref'),
2655 'class' =>
'holiday/class/holiday.class.php',
2656 'object' =>
'Holiday'),
2657 'expensereport' => array(
'table' =>
'expensereport',
2658 'fields' => array(
'ref'),
2659 'class' =>
'expensereport/class/expensereport.class.php',
2660 'object' =>
'ExpenseReport'),
2661 'recruitment/recruitmentjobposition' => array(
'table' =>
'recruitment_recruitmentjobposition',
2662 'fields' => array(
'ref'),
2663 'class' =>
'recruitment/class/recruitmentjobposition.class.php',
2664 'object' =>
'RecruitmentJobPosition'),
2665 'recruitment/recruitmentcandidature' => array(
'table' =>
'recruitment_recruitmentcandidature',
2666 'fields' => array(
'ref'),
2667 'class' =>
'recruitment/class/recruitmentcandidature.class.php',
2668 'object' =>
' RecruitmentCandidature'),
2669 'societe' => array(
'table' =>
'societe',
2670 'fields' => array(
'code_client',
'code_fournisseur'),
2671 'class' =>
'societe/class/societe.class.php',
2672 'object' =>
'Societe'),
2673 'commande' => array(
'table' =>
'commande',
2674 'fields' => array(
'ref'),
2675 'class' =>
'commande/class/commande.class.php',
2676 'object' =>
'Commande'),
2677 'expedition' => array(
'table' =>
'expedition',
2678 'fields' => array(
'ref'),
2679 'class' =>
'expedition/class/expedition.class.php',
2680 'object' =>
'Expedition'),
2681 'contract' => array(
'table' =>
'contrat',
2682 'fields' => array(
'ref'),
2683 'class' =>
'contrat/class/contrat.class.php',
2684 'object' =>
'Contrat'),
2685 'fichinter' => array(
'table' =>
'fichinter',
2686 'fields' => array(
'ref'),
2687 'class' =>
'fichinter/class/fichinter.class.php',
2688 'object' =>
'Fichinter'),
2689 'ticket' => array(
'table' =>
'ticket',
2690 'fields' => array(
'ref'),
2691 'class' =>
'ticket/class/ticket.class.php',
2692 'object' =>
'Ticket'),
2693 'knowledgemanagement' => array(
'table' =>
'knowledgemanagement_knowledgerecord',
2694 'fields' => array(
'ref'),
2695 'class' =>
'knowledgemanagement/class/knowledgemanagement.class.php',
2696 'object' =>
'KnowledgeRecord'),
2697 'supplier_proposal' => array(
'table' =>
'supplier_proposal',
2698 'fields' => array(
'ref'),
2699 'class' =>
'supplier_proposal/class/supplier_proposal.class.php',
2700 'object' =>
'SupplierProposal'),
2701 'fournisseur/commande' => array(
'table' =>
'commande_fournisseur',
2702 'fields' => array(
'ref',
'ref_supplier'),
2703 'class' =>
'fourn/class/fournisseur.commande.class.php',
2704 'object' =>
'SupplierProposal'),
2705 'facture' => array(
'table' =>
'facture',
2706 'fields' => array(
'ref'),
2707 'class' =>
'compta/facture/class/facture.class.php',
2708 'object' =>
'Facture'),
2709 'fournisseur/facture' => array(
'table' =>
'facture_fourn',
2710 'fields' => array(
'ref',
'ref_client'),
2711 'class' =>
'fourn/class/fournisseur.facture.class.php',
2712 'object' =>
'FactureFournisseur'),
2713 'produit' => array(
'table' =>
'product',
2714 'fields' => array(
'ref'),
2715 'class' =>
'product/class/product.class.php',
2716 'object' =>
'Product'),
2717 'productlot' => array(
'table' =>
'product_lot',
2718 'fields' => array(
'batch'),
2719 'class' =>
'product/stock/class/productlot.class.php',
2720 'object' =>
'Productlot'),
2721 'projet' => array(
'table' =>
'projet',
2722 'fields' => array(
'ref'),
2723 'class' =>
'projet/class/projet.class.php',
2724 'object' =>
'Project'),
2725 'projet_task' => array(
'table' =>
'projet_task',
2726 'fields' => array(
'ref'),
2727 'class' =>
'projet/class/task.class.php',
2728 'object' =>
'Task'),
2729 'ressource' => array(
'table' =>
'resource',
2730 'fields' => array(
'ref'),
2731 'class' =>
'ressource/class/dolressource.class.php',
2732 'object' =>
'Dolresource'),
2733 'bom' => array(
'table' =>
'bom_bom',
2734 'fields' => array(
'ref'),
2735 'class' =>
'bom/class/bom.class.php',
2737 'mrp' => array(
'table' =>
'mrp_mo',
2738 'fields' => array(
'ref'),
2739 'class' =>
'mrp/class/mo.class.php',
2743 if (!is_object($hookmanager)) {
2744 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
2747 $hookmanager->initHooks(array(
'emailcolector'));
2748 $parameters = array(
'arrayobject' => $arrayobject);
2749 $reshook = $hookmanager->executeHooks(
'addmoduletoeamailcollectorjoinpiece', $parameters);
2751 $arrayobject = $hookmanager->resArray;
2754 $resultobj = array();
2756 foreach ($arrayobject as $key => $objectdesc) {
2757 $sql =
'SELECT DISTINCT t.rowid ';
2758 $sql .=
' FROM ' . MAIN_DB_PREFIX . $objectdesc[
'table'] .
' AS t';
2760 foreach ($objectdesc[
'fields'] as $field) {
2761 $sql .=
"('" .$this->db->escape($subject) .
"' LIKE CONCAT('%', t." . $field .
", '%') AND t." . $field .
"<>'') OR ";
2763 $sql = substr($sql, 0, -4);
2765 $ressqlobj = $this->db->query($sql);
2767 while ($obj = $this->db->fetch_object($ressqlobj)) {
2768 $resultobj[$key][] = $obj->rowid;
2773 foreach ($resultobj as $mod => $ids) {
2774 $moddesc = $arrayobject[$mod];
2775 $elementpath = $mod;
2777 $objectmanaged =
new $moddesc[
'object']($this->db);
2778 foreach ($ids as $val) {
2779 $res = $objectmanaged->fetch($val);
2781 $path = ($objectmanaged->entity > 1 ?
"/" . $objectmanaged->entity :
'');
2782 $dirs[] = DOL_DATA_ROOT . $path .
"/" . $elementpath .
'/' .
dol_sanitizeFileName($objectmanaged->ref) .
'/';
2784 $this->errors[] =
'object not found';
2788 foreach ($dirs as $target) {
2789 $prefix = $this->actions[$this->id][
'actionparam'];
2790 foreach ($data as $filename => $content) {
2791 $resr =
saveAttachment($target, $prefix .
'_' . $filename, $content);
2793 $this->errors[] =
'Doc not saved';
2798 $operationslog .=
'<br>Save attachment files on disk';
2800 $this->errors[] =
'no joined piece';
2802 $operationslog .=
'<br>No joinded files';
2804 } elseif ($operation[
'type'] ==
'project') {
2806 $projecttocreate =
new Project($this->db);
2807 $alreadycreated = $projecttocreate->fetch(0,
'',
'', $msgid);
2808 if ($alreadycreated == 0) {
2809 if ($thirdpartystatic->id > 0) {
2810 $projecttocreate->socid = $thirdpartystatic->id;
2811 if ($thirdpartyfoundby) {
2812 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Third party found from '.$thirdpartyfoundby);
2815 if ($contactstatic->id > 0) {
2816 $projecttocreate->contact_id = $contactstatic->id;
2817 if ($contactfoundby) {
2818 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Contact/address found from '.$contactfoundby);
2822 $description = $descriptiontitle;
2828 $descriptionfull = $description;
2829 if (empty($conf->global->MAIN_EMAILCOLLECTOR_MAIL_WITHOUT_HEADER)) {
2830 $descriptionfull =
dol_concatdesc($descriptionfull,
"----- Header");
2834 $id_opp_status =
dol_getIdFromCode($this->db,
'PROSP',
'c_lead_status',
'code',
'rowid');
2835 $percent_opp_status =
dol_getIdFromCode($this->db,
'PROSP',
'c_lead_status',
'code',
'percent');
2837 $projecttocreate->title = $subject;
2838 $projecttocreate->date_start = $date;
2839 $projecttocreate->date_end =
'';
2840 $projecttocreate->opp_status = $id_opp_status;
2841 $projecttocreate->opp_percent = $percent_opp_status;
2843 $projecttocreate->note_private = $descriptionfull;
2844 $projecttocreate->entity = $conf->entity;
2845 $projecttocreate->email_msgid = $msgid;
2847 $savesocid = $projecttocreate->socid;
2851 $errorforthisaction = $this->
overwritePropertiesOfObject($projecttocreate, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
2854 if (empty($projecttocreate->ref)) {
2857 $modele = empty($conf->global->PROJECT_ADDON) ?
'mod_project_simple' : $conf->global->PROJECT_ADDON;
2860 $file =
''; $classname =
''; $filefound = 0; $reldir =
'';
2861 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
2862 foreach ($dirmodels as $reldir) {
2863 $file =
dol_buildpath($reldir.
"core/modules/project/".$modele.
'.php', 0);
2864 if (file_exists($file)) {
2866 $classname = $modele;
2872 if ($savesocid > 0) {
2873 if ($savesocid != $projecttocreate->socid) {
2875 setEventMessages(
'You loaded a thirdparty (id='.$savesocid.
') and you force another thirdparty id (id='.$projecttocreate->socid.
') by setting socid in operation with a different value',
null,
'errors');
2878 if ($projecttocreate->socid > 0) {
2879 $thirdpartystatic->fetch($projecttocreate->socid);
2883 $result =
dol_include_once($reldir.
"core/modules/project/".$modele.
'.php');
2884 $modModuleToUseForNextValue =
new $classname;
2885 $defaultref = $modModuleToUseForNextValue->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic :
null), $projecttocreate);
2887 $projecttocreate->ref = $defaultref;
2891 if ($errorforthisaction) {
2894 if (empty($projecttocreate->ref) || (is_numeric($projecttocreate->ref) && $projecttocreate->ref <= 0)) {
2896 $this->error =
'Failed to create project: Can\'t get a valid value for the field ref with numbering template = '.$modele.
', thirdparty id = '.$thirdpartystatic->id;
2898 $operationslog .=
'<br>'.$this->error;
2901 $result = $projecttocreate->create($user);
2904 $this->error =
'Failed to create project: '.$langs->trans($projecttocreate->error);
2905 $this->errors = $projecttocreate->errors;
2907 $operationslog .=
'<br>'.$this->error;
2910 $destdir = $conf->project->dir_output.
'/'.$projecttocreate->ref;
2914 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
2915 foreach ($attachments as $attachment) {
2916 $attachment->save($destdir.
'/');
2919 $this->
getmsg($connection, $imapemail, $destdir);
2922 $operationslog .=
'<br>Project created with attachments -> id='.dol_escape_htmltag($projecttocreate->id);
2924 $operationslog .=
'<br>Project created without attachments -> id='.dol_escape_htmltag($projecttocreate->id);
2932 $operationslog .=
'<br>Project already exists for msgid ='.dol_escape_htmltag($msgid);
2934 } elseif ($operation[
'type'] ==
'ticket') {
2936 $tickettocreate =
new Ticket($this->db);
2938 $alreadycreated = $tickettocreate->fetch(0,
'',
'', $msgid);
2939 if ($alreadycreated == 0) {
2940 if ($thirdpartystatic->id > 0) {
2941 $tickettocreate->socid = $thirdpartystatic->id;
2942 $tickettocreate->fk_soc = $thirdpartystatic->id;
2943 if ($thirdpartyfoundby) {
2944 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Third party found from '.$thirdpartyfoundby);
2947 if ($contactstatic->id > 0) {
2948 $tickettocreate->contact_id = $contactstatic->id;
2949 if ($contactfoundby) {
2950 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Contact/address found from '.$contactfoundby);
2954 $description = $descriptiontitle;
2960 $descriptionfull = $description;
2961 if (empty($conf->global->MAIN_EMAILCOLLECTOR_MAIL_WITHOUT_HEADER)) {
2962 $descriptionfull =
dol_concatdesc($descriptionfull,
"----- Header");
2966 $tickettocreate->subject = $subject;
2967 $tickettocreate->message = $description;
2968 $tickettocreate->type_code = (!empty($conf->global->MAIN_EMAILCOLLECTOR_TICKET_TYPE_CODE) ? $conf->global->MAIN_EMAILCOLLECTOR_TICKET_TYPE_CODE :
dol_getIdFromCode($this->db, 1,
'c_ticket_type',
'use_default',
'code', 1));
2969 $tickettocreate->category_code = (!empty($conf->global->MAIN_EMAILCOLLECTOR_TICKET_CATEGORY_CODE) ? $conf->global->MAIN_EMAILCOLLECTOR_TICKET_CATEGORY_CODE :
dol_getIdFromCode($this->db, 1,
'c_ticket_category',
'use_default',
'code', 1));
2970 $tickettocreate->severity_code = (!empty($conf->global->MAIN_EMAILCOLLECTOR_TICKET_SEVERITY_CODE) ? $conf->global->MAIN_EMAILCOLLECTOR_TICKET_SEVERITY_CODE :
dol_getIdFromCode($this->db, 1,
'c_ticket_severity',
'use_default',
'code', 1));
2971 $tickettocreate->origin_email = $from;
2972 $tickettocreate->fk_user_create = $user->id;
2973 $tickettocreate->datec =
dol_now();
2974 $tickettocreate->fk_project = $projectstatic->id;
2975 $tickettocreate->notify_tiers_at_create = 0;
2976 $tickettocreate->note_private = $descriptionfull;
2977 $tickettocreate->entity = $conf->entity;
2978 $tickettocreate->email_msgid = $msgid;
2979 $tickettocreate->email_date = $date;
2982 $savesocid = $tickettocreate->socid;
2986 $errorforthisaction = $this->
overwritePropertiesOfObject($tickettocreate, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
2989 if (empty($tickettocreate->ref)) {
2992 $modele = empty($conf->global->TICKET_ADDON) ?
'mod_ticket_simple' : $conf->global->TICKET_ADDON;
2995 $file =
''; $classname =
''; $filefound = 0; $reldir =
'';
2996 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
2997 foreach ($dirmodels as $reldir) {
2998 $file =
dol_buildpath($reldir.
"core/modules/ticket/".$modele.
'.php', 0);
2999 if (file_exists($file)) {
3001 $classname = $modele;
3007 if ($savesocid > 0) {
3008 if ($savesocid != $tickettocreate->socid) {
3010 setEventMessages(
'You loaded a thirdparty (id='.$savesocid.
') and you force another thirdparty id (id='.$tickettocreate->socid.
') by setting socid in operation with a different value',
null,
'errors');
3013 if ($tickettocreate->socid > 0) {
3014 $thirdpartystatic->fetch($tickettocreate->socid);
3019 $modModuleToUseForNextValue =
new $classname;
3020 $defaultref = $modModuleToUseForNextValue->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic :
null), $tickettocreate);
3022 $tickettocreate->ref = $defaultref;
3025 if ($errorforthisaction) {
3028 if (is_numeric($tickettocreate->ref) && $tickettocreate->ref <= 0) {
3030 $this->error =
'Failed to create ticket: Can\'t get a valid value for the field ref with numbering template = '.$modele.
', thirdparty id = '.$thirdpartystatic->id;
3033 $result = $tickettocreate->create($user);
3036 $this->error =
'Failed to create ticket: '.$langs->trans($tickettocreate->error);
3037 $this->errors = $tickettocreate->errors;
3040 $destdir = $conf->ticket->dir_output.
'/'.$tickettocreate->ref;
3044 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
3045 foreach ($attachments as $attachment) {
3046 $attachment->save($destdir.
'/');
3049 $this->
getmsg($connection, $imapemail, $destdir);
3052 $operationslog .=
'<br>Ticket created with attachments -> id='.dol_escape_htmltag($tickettocreate->id);
3054 $operationslog .=
'<br>Ticket created without attachments -> id='.dol_escape_htmltag($tickettocreate->id);
3060 } elseif ($operation[
'type'] ==
'candidature') {
3064 $alreadycreated = $candidaturetocreate->fetch(0,
'', $msgid);
3065 if ($alreadycreated == 0) {
3066 $description = $descriptiontitle;
3072 $descriptionfull = $description;
3073 $descriptionfull =
dol_concatdesc($descriptionfull,
"----- Header");
3076 $candidaturetocreate->subject = $subject;
3077 $candidaturetocreate->message = $description;
3078 $candidaturetocreate->type_code = 0;
3079 $candidaturetocreate->category_code =
null;
3080 $candidaturetocreate->severity_code =
null;
3081 $candidaturetocreate->email = $from;
3083 $candidaturetocreate->fk_user_creat = $user->id;
3084 $candidaturetocreate->date_creation =
dol_now();
3085 $candidaturetocreate->fk_project = $projectstatic->id;
3086 $candidaturetocreate->description = $description;
3087 $candidaturetocreate->note_private = $descriptionfull;
3088 $candidaturetocreate->entity = $conf->entity;
3089 $candidaturetocreate->email_msgid = $msgid;
3090 $candidaturetocreate->email_date = $date;
3091 $candidaturetocreate->status = $candidaturetocreate::STATUS_DRAFT;
3096 $errorforthisaction = $this->
overwritePropertiesOfObject($candidaturetocreate, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
3138 if ($errorforthisaction) {
3142 $result = $candidaturetocreate->create($user);
3145 $this->error =
'Failed to create candidature: '.join(
', ', $candidaturetocreate->errors);
3146 $this->errors = $candidaturetocreate->errors;
3149 $operationslog .=
'<br>Candidature created without attachments -> id='.dol_escape_htmltag($candidaturetocreate->id);
3152 } elseif (substr($operation[
'type'], 0, 4) ==
'hook') {
3155 if (!is_object($hookmanager)) {
3156 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
3160 $parameters = array(
3161 'connection'=> $connection,
3162 'imapemail'=>$imapemail,
3163 'overview'=>$overview,
3166 'fromtext' => $fromtext,
3168 'actionparam'=> $operation[
'actionparam'],
3170 'thirdpartyid' => $thirdpartyid,
3171 'objectid'=> $objectid,
3172 'objectemail'=> $objectemail,
3174 'messagetext'=>$messagetext,
3175 'subject'=>$subject,
3177 'attachments'=>$attachments,
3179 $reshook = $hookmanager->executeHooks(
'doCollectImapOneCollector', $parameters, $this, $operation[
'type']);
3182 $errorforthisaction++;
3183 $this->error = $hookmanager->resPrint;
3185 if ($errorforthisaction) {
3187 $operationslog .=
'<br>Hook doCollectImapOneCollector executed with error';
3189 $operationslog .=
'<br>Hook doCollectImapOneCollector executed without error';
3193 if (!$errorforactions) {
3194 $nbactiondoneforemail++;
3200 if (!$errorforactions) {
3201 if (!empty($targetdir)) {
3202 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
3204 dol_syslog(
"EmailCollector::doCollectOneCollector move message ".($imapemail->getHeader()->get(
'subject')).
" to ".$targetdir, LOG_DEBUG);
3206 $imapemail->move($targetdir);
3209 dol_syslog(
"EmailCollector::doCollectOneCollector move message ".($this->
uidAsString($imapemail)).
" to ".$connectstringtarget, LOG_DEBUG);
3210 $operationslog .=
'<br>Move mail '.($this->uidAsString($imapemail)).
' - '.$msgid;
3212 $arrayofemailtodelete[$imapemail] = $msgid;
3215 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
3216 dol_syslog(
"EmailCollector::doCollectOneCollector message '".($imapemail->getHeader()->get(
'subject')).
"' using this->host=".$this->host.
", this->access_type=".$this->acces_type.
" was set to read", LOG_DEBUG);
3218 dol_syslog(
"EmailCollector::doCollectOneCollector message ".($this->
uidAsString($imapemail)).
" to ".$connectstringtarget.
" was set to read", LOG_DEBUG);
3226 unset($objectemail);
3227 unset($projectstatic);
3228 unset($thirdpartystatic);
3229 unset($contactstatic);
3231 $nbemailprocessed++;
3233 if (!$errorforemail) {
3234 $nbactiondone += $nbactiondoneforemail;
3238 $this->db->commit();
3240 $this->db->rollback();
3244 if ($this->maxemailpercollect > 0 && $nbemailok >= $this->maxemailpercollect) {
3245 dol_syslog(
"EmailCollect::doCollectOneCollector We reach maximum of ".$nbemailok.
" collected with success, so we stop this collector now.");
3251 $this->db->rollback();
3255 $output = $langs->trans(
'XEmailsDoneYActionsDone', $nbemailprocessed, $nbemailok, $nbactiondone);
3257 dol_syslog(
"End of loop on emails", LOG_INFO, -1);
3259 $langs->load(
"admin");
3260 $output = $langs->trans(
'NoNewEmailToProcess');
3264 if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
3265 $client->disconnect();
3267 foreach ($arrayofemailtodelete as $imapemail => $msgid) {
3268 dol_syslog(
"EmailCollect::doCollectOneCollector delete email ".$imapemail.
" ".$msgid);
3270 $operationslog .=
"<br> delete email ".$imapemail.
" ".$msgid;
3272 if (empty($mode) && empty($error)) {
3273 $res = imap_mail_move($connection, $imapemail, $targetdir, CP_UID);
3274 if ($res ==
false) {
3276 $this->error = imap_last_error();
3277 $this->errors[] = $this->error;
3279 $operationslog .=
'<br>Error in move '.$this->error;
3286 if (empty($mode) && empty($error)) {
3288 $operationslog .=
"<br>Expunge";
3290 imap_expunge($connection);
3292 imap_close($connection);
3295 $this->datelastresult = $now;
3296 $this->lastresult = $output;
3297 $this->debuginfo .=
'IMAP search string used : '.$search;
3299 $this->debuginfo .=
'<br>Then search string into email header : '.dol_escape_htmltag($searchhead);
3301 if ($operationslog) {
3302 $this->debuginfo .= $operationslog;
3305 if (empty($error) && empty($mode)) {
3306 $this->datelastok = $now;
3309 if (!empty($this->errors)) {
3310 $this->lastresult .=
"<br>".join(
"<br>", $this->errors);
3312 $this->codelastresult = ($error ?
'KO' :
'OK');
3318 dol_syslog(
"EmailCollector::doCollectOneCollector end", LOG_INFO);
3320 return $error ? -1 : 1;