Can use the login/pass with PHP-IMAP

This commit is contained in:
Laurent Destailleur 2022-08-24 02:17:53 +02:00
parent 5071c3d626
commit fcbe933523

View File

@ -1022,7 +1022,9 @@ class EmailCollector extends CommonObject
$this->fetchFilters(); $this->fetchFilters();
$this->fetchActions(); $this->fetchActions();
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
if ($this->acces_type == 1) {
// Mode OAUth2 with PHP-IMAP
require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
$keyforsupportedoauth2array = $this->oauth_service; $keyforsupportedoauth2array = $this->oauth_service;
if (preg_match('/^.*-/', $keyforsupportedoauth2array)) { if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
@ -1036,7 +1038,8 @@ class EmailCollector extends CommonObject
$OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : '')); $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : ''));
require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php'; 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; //$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;
//dol_syslog($debugtext);
$storage = new DoliStorage($db, $conf); $storage = new DoliStorage($db, $conf);
@ -1082,7 +1085,7 @@ class EmailCollector extends CommonObject
$cm = new ClientManager(); $cm = new ClientManager();
$client = $cm->make([ $client = $cm->make([
'host' => $this->host, 'host' => $this->host,
'port' => 993, 'port' => $this->port,
'encryption' => 'ssl', 'encryption' => 'ssl',
'validate_cert' => true, 'validate_cert' => true,
'protocol' => 'imap', 'protocol' => 'imap',
@ -1090,6 +1093,20 @@ class EmailCollector extends CommonObject
'password' => $token, 'password' => $token,
'authentication' => "oauth", 'authentication' => "oauth",
]); ]);
} else {
// Mode login/pass with PHP-IMAP
$cm = new ClientManager();
$client = $cm->make([
'host' => $this->host,
'port' => $this->port,
'encryption' => 'ssl',
'validate_cert' => true,
'protocol' => 'imap',
'username' => $this->login,
'password' => $this->password,
'authentication' => "login",
]);
}
try { try {
$client->connect(); $client->connect();
@ -1102,6 +1119,7 @@ class EmailCollector extends CommonObject
$host = dol_getprefix('email'); $host = dol_getprefix('email');
} else { } else {
// Use native IMAP functions
if (!function_exists('imap_open')) { if (!function_exists('imap_open')) {
$this->error = 'IMAP function not enabled on your PHP'; $this->error = 'IMAP function not enabled on your PHP';
return -2; return -2;
@ -1115,7 +1133,7 @@ class EmailCollector extends CommonObject
$connection = imap_open($connectstringsource, $this->login, $this->password); $connection = imap_open($connectstringsource, $this->login, $this->password);
if (!$connection) { if (!$connection) {
$this->error = 'Failed to open IMAP connection '.$connectstringsource; $this->error = 'Failed to open IMAP connection '.$connectstringsource.' '.imap_last_error();
return -3; return -3;
} }
imap_errors(); // Clear stack of errors. imap_errors(); // Clear stack of errors.
@ -1129,7 +1147,7 @@ class EmailCollector extends CommonObject
//$search='ALL'; //$search='ALL';
} }
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
$criteria = array(array('UNDELETED')); // Seems not supported by some servers $criteria = array(array('UNDELETED')); // Seems not supported by some servers
$search = ''; $search = '';
$searchhead = ''; $searchhead = '';
@ -1341,7 +1359,7 @@ 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) && $this->acces_type == 1) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
try { try {
//$criteria = [['ALL']]; //$criteria = [['ALL']];
//$Query = $client->getFolders()[0]->messages()->where($criteria); //$Query = $client->getFolders()[0]->messages()->where($criteria);
@ -1412,11 +1430,12 @@ class EmailCollector extends CommonObject
// GET header and overview datas // GET header and overview datas
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
$header = $imapemail->getHeader()->raw; $header = $imapemail->getHeader()->raw;
$overview = $imapemail->getAttributes(); $overview = $imapemail->getAttributes();
} else { } else {
$header = imap_headerinfo($connection, $imapemail); //$header = imap_headerinfo($connection, $imapemail);
$header = imap_fetchheader($connection, $imapemail, 0);
$overview = imap_fetch_overview($connection, $imapemail, 0); $overview = imap_fetch_overview($connection, $imapemail, 0);
} }
@ -1446,7 +1465,6 @@ 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']);
// 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'])) {
@ -1520,9 +1538,10 @@ class EmailCollector extends CommonObject
$this->db->begin(); $this->db->begin();
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
dol_syslog("msgid=".$overview['message_id']." date=".dol_print_date($overview['date'], 'dayrfc', 'gmt')." from=".$overview['from']." to=".$overview['to']." subject=".$overview['subject']); dol_syslog("msgid=".$overview['message_id']." date=".dol_print_date($overview['date'], 'dayrfc', 'gmt')." from=".$overview['from']." to=".$overview['to']." subject=".$overview['subject']);
// Removed emojis
$overview['subject'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $overview['subject']); $overview['subject'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $overview['subject']);
} else { } 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);
@ -1538,7 +1557,7 @@ class EmailCollector extends CommonObject
global $htmlmsg, $plainmsg, $charset, $attachments; global $htmlmsg, $plainmsg, $charset, $attachments;
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
if ($imapemail->hasHTMLBody()) { if ($imapemail->hasHTMLBody()) {
$htmlmsg = $imapemail->getHTMLBody(); $htmlmsg = $imapemail->getHTMLBody();
} }
@ -1614,7 +1633,7 @@ class EmailCollector extends CommonObject
//print $messagetext; //print $messagetext;
//exit; //exit;
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
$fromstring = $overview['from']; $fromstring = $overview['from'];
$sender = $overview['sender']; $sender = $overview['sender'];
@ -2514,7 +2533,7 @@ 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) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
$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; return -1;
@ -2663,7 +2682,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 (empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
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);
@ -2675,6 +2694,7 @@ 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);
// TODO Make the move
} }
} }
} else { } else {
@ -2714,13 +2734,14 @@ class EmailCollector extends CommonObject
$output = $langs->trans('NoNewEmailToProcess'); $output = $langs->trans('NoNewEmailToProcess');
} }
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP) && $this->acces_type == 1) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
$client->disconnect(); $client->disconnect();
} else { } 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;