chirag04 / mail-listener2

Mail listener library for node.js. Get notification when new email arrived.
MIT License
246 stars 116 forks source link

how to get buid or seqno ? #13

Closed waqas1991 closed 10 years ago

waqas1991 commented 10 years ago

How can I get a message buid or seqno ??

remitod commented 10 years ago

I think I found how to do that by using the imap object:

mailListener.imap.openBox('INBOX', true, function(err, box) { if (err) { throw err; }

        var uid = '',
            f = mailListener.imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM)','TEXT'] });

        f.on('message', function(msg, seqno) {
            msg.once('attributes', function(attrs) {
                console.log(attrs['x-gm-msgid']);

                uid = attrs['x-gm-msgid'];
            });
        });
        f.once('error', function(err) {
            console.log('Fetch error: ' + err);
        });
        f.once('end', function() {
            console.log('Done fetching all messages!');
                            mailListener.imap.end();
        });
    });

It is not very clean but I get the uid (the Gmail uid). My problem is that when I try to use it after to set a flag in example, I've got an error...

mailListener.imap.addFlags(uid, 'Deleted', function(err){ console.log(err); });

{ [Error: Could not parse command] textCode: undefined, source: 'protocol' }

chirag04 commented 10 years ago

the mail event will now have seq and attributes. Refer the updated readme.

eg:

mailListener.on("mail", function(mail, seqno, attributes){
  // do something with mail object including attachments
  console.log("emailParsed", mail);
  // mail processing code goes here
});