salesagility / SuiteCRM

SuiteCRM - Open source CRM for the world
https://www.suitecrm.com
GNU Affero General Public License v3.0
4.39k stars 2.06k forks source link

Error in Email: An Imap error detected: “IMAP close error” & ImapHandler trying to use a non valid resource stream - SuiteCRM 8.4.2, also tested on SuiteCRM 7.14.4 (same issue) #10472

Open Ignacio78-86 opened 1 month ago

Ignacio78-86 commented 1 month ago

Issue

I’,m trying help to a small business and I am trying to use CRM Suite 8.4.2, and I have 2 problems that I think are associated with the same thing.

Admin → Inbound Mail (settings): when I set my “INBOUND EMAIL ACCOUNTS” → edit: select a “MONITORED FOLDERS:*” (I can attach my folders without problems) and then when I put donde, in the general log (suitecrm.log) apears: [8505][1][FATAL] An Imap error detected: “[8505][1][FATAL] An Imap error detected: “IMAP close error””, may be the issue is in the code 1 or 2 attached in the .txt. (test options for imap works good (I think))

when I access my email, although it is true I can see what is in the inbox, but when I select sent or another folder, the log at that moment (seen by the console through a tail) gives me the error : “[1][FATAL] ImapHandler trying to use a non valid resource stream.” I have uploaded 2 example photos, I think the error is in: /var/www/SuiteCRM/public/legacy/modules/InboundEmail/InboundEmail.php

but I don’t know what should be corrected

Issues 1 and 2

Possible Fix

solution could be in this part of the code (line 6508)


/**
 * Attempt to create an IMAP connection using passed in parameters
 * return either the connection resource or false if unable to connect
 *
 * @param string $mailbox Mailbox to be used to create imap connection
 * @param string $username The user name
 * @param string $password The password associated with the username
 * @param integer $options Bitmask for options parameter to the imap_open function
 *
 * @return resource|boolean  Connection resource on success, FALSE on failure
 */
protected function getImapConnection($mailbox, $username, $password, $options = 0)
{
    $connection = null;
    $authenticators = ['', 'GSSAPI', 'NTLM'];

    $isOAuth = $this->isOAuth();
    if ($isOAuth === true) {
        $token = $this->getOAuthToken($this->external_oauth_connection_id ?? '');

        if ($token === null) {
            return false;
        }

        $password = $token;
    }

    while (!$connection && ($authenticator = array_shift($authenticators)) !== null) {
        if ($authenticator) {
            $params = [
                'DISABLE_AUTHENTICATOR' => $authenticator,
            ];
        } else {
            $params = [];
        }

        $connection = $this->getImap()->open($mailbox, $username, $password, $options, 0, $params);

        if (!$connection){
            break;
        }

    }

    return $connection;
}

or solution could be in this part of the code: (line 7417)

/**
 * fills InboundEmail->email with an email's details
 * @param int uid Unique ID of email
 * @param bool isMsgNo flag that passed ID is msgNo, default false
 * @param bool setRead Sets the 'seen' flag in cache
 * @param bool forceRefresh Skips cache file
 * @return string
 */
public function setEmailForDisplay($uid, $isMsgNo = false, $setRead = false, $forceRefresh = false)
{

    if (empty($uid)) {
        $GLOBALS['log']->debug("*** ERROR: INBOUNDEMAIL trying to setEmailForDisplay() with no UID");

        return 'NOOP';
    }

    global $sugar_config;
    global $app_strings;

    $cacheFile = [];
    // if its a pop3 then get the UIDL and see if this file name exist or not
    if ($this->isPop3Protocol()) {
        // get the UIDL from database;
        $cachedUIDL = md5($uid);
        $cache = "{$this->EmailCachePath}/{$this->id}/messages/{$this->mailbox}{$cachedUIDL}.php";
    } else {
        $cache = "{$this->EmailCachePath}/{$this->id}/messages/{$this->mailbox}{$uid}.php";
    }

    if (isset($cache) && strpos($cache, "..") !== false) {
        die("Directory navigation attack denied.");
    }

    if (file_exists($cache) && !$forceRefresh) {
        $GLOBALS['log']->info("INBOUNDEMAIL: Using cache file for setEmailForDisplay()");

        include($cache); // profides $cacheFile
        /** @var $cacheFile array */

        $metaOut = unserialize($cacheFile['out']);
        $meta = $metaOut['meta']['email'];
        $email = BeanFactory::newBean('Emails');

        foreach ($meta as $k => $v) {
            $email->$k = $v;
        }

        $email->to_addrs = $meta['toaddrs'];
        $email->date_sent_received = $meta['date_start'];

        $this->email = $email;
        $this->email->email2init();
        $ret = 'cache';
    } else {
        $GLOBALS['log']->info("INBOUNDEMAIL: opening new connection for setEmailForDisplay()");
        if ($this->isPop3Protocol()) {
            $msgNo = $this->getCorrectMessageNoForPop3($uid);
        } else {
            if (empty($this->conn)) {
                $this->connectMailserver();
            }
            $msgNo = ($isMsgNo) ? $uid : $this->getImap()->getMessageNo($uid);
        }
        if (empty($this->conn)) {
            $status = $this->connectMailserver();
            if ($status == "false") {
                $this->email = BeanFactory::newBean('Emails');
                $this->email->name = $app_strings['LBL_EMAIL_ERROR_MAILSERVERCONNECTION'];
                $ret = 'error';

                return $ret;
            }
        }

        $this->returnImportedEmail($msgNo, $uid, true);
        $this->email->id = '';
        $this->email->new_with_id = false;
        $ret = 'import';
    }

    if ($setRead) {
        $this->setStatuses($uid, 'seen', 1);
    }

    return $ret;
}

