mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.17k stars 383 forks source link

IMap search getting stuck #761

Open Menter32 opened 5 years ago

Menter32 commented 5 years ago

I want to check for new emails and parse them every X seconds in Gmail.

To keep it connected all the time I have this:

var imap = new IMap(Config.Email);
imap.connect();

imap.on("ready", () => 
{
    imap.openBox("INBOX", false, (Error, mailBox) =>
    {
        if (Error) 
        {
            console.error(`IMap opening inbox: ${Error}`);
            return imap.end();
        }
    });
});

imap.on("error", (Error) =>
{
    console.error(`IMap connection: ${Error}`);
    imap.connect();
});

imap.on("end", () => 
{
    console.error(`IMap connection ended`);
    imap.connect();
});

And for searching for email I use "forever" function from async library. It executes this function:

function Search(IMap, Callback)
{
    try
    {
        if(IMap.state != 'authenticated')
        {
            console.error(`Not authenticated`);
            return Callback();
        }

        console.log(`Pre-search`);

        IMap.search(["UNSEEN"], function(Error, Results) 
        {
            console.log(`Search`);
            if(Error)
            {
                console.error(`IMap searching: ${Error}`);
                return Callback();
            }

            if(!Results || !Results.length)
            {
                console.log(`No emails`);
                return Callback();
            }
            console.log(`Fetching emails`);
            ...

It works fine for a couple of hours but when connection randomly ends it seems to be stuck on IMap.search when it should probably be called with Error? IMap reconnects but as checking email function stopped on IMap.search it never called Callback

28-03 20:58:30.561: Pre-search
28-03 20:58:31.093: IMap connection ended

Would appreciate if I could be told how should it be done correctly. Thanks.

ghost commented 5 years ago

Is there any update? I have the same issue.

BiaoGe2399 commented 4 years ago

I have this problem too,have you solved it now?