node-imap icon indicating copy to clipboard operation
node-imap copied to clipboard

How do I add a SEEN flag to the message in gmail ?

Open qorrect opened this issue 14 years ago • 16 comments

I've tried

imap.addFlags(msg.uid, '\SEEN'); imap.addFlags(msg.uid, 'SEEN'); imap.addFlags(msg.uid, '(SEEN)');

All get the same response :

Error: The flag "\SEEN" is not allowed by the server for this mailbox at ImapConnection._store (/home/q/workspace/formhog/node_modules/imap/lib/imap.js:1164:15) at ImapConnection.addFlags (/home/q/workspace/formhog/node_modules/imap/lib/imap.js:958:8) at ImapMessage. (/home/q/workspace/formhog/server/scratch.js:90:22) at ImapMessage.EventEmitter.emit (events.js:85:17) at CleartextStream.ondata (/home/q/workspace/formhog/node_modules/imap/lib/imap.js:284:19) at CleartextStream.EventEmitter.emit (events.js:88:17) at CleartextStream.CryptoStream._push (tls.js:491:12) at SecurePair.cycle (tls.js:849:20) at EncryptedStream.CryptoStream.write (tls.js:230:13) at Socket.ondata (stream.js:38:26)

qorrect avatar Nov 01 '12 20:11 qorrect

The correct usage is imap.addFlags(msg.uid, 'SEEN');. However, I'll add a check to look for a prepended backslash.

Did you open the mailbox in read-only mode? If not and you're getting that error with 'SEEN', then can you provide the value displayed by console.dir(imap._state.box.permFlags); ?

mscdex avatar Nov 01 '12 20:11 mscdex

Nevermind, markSeen flag is exactly what I needed, thanks!

qorrect avatar Nov 01 '12 20:11 qorrect

Can I also use the message-id to update an Email? e.g.: [email protected]

is msg.uid == seqno?

Regards, Christian

heygambo avatar Nov 14 '12 15:11 heygambo

@Gambo

  1. Not without doing a search on that header first to find its uid/seqno.
  2. No, a uid is an identifier for a message that is unique within its mailbox as long as the mailbox's uidvalidity value does not change. Once that value changes, uids for existing messages have changed.

seqno is a sequence number that indicates the relative position of a message in the mailbox. So when a message is expunged, all messages after it have their sequence numbers decremented by 1. When a message is added to the mailbox, its sequence number is the total number of messages - 1.

Those are very basic descriptions. If you want more technical, in-depth definitions, your best bet is to take a peek at RFC 3501 starting here.

mscdex avatar Nov 14 '12 18:11 mscdex

A bit of a follow-up for the new version (0.8): Using the methodology described in your answer (imap.addFlags(msg.uid, 'SEEN');), I was not able to get the flags to update on the server side (Gmail).

The box is not in read-only mode, as if it were I would get an error - and in my case, nothing at all happens.

albertxing avatar Jul 05 '13 03:07 albertxing

Strict flag check removed in 1c5f756.

mscdex avatar Jul 05 '13 03:07 mscdex

Okay, works fine now - just a note: it seems to run the callback function more than once (anywhere from once [expected] to six times) in succession.

albertxing avatar Jul 05 '13 04:07 albertxing

@albertxing Can you provide a debug log of a session where it happens more than once (set debug: console.log in constructor object)?

EDIT: Can you also provide the code you're using that's exhibiting this behavior?

mscdex avatar Jul 05 '13 05:07 mscdex

It doesn't let me write debug: console.log - making sure, this is through new Imap({ debug: console.log})?

function getMessages(filter, cl) {
    imap.search([filter], function (err, results) {

        if (!results[0]) {
            console.log('Nothing to fetch');
            return;
        }

        if (err) {
            console.log(err);
        }

        var f = imap.fetch(results, {
            bodies: ''
        });

        f.on('message', function (msg, seqno) {

            var uid,
                headers,
                body = '';

            msg.on('body', function (stream, info) {
                var mailparser = new MailParser();

                stream.pipe(mailparser);

                mailparser.on('end', function (_message) {
                    headers = {
                        to: _message.to,
                        from: _message.from,
                        subject: _message.subject,
                        date: _message.headers.date
                    };

                    body = _message.html;

                    if (body === undefined) {
                        body = "";
                    }

                    mail[uid] = {
                        to: headers.to,
                        from: headers.from,
                        subject: headers.subject,
                        date: headers.date,
                        message: body
                    };

                    var names = '';

                    headers.from.forEach(function (i, v) {
                        names += i.name;
                        names += ' ';
                    });

                    $("table").append('<tr class="' + cl + '" id="' + uid + '"><td>' + names + '</td><td>' + headers.subject + '</td><td>' + headers.date + '</td></tr>').promise().done(function () {
                        $("tr:not(:first-child)").click(function () {
                            var uid = $(this).attr('id');
                            $("p").html(JSON.parse(localStorage.mail)[uid].message);
                            if ($(this).hasClass('unread')) {
                                imap.addFlags(uid, 'seen', function (err) {
                                    console.log('add flag to ' + uid);
                                });

                                $(this).removeClass('unread').addClass('read');
                            }
                        });
                    });

                    localStorage.mail = JSON.stringify(mail);
                });
            });

            msg.once('attributes', function (attrs) {
                uid = attrs.uid;
                console.log(attrs);
            });

            msg.once('end', function () {
                console.log('Finished');                                               
            });
        });

        f.once('error', function (err) {
            console.log('Fetch error: ' + err);
        });

        f.once('end', function () {
            console.log('Done fetching all messages!');
        });
    });
}

