Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMAP module not moving mail to folder. #4313

Closed
leenooks opened this issue Oct 31, 2024 · 12 comments
Closed

IMAP module not moving mail to folder. #4313

leenooks opened this issue Oct 31, 2024 · 12 comments

Comments

@leenooks
Copy link

PHP version: 8.2.20
FreeScout version: 1.8.154
Database: PostgreSQL
Are you using CloudFlare: Yes
Are you using non-official modules: Yes

Hi, I'm using the IMAP Module to move mail to a folder after it has been received/imported into freescout.

The mails stay in the inbox, are marked unread, but are not moved to a folder.

I have tried "Trash", "Archive", "INBOX/Processed" - all folders exist, and are confirmed by the Get Folders on the IMAP folders input.

The log shows this:

[2024-10-31 15:10:04] production.ERROR: [Move or Remove IMAP Message Module] Mailbox: XXX; Message: XXX; Could not move the email to the following IMAP folder: Archive
[2024-10-31 16:45:03] production.ERROR: [Move or Remove IMAP Message Module] Mailbox: XXX; Message: XXX; Could not move the email to the following IMAP folder: INBOX/Processed
@freescout-help
Copy link
Collaborator

Can you send a screenshot of your folders structure (their nesting).

@leenooks
Copy link
Author

image

@leenooks
Copy link
Author

And on the server:
image

@freescout-help
Copy link
Collaborator

You'll need to debug this function: https://github.com/freescout-help-desk/freescout/blob/dist/overrides/webklex/php-imap/src/Message.php#L956

https://github.com/freescout-help-desk/freescout/wiki/Debugging

Try to replace the move() function with the following code and send us the result from Manage > Logs > App Logs:

    public function move(string $folder_path, bool $expunge = false) {
\Log::error('[Debugging] move() function executed.');
        $this->client->openFolder($folder_path);
\Log::error('[Debugging] openFolder() executed.');
        $status = $this->client->getConnection()->examineFolder($folder_path);
\Log::error('[Debugging] status: '.json_encode($status));
        if (isset($status["uidnext"])) {
            $next_uid = $status["uidnext"];
\Log::error('[Debugging] next_uid: '.$next_uid);
            if ((int)$next_uid <= 0) {
                return null;
            }
            
            /** @var Folder $folder */
            $folder = $this->client->getFolderByPath($folder_path);
\Log::error('[Debugging] folder path: '.($folder->path ?? ''));
            $this->client->openFolder($this->folder_path);
            if ($this->client->getConnection()->moveMessage($folder->path, $this->getSequenceId(), null, $this->sequence === IMAP::ST_UID) == true) {
\Log::error('[Debugging] moveMessage() executed.');
                return $this->fetchNewMail($folder, $next_uid, "moved", $expunge);
            }
        }

        return null;
    }

@leenooks
Copy link
Author

[2024-11-01 09:40:04] production.ERROR: [Debugging] move() function executed.
[2024-11-01 09:40:04] production.ERROR: [Debugging] openFolder() executed.
[2024-11-01 09:40:04] production.ERROR: [Debugging] status: {"flags":[["\\Seen","\\Answered","\\Flagged","\\Deleted","\\Draft","$MDNSent","$Forwarded","$Completed"]],"exists":126,"recent":0,"uidnext":305,"uidvalidity":2030456696}
[2024-11-01 09:40:04] production.ERROR: [Debugging] next_uid: 305
[2024-11-01 09:40:04] production.ERROR: [Debugging] folder path: Trash
[2024-11-01 09:40:04] production.ERROR: [Move or Remove IMAP Message Module] Mailbox: XXX; Message: XXX; Could not move the email to the following IMAP folder: Trash

(In this log, I had set it to move the mail to Trash (which is what I originally wanted) - where it is kept for 30 days before being deleted. Didnt want to use the delete function of your module, as I've already discovered some mails are not coming into freescout completely (a different issue), so need to have the mail kept on the server just in case.)

@freescout-help
Copy link
Collaborator

You'll need to debug further the moveMessage() function call: https://github.com/freescout-help-desk/freescout/blob/dist/overrides/webklex/php-imap/src/Message.php#L970

Or check your mail server logs - maybe you'll find some hint there.

@leenooks
Copy link
Author

leenooks commented Nov 1, 2024

The server logs shows this:

│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: << TAG15 LIST "" "*"
… 
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> TAG15 OK LIST completed [1 msec]
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: << TAG16 SELECT "INBOX"
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> * OK [CLOSED]
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent $Forwarded $Completed)
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> * 1 EXISTS
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> * 0 RECENT
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent $Forwarded $Completed \*)] Permanent flags 
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> * OK [UIDNEXT 2232] Predicted next UID
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> * OK [UIDVALIDITY 1101581658] UIDs valid
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> TAG16 OK [READ-WRITE] SELECT completed [0 msec]
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: << TAG17 UID MOVE 2231 "Trash"\r\n
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: >> TAG17 BAD Error: Unexpected item MOVE
│2024-11-01 06:35:03 +0000 16 axigen IMAP:00003488: << TAG18 LOGOUT

@freescout-help
Copy link
Collaborator

It looks like you are using the Axigen mail server. Try to contact their team and ask why "MOVE" command does not work on their server while working on all others.

@leenooks
Copy link
Author

leenooks commented Nov 7, 2024

So I did some tracing and research, and it seems like axigen uses the "Copy" then "Delete" approach instead of move:

https://stackoverflow.com/questions/122267/imap-how-to-move-a-message-from-one-folder-to-another

│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: << 28.5 UID COPY 934 "Filtered Email" 
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: >> 28.5 OK [COPYUID 1740330121 934 105] COPY completed
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: << 29.5 IDLE
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: >> + Expecting DONE
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: << DONE
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: >> 29.5 OK IDLE completed [0 msec]
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: << 30.5 UID STORE 934 +FLAGS.SILENT (\Deleted)
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: >> * 14 FETCH (MODSEQ (3594))
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: >> 30.5 OK STORE completed [0 msec]
│2024-11-01 06:38:16 +0000 16 axigen IMAP:00003490: >> * 4 EXISTS
│2024-11-01 06:38:16 +0000 16 axigen IMAP:00003490: >> * 1 RECENT
│2024-11-01 06:38:16 +0000 16 axigen IMAP:0000348F: << 31.5 UID EXPUNGE 934

Can you update your code to support the RFC4315 approach to moving messages?

@freescout-help
Copy link
Collaborator

freescout-help commented Nov 7, 2024

Axigen uses UID COPY, UID STORE and UID EXPUNGE.

FreeScout uses Webklex/PHP-IMAP library to communicate with mail servers via IMAP. So we've shared the suggestion with them: Webklex/php-imap#123 (comment)

@freescout-help
Copy link
Collaborator

Try with the attached /overrides/webklex/php-imap/src/Connection/Protocols/ImapProtocol.php file:

ImapProtocol.zip

@leenooks
Copy link
Author

leenooks commented Nov 8, 2024

Thanks, this works well. :)

Can I also suggest, that the "Move to IMAP folder" dialogue also has the "get imap folders" button, enabling you to select the folder from a dropdown list - this would avoid any typo errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants