chadxz / imap-simple

Wrapper over node-imap, providing a simpler api for common use cases
MIT License
243 stars 80 forks source link

Passing argument to handler (onupdate/onmail) #92

Closed patryk-matis closed 3 years ago

patryk-matis commented 3 years ago

I'm trying to pass connection to onupdate/onmail handler, but I can't figure out how to do it.

       const test = (connection) =>{
            console.log(connection);
        }

        const makeConnection = async () => {
            await imaps.connect(config)
                .then(async(connection) => {
                    connection.on('error', onerror);
                    connection.on('close', onclose);
                    await connection.openBox('INBOX');
                    connection.on('update', test)
...

I want to use search function each time when new emails arrives. It works fine when I will use my function like:

                    await connection.openBox('INBOX');
                    connection.on('update', () =>{
                           console.log(connection);
                    })
Leetcore commented 3 years ago

How did you do it? I have the same problem. The docs say to put onmail to the config for connect, but it is only triggering once and not if i get a new mail.

Leetcore commented 3 years ago

Nevermind i found an example and my mistake. I changed the current mailbox and didn't get the events.

trasherdk commented 3 years ago

@Leetcore Maybe you could share a link to the example that helped with your solution?

Leetcore commented 3 years ago

I do this with:

// new mail event
connection.on('mail', num => {
    // new mail in inbox or sent folder
    updateMailView(mailConnection, conversations, contacts)
})

My problem was that i changed the current Inbox: connection.openBox('Sent'). The mail event will only work for the current Mailbox you have opened. I opened Inbox first and got the "new mail event" but afters switching to Sent folder i would not get new emails anymore. That's because the "new mail event" will only trigger for new mails in your currently opened box. I will try to change my code to use two connections for Inbox and Sent.