Can't mark message as seen.
I'm trying to mark message as seen via fetch method 'markSeen:true' attribut or via setFlags/addFlags but when I fetch them for the second time they are still NEW.
If I use markSeen:true and display body of the message I can see the flag is there, but it's not persistent.
My message box is not read only and I can delete message by adding appropriate flag and closing the mailbox. Also flag '\Seen' is on box permFlags list. I am using version 0.8.14.
thanks mC
Did you open the mailbox as read-only (if you pass true as the second argument to openBox(), this is the case)?
As I mentioned above I'm opening inbox with second parameter set to false (to be honest I tried both and neither worked for me...).
mC
When you fetch the message, what are you using to determine if the message is "new?" Whether the \Seen flag is set? The \Recent flag? Something else?
I search for New messages and then in fetch check flags. Every mail has '\Recent' flag. If I add markSeen:true in fetch message will have both flags '\Recent' and '\Seen' but on next run of my app same messages will be fetched. What's more if I remove markSeen attr from fetch same message will have only '\Recent' flag this time. Can I change '\Recent' flag somehow?
mC
You can't change \Recent. That's just an indicator that your session was the one notified of that new message. If you open up a new connection and fetch the same message, it should not have the \Recent flag.
My messages always have recent flag. I don't see also console log that connection ended although I call end() as in examples - can it be the reason?
mC
Can you show the exact code you're using?
My code (well, most of it is from example):
var imap = new Imap({
user: configRow.login,
password: configRow.pwd,
host: configRow.imap_host,
port: configRow.imap_port,
tls: (configRow.imap_port === 993),
debug: function(info) { console.log(info); }
});
function openInbox(cb) {
imap.openBox('INBOX', false, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
console.log(box.readOnlyb);
if (err) console.log(err);
imap.search(['NEW'], function(err, results) {
if (err) throw err;
var f = imap.fetch(results, {markSeen: true, bodies: ''});
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
//imap.setFlags(seqno, '\\Seen', function(err) { console.log('flagerr ' + err); } );
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
console.log(prefix + 'Body');
var str = '';
stream.on('data', function(chunk) { str = str + chunk; });
stream.on('end', function() { console.log(str); });
});
msg.once('attributes', function(attrs) {
console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
});
msg.once('end', function() {
console.log(prefix + 'Finished');
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
imap.closeBox(function() { imap.end(); console.log('end'); });
//imap.end();
});
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
Sorry about my poor English. I have the same problem too.
My debug output: 'A8 UID STORE 85,86,87,89,90 FLAGS.SILENT (\Seen)' 'A8 OK UID STORE completed'
There is my code:
function ReceiveEmail(username,pwd,host,port,savepath){ events.EventEmitter.call(this); this.username = username; this.pwd = pwd; this.host = host; this.port = port; this.savepath = savepath; }
util.inherits(ReceiveEmail,events.EventEmitter);
ReceiveEmail.prototype.getemail = function(){ var imap = new Imap({ user: this.username, password: this.pwd, host: this.host, port: this.port, tls: true, tlsOptions: {secureProtocol: 'SSLv3_method'}, authTimeout:10000, debug:function(info){ console.log(info); } }); var self = this;
function openInbox(cb) {
imap.openBox('INBOX', false, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
console.log("email:%s!!box.permFlags:",self.username,inspect(box.permFlags));
if (err){
throw 'Emailbox:'+this.username+'|err:'+err;
self.emit('finished');
}
imap.search([ 'UNSEEN'], function(err, results) {
if (err){
throw err;
self.emit('finished');
}
if(results[0]>0){
var f = imap.fetch(results, {
bodies: ['','HEADER.FIELDS (FROM TO SUBJECT CC DATE MESSAGE-ID)'],
struct:true,
markSeen:true //标记为已看
});
f.on('message', function(msg, seqno) {
var prefix = '(#' + seqno + ') ',
from = '',
to = '',
subject = '',
cc = '',
date = '',
mid = '',
filepath = self.savepath+self.username+new String(new Date().getTime().toString()).concat((Math.random()*1000).toString())+'.email';
msg.on('body', function(stream, info) {
if(info.which == 'HEADER.FIELDS (FROM TO SUBJECT CC DATE MESSAGE-ID)'){
var buffer = '';
stream.on('data',function(chunk){
buffer += chunk;
})
stream.once('end',function(){
var headers = Imap.parseHeader(buffer);
from = headers['from'];
to = headers['to'];
subject = headers['subject'];
cc = headers['cc'];
date = (new Date(headers['date']).getTime())/1000;
mid = headers['message-id'];
})
}else{
stream.pipe(fs.createWriteStream(filepath));
}
});
msg.once('end', function() {
//写入数据库
var connection = mysql.createConnection({
host : db_info['db_host'],
user : db_info['db_username'],
password: db_info['db_pwd'],
charset : db_info['db_charset']
});
connection.connect(function(err){
if(err){
console.error('error connecting:',err);
}
});
connection.query('USE '+db_info['db_name'],function(err){
if(err){
throw err;
connection.end();
}
if(!cc) cc='';
if(!mid) mid='';
var db_data={
"from":from,
"to":self.username,
"send_date":date,
"body_path":filepath,
"cc":cc,
"email_id":mid
}
connection.query(
'INSERT INTO `original_email` SET ?',
db_data,
function(err,result){
if(err) throw err;
console.log(prefix + 'Finished');
connection.end();
}
);
});
//console.log(prefix + 'SQL:INSERT INTO `original_email`(from,to,send_date,body_path,cc,email_id)VALUES(%s,%s,%s,%s,%s,%s)',from,username,date,filepath,cc,mid);
});
});
f.once('error', function(err) {
self.emit('finished');
console.log('Emailbox:'+self.username+'Fetch error: ' + err);
});
f.once('end', function() {
self.emit('finished');
console.log('Emailbox:'+self.username+'-Done fetching all messages!');
imap.end();
});
imap.setFlags(results,['\\Seen'],function(err){
if(err){
self.emit('finished');
throw err;
}
});
}else{
self.emit('finished');
imap.end();
}
});
});
});
imap.once('error', function(err) {
self.emit('finished');
console.log('Emailbox:'+self.username+'|err:'+err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
}
module.exports = ReceiveEmail;
I am also facing same problem. Mails are not being marked as READ It works fine when there is some 300+ mail unread, but it fails when unread count is 500+
Same here.
+1
+1
+1
+1
+1
+1...
You should call addFlags with the UID and not the Seqno. If you want mark them as 'seen' by seqno call imap.seq.fetch([seqno], function() { markSeen: true }
Here's how node-imap does it https://github.com/mscdex/node-imap/blob/master/lib/Connection.js#L835 during .fetch(). And Gmail/whatever doesn't seem to support the .PEEK flag for fetching emails. So that makeSeen option is useless.
Solution: https://github.com/xc-stars/xcmail/blob/cb797ebe877fb3c7309b7bc4d248f7ecaec2f13b/src/main-process/mailReceiver.js#L118
You basically will need to be manually setting that \SEEN flag. I do it like this:
msg.once('attributes', function(attrs) {
markMailSeen(attrs.uid)
});
Make a note that Seqno is not UID (both are sequiential digits, yet you need the UID because that's the unique one).
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.seq.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
}
I don't know why my code is not getting properly formatted here, so basically results contains the uuid
we need to call imap.setFlags([uuids], ['\\Seen'], cb) function to make it mark as read
handle all errors properly, that i haven't done here :