MateMalice / mail-listener2

Mail listener library for node.js. Get notification when new email arrived.
Other
9 stars 7 forks source link

Attributes for on mail event occasionally undefined #38

Closed MorphedAU closed 1 year ago

MorphedAU commented 1 year ago

Hi team,

Have an issue where the attributes variable passed along with an on mail event seem to be intermittently undefined. This is causing issues when trying to move emails after processing them.

Sample code here, drastically slimmed down

mailListener.on("mail", function (mail, seqno, attributes) {
    if (mail.from.value[0].address == "email@example") {
        parseEmail();
        try {
            mailListener.imap.move(attributes.uid, "Processed", function (err) {
                if (err) {
                    emitToTerminal('Error moving message - '+ mail.subject);
                    return;
                }
            }
        } catch (e) {
            emitToTerminal('Error moving message '+ mail.subject + ' ++ERR++ '+ e);
        }
    }
}

Which results in hitting the catch block and presenting: Error moving message subject goes here ++ERR++ TypeError: Cannot read property 'uid' of undefined

I can't see any pattern as to what is causing this: 5 failures, 1 success, 1 failure, 1 success, 2 failures, 1 success, 1 failure, 2 success

We are sadly running Node v14.15.4, trying to get permission to upgrade to 18 but this may take a while. Everything else seems to be working fine on this Node version, apart from the attributes consistently being passed along.

Hoping to get some ideas on anything that can be done to resolve this or if updating to Node 18 is ultimately the fix.

Cheers in advance!

MateMalice commented 1 year ago

Hi @MorphedAU,

Are you encountering this issue on NodeJS v18 as well, or is the issue limited to NodeJS v14? Because this is an intermittent issue, diagnosing the root cause might require a lot of effort, which I'm reluctant to put into a Node version that is soon reaching end-of-life.

It may help if you provide samples (with any credentials, PII or confidential information redacted of course) of an email that is being parsed successfully and an email that is not working - I'm particularly interested in the headers and the attributes.

If upgrading to v18 fixes this particular problem, then that is probably the simplest fix. However, if it doesn't fix the problem, I'd like to identify the root cause.

MorphedAU commented 1 year ago

Haven't been able to upgrade to 18 yet. Still trying to push for that, hopefully will be able to go to NodeJS 18 next week, understandable about troubleshooting on an end-of-life version. Will also be changing the email server that the script hits next week, which I guess is possibly another cause. Was hoping someone else had experienced it and knew of a fix.

Will close this issue for now until I can get NodeJS up to 18 and determine if it is still an issue.

stratbasher commented 1 year ago

@MorphedAU did you ever figure this out? I am having this issue as well:

https://github.com/MateMalice/mail-listener2/issues/41