Steps to Reproduce the Issue

1.I have configured the email has described in this video: https://www.youtube.com/watch?v=WE4qjlRDA8o
2. then, access in your menu "emails" and then try to view your inbound emails and nothing happens. 

3. topic also is described here:

https://community.suitecrm.com/t/error-in-email-an-imap-error-detected-imap-close-error-imaphandler-trying-to-use-a-non-valid-resource-stream/93416

Context

trying open my inbound emails in the emails menu

Version

SuiteCRM 8.4.2 and also tested on SuiteCRM 7.14.4 (same issue)

What browser are you currently using?

Chrome

Browser Version

Chrome Versión 126.0.6478.128 (Build oficial) (64 bits)

Environment Information

10.6.18-MariaDB (also tested on MySQL 8.0) (same issue)

Operating System and Version

Ubuntu 22.04.4 LTS

SuiteBot commented 1 month ago

This issue has been mentioned on SuiteCRM. There might be relevant details there:

https://community.suitecrm.com/t/error-in-email-an-imap-error-detected-imap-close-error-imaphandler-trying-to-use-a-non-valid-resource-stream/93416/7

serhiisamko091184 commented 1 month ago

Hello @Ignacio78-86,

thank you for contributing to the SuiteCRM project,

could you, please specify the PHP version you are currently using? Confirm, please, that PHP IMAP module has been installed.

Thanks in advance!

Regards, Serhii

Ignacio78-86 commented 1 month ago

yes of course, sorry, I have tested with 2 versions of PHP (in both imap PHP installed) and the error is the same:

case 1 (photo: Case 1 PHP8.1 and Imap enabled) Case 1 PHP8 1 and Imap enabled

CASE 1: PHP 8.1.2-1ubuntu2.18 (cli) (built: Jun 14 2024 15:52:55) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2-1ubuntu2.18, Copyright (c), by Zend Technologies ecrmv1@ecrmv1:~$ php -m Cannot load Zend OPcache - it was already loaded [PHP Modules] bcmath calendar Core ctype curl date dom exif FFI fileinfo filter ftp gd gettext hash iconv imap intl json ldap libxml mbstring mysqli mysqlnd openssl pcntl pcre PDO pdo_mysql pdo_pgsql pdo_sqlite pgsql Phar posix readline Reflection session shmop SimpleXML soap sockets sodium SPL sqlite3 standard sysvmsg sysvsem sysvshm tokenizer xml xmlreader xmlwriter xsl Zend OPcache zip zlib

[Zend Modules] Zend OPcache

case 2 (photo: Case 2 PHP8.2 and Imap enabled) Case 2 PHP8 2 and Imap enabled

CASE 2: PHP 8.2.21 (cli) (built: Jul 23 2024 08:42:36) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.21, Copyright (c) Zend Technologies with Zend OPcache v8.2.21, Copyright (c), by Zend Technologies root@a8654a516ec7:/# php -m Cannot load Zend OPcache - it was already loaded [PHP Modules] bcmath calendar Core ctype curl date dom exif FFI fileinfo filter ftp gd hash iconv imap intl json ldap libxml mbstring mysqli mysqlnd openssl pcre PDO pdo_mysql pdo_sqlite pgsql Phar posix random readline Reflection session shmop SimpleXML soap sockets sodium SPL sqlite3 standard sysvmsg sysvsem sysvshm tokenizer xml xmlreader xmlwriter Zend OPcache zip zlib

[Zend Modules] Zend OPcache