how to mark unseen email as seen with node-imap in

2019-05-18 20:46发布

问题:

i want to recieve email and mark email unseen as seen with node-imap. the recieving i have done, but i don't know how to mark email unseen as seen. the API offers a function seems like replace the code var f = imap.fetch(results, { bodies: '' }); with var f = imap.fetch(results, { markSeen : true });in the example,but it seems doesn't work. what should i do?

回答1:

oh, i'va got solution. it's my fault to open mailbox in read-only mode, that's why i couldn't modify the mail's status.imap.openBox('INBOX', false, cb); the second args false means open mailbox not in read-only mode.



回答2:

I tried setting mail status to false imap.openBox('INBOX', false, cb) but it didn't work at first.

But then I changed the data in body and set it to HEADER.FIELDS (FROM TO SUBJECT DATE) it worked. I don't know how is this related but it's working now while the mail box read-only status is false obviously.

Non working code:

imap.fetch(results, { bodies: '', markSeen: true });

Working code:

imap.fetch(results, { bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)', markSeen: true });


回答3:

imap.openBox("INBOX", false, function(err, mailBox) {
    if (err) {
        console.error(err);
        return;
    }
    imap.search(["UNSEEN"], function(err, results) {

        imap.setFlags(results, ['\\Seen'], function(err) {
            if (!err) {
                console.log("marked as read");
            } else {
                console.log(JSON.stringify(err, null, 2));
            }
        });

        var f = imap.fetch(results, { bodies: "" });
        f.on("message", processMessage);
        f.once("error", function(err) {
            return Promise.reject(err);
        });
        f.once("end", function() {
            console.log("Done fetching all unseen messages.");
            imap.end();
        });
    });
});

processMessage

function processMessage(msg seqno){
     /*use mailparser*/}

so basically results contains the uuid

need to call imap.setFlags([uuids], ['\Seen'], cb) function explicitly to make it mark as read



回答4:

Here is a working example for the same.

var Imap = require('imap'),
    inspect = require('util').inspect;

var imap = new Imap({
    user: 'USERNAME',
    password: 'PASSWORD',
    host: 'IMAP_HOST',
    port: 993, // Default port is 993
    tls: true,
    tlsOptions: {rejectUnauthorized: false}
});

function openInbox(cb) {
    // openReadOnly = false
    imap.openBox('Inbox', false, cb);
}

imap.once('ready', function () {
    openInbox(function (err, box) {
        if (err) throw err;

        // Search emails having "Some Subject" in their Subject headers
        imap.search([['HEADER', 'SUBJECT', 'Some Subject']], function (err, results) {
            if (err) throw err;

            try {
                var f = imap.fetch(results, {bodies: 'TEXT'});
                f.on('message', function (msg, seqno) {
                    msg.on('body', function (stream, info) {
                        var buffer = '';
                        stream.on('data', function (chunk) {
                            buffer += chunk.toString('utf8');
                        });
                        stream.once('end', function () {

                            // Mark the above mails as read
                            msg.once('attributes', function (attrs) {

                                let uid = attrs.uid;
                                imap.addFlags(uid, ['\\Seen'], function (err) {
                                    if (err) {
                                        console.log(err);
                                    } else {
                                        console.log("Marked as read!")
                                    }
                                });

                            });

                        });
                    });
                });
                f.once('end', function () {
                    imap.end();
                });
            } catch (errorWhileFetching) {
                console.log(errorWhileFetching.message);
                imap.end();
            }

        });
    });
});

imap.connect();


标签: node.js imap