albertxing avatar Jul 05 '13 05:07 albertxing

Nevermind I got the logging to work through a parent function that wraps console.log. Here are the results:

[connection] Connected to host app.js:17
<= '* OK Gimap ready for requests from ******** tz1if7444974pac.113' app.js:17
=> 'A0 CAPABILITY' app.js:17
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2' app.js:17
<= 'A0 OK Thats all she wrote! tz1if7444974pac.113' app.js:17
=> 'A1 LOGIN "*******@gmail.com" "*********[hidden]"' app.js:17
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH' app.js:17
<= 'A1 OK ******@gmail.com ********* authenticated (Success)' app.js:17
=> 'A2 NAMESPACE' app.js:17
<= '* NAMESPACE (("" "/")) NIL NIL' app.js:17
<= 'A2 OK Success' app.js:17
=> 'A3 LIST "" ""' app.js:17
<= '* LIST (\\Noselect) "/" "/"' app.js:17
<= 'A3 OK Success' app.js:17
=> 'A4 SELECT "INBOX"' app.js:17
<= '* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)' app.js:17
<= '* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen \\*)] Flags permitted.' app.js:17
<= '* OK [UIDVALIDITY 1] UIDs valid.' app.js:17
<= '* 6 EXISTS' app.js:17
<= '* 0 RECENT' app.js:17
<= '* OK [UIDNEXT 23] Predicted next UID.' app.js:17
<= '* OK [HIGHESTMODSEQ 5834]' app.js:17
<= 'A4 OK [READ-WRITE] INBOX selected. (Success)' app.js:17
=> 'A5 UID SEARCH NOT SEEN' app.js:17
<= '* SEARCH 18' app.js:17
<= 'A5 OK SEARCH completed (Success)' app.js:17
=> 'A6 UID SEARCH SEEN' app.js:17
<= '* SEARCH 15 16 17 21 22' app.js:17
<= 'A6 OK SEARCH completed (Success)' app.js:17
=> 'A7 UID FETCH 18 (X-GM-THRID X-GM-MSGID X-GM-LABELS UID FLAGS INTERNALDATE BODY.PEEK[])' app.js:17
<= '* 4 FETCH (X-GM-THRID 1439100081216835727 X-GM-MSGID 1439100081216835727 X-GM-LABELS () UID 18 INTERNALDATE "28-Jun-2013 15:19:46 +0000" FLAGS () BODY[] {34823}' app.js:17
<= '* 4 FETCH (X-GM-THRID 1439100081216835727 X-GM-MSGID 1439100081216835727 X-GM-LABELS () UID 18 INTERNALDATE "28-Jun-2013 15:19:46 +0000" FLAGS () )' app.js:17
<= 'A7 OK Success' app.js:17
=> 'A8 UID FETCH 15,16,17,21,22 (X-GM-THRID X-GM-MSGID X-GM-LABELS UID FLAGS INTERNALDATE BODY.PEEK[])' app.js:17
<= '* 1 FETCH (X-GM-THRID 1437755649423430341 X-GM-MSGID 1437755649423430341 X-GM-LABELS ("\\\\Sent") UID 15 INTERNALDATE "13-Jun-2013 19:10:36 +0000" FLAGS (\\Seen) BODY[] {669}' app.js:17
<= '* 1 FETCH (X-GM-THRID 1437755649423430341 X-GM-MSGID 1437755649423430341 X-GM-LABELS ("\\\\Sent") UID 15 INTERNALDATE "13-Jun-2013 19:10:36 +0000" FLAGS (\\Seen) )' app.js:17
<= '* 2 FETCH (X-GM-THRID 1437761272347738581 X-GM-MSGID 1437761530589805442 X-GM-LABELS ("\\\\Sent") UID 16 INTERNALDATE "13-Jun-2013 20:44:04 +0000" FLAGS (\\Seen) BODY[] {981}' app.js:17
<= '* 2 FETCH (X-GM-THRID 1437761272347738581 X-GM-MSGID 1437761530589805442 X-GM-LABELS ("\\\\Sent") UID 16 INTERNALDATE "13-Jun-2013 20:44:04 +0000" FLAGS (\\Seen) )' app.js:17
<= '* 3 FETCH (X-GM-THRID 1437761272347738581 X-GM-MSGID 1437761272347738581 X-GM-LABELS ("\\\\Sent") UID 17 INTERNALDATE "13-Jun-2013 20:39:58 +0000" FLAGS (\\Seen) BODY[] {666}' app.js:17
<= '* 3 FETCH (X-GM-THRID 1437761272347738581 X-GM-MSGID 1437761272347738581 X-GM-LABELS ("\\\\Sent") UID 17 INTERNALDATE "13-Jun-2013 20:39:58 +0000" FLAGS (\\Seen) )' app.js:17
<= '* 5 FETCH (X-GM-THRID 1437755649423430341 X-GM-MSGID 1437755854479631640 X-GM-LABELS ("\\\\Sent") UID 21 INTERNALDATE "13-Jun-2013 19:13:51 +0000" FLAGS (\\Seen) BODY[] {1552}' app.js:17
<= '* 5 FETCH (X-GM-THRID 1437755649423430341 X-GM-MSGID 1437755854479631640 X-GM-LABELS ("\\\\Sent") UID 21 INTERNALDATE "13-Jun-2013 19:13:51 +0000" FLAGS (\\Seen) )' app.js:17
<= '* 6 FETCH (X-GM-THRID 1439327942687972907 X-GM-MSGID 1439327942687972907 X-GM-LABELS ("\\\\Important") UID 22 INTERNALDATE "01-Jul-2013 03:41:31 +0000" FLAGS (\\Seen) BODY[] {1783}' app.js:17
<= '* 6 FETCH (X-GM-THRID 1439327942687972907 X-GM-MSGID 1439327942687972907 X-GM-LABELS ("\\\\Important") UID 22 INTERNALDATE "01-Jul-2013 03:41:31 +0000" FLAGS (\\Seen) )' app.js:17
<= 'A8 OK Success' app.js:17
=> 'IDLE IDLE' app.js:17
<= '+ idling' app.js:17

