chadxz / imap-simple

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

closeBox to delete email #21

Closed shenron closed 7 years ago

shenron commented 7 years ago

Hello,

I try to delete emails so I tag them, but emails continue to be stored on the IMAP Server. Maybe I'm wrong, but I think to delete emails (tagged with \Deleted flag) we have to use closeBox.

Thank you for your help.

chadxz commented 7 years ago

Hi Thomas,

This library does not have a wrapper around closeBox from the node-imap library yet, so any behavior you are experiencing with that method would not have anything to do with the implementation here.

If you want this library to support closeBox directly, we're accepting pull requests 😊

elvisiope commented 5 years ago

Hello, I'm having the same problem deleting or adding flag!

I created a simple code to remove the infomations of emails after 1 hour. But it is giving error and not the same.

my code:

`var data = this._data; const interval = setInterval(() => { if(data.end){ clearInterval(interval); process.send(data); } }, 10 * 1000);

    return await imaps.connect(this._config).then((connection) => {        
        connection.openBox('INBOX').then((box) => {
            var searchCriteria = ['UNSEEN'];
            var fetchOptions = { 
                bodies: ['TEXT'],
                struct: true 
            };
            return connection.search(searchCriteria, fetchOptions);

        }).then((messages) => {
            let taskList = messages.map((message) => {
                return new Promise((res, rej) => {
                    var parts = imaps.getParts(message.attributes.struct); 
                    parts.map((part) => {
                        return connection.getPartData(message, part)
                        .then((partData) => {
                            try{
                                const then = moment().add(-1, 'hours');
                                const now = moment(message.attributes.date);

                                if(now <= then){
                                    connection.delFlags(message.attributes.uid, '\Flagged', (err)=>{
                                        if(err)
                                            logger.info(err);
                                    });
                                    connection.addFlags(message.attributes.uid, ['\Seen','\Deleted'], (err)=>{
                                        if(err){
                                            logger.warn("Erro ao deletar o e-mail %s msg: %s", message.attributes.uid, err);
                                            rej(err);
                                        }
                                        res();
                                    });
                                }else{
                                    res();
                                }   
                            }catch(err){
                                logger.warn(err);
                            }
                        });
                    });
                });    
            });

            return Promise.all(taskList).then(() => {
                connection.closeBox(true, (err)=>{
                    if(err){
                        logger.warn({
                            msg:"Não conseguiu deletar as mensagens!",
                            erro:err
                        });
                    }
                });
                connection.end();
                data.end = true;
            });
        }).catch((err)=>{
            logger.warn(err);
            data.end = true;
            connection.end();
        });
    }).catch((err)=>{
        logger.warn(err);
        data.end = true;
    });`

I followed the example of README.md. The same presents me with the following error!

connection.closeBox is not a function TypeError: connection.closeBox is not a function at Promise.all.then (/Users/elvis/Desktop/mail-monitor/lib/services/imap-mailer.service.js:158:17) at process._tickCallback (internal/process/next_tick.js:68:7)

From what I saw the function does not really exist, can anyone help me solve this problem?

IgorSasovets commented 5 years ago

@elvisiope , I have the same issue

IgorSasovets commented 5 years ago

@elvisiope , please try to replace connection.closeBox with connection.imap.closeBox and notify me about the results

IgorSasovets commented 5 years ago

@chadxz , I think that README should be updated. I can open a PR for this. What do you think about that?

chadxz commented 5 years ago

Yes please do

IgorSasovets commented 5 years ago

@chadxz , done

chadxz commented 5 years ago

Thank you

elvisiope commented 5 years ago

@IgorSasovets, I made the change, Is no longer giving error, but neither is it ruling out.

If I mark the "closeBox" as false will it just move the message to the trash? If so, that would be a good solution for me.

IgorSasovets commented 5 years ago

Hi, @elvisiope ! Sorry for the late response. Below listed code that I use to delete mails by search criteria:

connection.addFlags(message.attributes.uid, 'Deleted', (err) => {
...
.spread(() => {
            return connection.imap.closeBox(true, (err) => {
                if (err)
                    throw err;
            });
        })
...

and it works for me.