/** The following is after I add the flag **/

=> DONE app.js:17
<= 'IDLE OK IDLE terminated (Success)' app.js:17
=> 'A9 UID STORE 18 +FLAGS.SILENT (\\seen)' app.js:17
<= 'A9 OK Success' app.js:17
=> 'IDLE IDLE' app.js:17
<= '+ idling' app.js:17

albertxing avatar Jul 05 '13 05:07 albertxing

@albertxing Ok, first of all if you're already opening the mailbox in read-write mode (second argument to openBox() is set to false -- the default), then messages are already marked as Seen once you fetch() them.

Secondly, I don't see the output from your addFlags() callback in the output you've shown.

mscdex avatar Jul 05 '13 05:07 mscdex

Nevermind, I've figured out the problem - it only happens when you add another SEEN flag to one that already has it (so as a duplicate flag).

In case you still want the log:

=> DONE app.js:17
<= 'IDLE OK IDLE terminated (Success)' app.js:17
=> 'A13 UID STORE 15 +FLAGS.SILENT (\\seen)' app.js:17
<= '* BAD [CLIENTBUG] Invalid tag' app.js:17
<= '* BAD [CLIENTBUG] Invalid tag' app.js:17
<= '* BAD [CLIENTBUG] Invalid tag' app.js:17
<= '* BAD [CLIENTBUG] Invalid tag' app.js:17
<= '* BAD [CLIENTBUG] Invalid tag' app.js:17
<= 'A13 OK Success' app.js:17
add flag to 15 app.js:86
=> 'A14 UID STORE 15 +FLAGS.SILENT (\\seen)' app.js:17
<= 'A14 OK Success' app.js:17
add flag to 15 app.js:86
=> 'A15 UID STORE 15 +FLAGS.SILENT (\\seen)' app.js:17
<= 'A15 OK Success' app.js:17
add flag to 15 app.js:86
=> 'A16 UID STORE 15 +FLAGS.SILENT (\\seen)' app.js:17
<= 'A16 OK Success' app.js:17
add flag to 15 app.js:86
=> 'A17 UID STORE 15 +FLAGS.SILENT (\\seen)' app.js:17
<= 'A17 OK Success' app.js:17
add flag to 15 app.js:86
=> 'A18 UID STORE 15 +FLAGS.SILENT (\\seen)' app.js:17
<= 'A18 OK Success' app.js:17
add flag to 15 app.js:86
=> 'IDLE IDLE' app.js:17
<= '+ idling' app.js:17

As for marking as SEEN on fetch(), it doesn't exhibit that behavior for me - and that's a good thing as I don't want to mark as read on fetch. Should I also include markSeen: false just to be sure?

albertxing avatar Jul 05 '13 17:07 albertxing

@albertxing Judging by that log output, everything looks normal aside from the odd untagged 'Invalid tag' responses.

Regarding fetch() and Seen, I forgot to say it's only if markSeen is explicitly set to true, as it is false by default.

mscdex avatar Jul 05 '13 17:07 mscdex

@mscdex Sure, I was worried about marking as seen on fetch by default.

albertxing avatar Jul 05 '13 17:07 albertxing

how can I get buid or seqno if i using mail-listener 2 package ?

waqas1991 avatar Apr 22 '14 12:04 waqas1991

Read To UnRead in IMAP FLOW Code

client.messageFlagsAdd({uid: 116,seen: false}, ['\\Seen']);

client.messageFlagsRemove({uid: 116,seen: true}, ['\\Seen']);

nsubhadipta avatar Nov 19 '21 09:11 nsubhadipta