gpt4 book ai didi

node.js - 使用 Node.js 进行 Imap,将电子邮件标记为已读

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:18 31 4
gpt4 key购买 nike

我将 Imap 与 Node.js 一起使用,每次连接它时,它都会从我提供的数据中获取所有电子邮件,但不会将它们标记为已读,因此下次它会重新接收它们再次。

这是我的代码(这里实现的所有函数都在 main.js 文件中调用),我可以更改什么来将我正在获取的电子邮件标记为已读?

我尝试像这里写的那样( how to mark unseen email as seen with node-imap in node.js ),但没有成功..

var Imap = require('imap');
var inspect = require('util').inspect;
var formatter = require('./formatter.js');

var mailError = exports.mailError = [];

var imap = new Imap({
user: 'aaaaaaaaa@gmail.com',
password: 'xxxxxxx',
host: 'imap.gmail.com',
port: 993, //Perchè non 143, standard IMAP?
tls: true,
markSeen: true
});

function openInbox(cb) {
imap.openBox('INBOX', false, cb);
}


var onBodyCb = exports.onBodyCb = function(parsedHeaders, body){
var pos = (body.indexOf("Delivery to the following recipient failed permanently:") + 55);
var address = body.substr(pos,50);
var at = address.indexOf("@");
address = address.substr(0,at+formatter.company.length+formatter.domain.length+2);
address = address.replace(/ /g,'');
address = address.replace(/(\r\n|\n|\r)/gm,"");
console.log("onBodyCB: "+address+'\n');
mailError.push(address);
};

var onFinishedFetching = exports.onFinishedFetching = function(){};

imap.once('ready', function() {
var fs = require('fs'), fileStream;

openInbox(function(err, box) {
if (err) throw err;
imap.search([ 'UNSEEN', ['SINCE', 'July 10, 2014'] ], function(err, results) {
if (err) throw err;
// a questo abbiamo la box a cui possiamo accedere
var f = imap.fetch(results, { bodies: '' });
f.on('message', function(msg, seqno) {
//console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
console.log(prefix + 'Body');
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
//console.log(buffer);
stream.once('end', function() {
//console.log(prefix + 'Parsed header: %s', inspect());
var header = Imap.parseHeader(buffer);
exports.onBodyCb(header, buffer);
});
});
//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.end();
exports.onFinishedFetching();
});
});
});
});

imap.once('error', function(err) {
console.log(err);
});

imap.once('end', function() {
console.log('Connection ended');

});

exports.connect = function(){
imap.connect();
};

编辑:

我已经按照建议进行了更改,但仍然不起作用...

var Imap = require('imap');
var inspect = require('util').inspect;
var formatter = require('./formatter.js');

var mailError = exports.mailError = [];

var imap = new Imap({
user: 'marco.loco.recruiter@gmail.com',
password: 'coci2014',
host: 'imap.gmail.com',
port: 993, //Perchè non 143, standard IMAP?
tls: true,
markSeen : true,
markRead : true
});

function openInbox(cb) {
imap.openBox('INBOX', false, cb);
}


var onBodyCb = exports.onBodyCb = function(parsedHeaders, body){
var pos = (body.indexOf("Delivery to the following recipient failed permanently:") + 55);
var address = body.substr(pos,50);
var at = address.indexOf("@");
address = address.substr(0,at+formatter.company.length+formatter.domain.length+2);
address = address.replace(/ /g,'');
address = address.replace(/(\r\n|\n|\r)/gm,"");
console.log("onBodyCB: "+address+'\n');
mailError.push(address);
};

var onFinishedFetching = exports.onFinishedFetching = function(){};

imap.once('ready', function() {
var fs = require('fs'), fileStream;

openInbox(function(err, box) {
if (err) throw err;
imap.search([ 'UNSEEN', ['SINCE', 'July 10, 2014'] ], function(err, results) {
if (err) throw err;
// a questo abbiamo la box a cui possiamo accedere
var f = imap.fetch(results, { bodies: '' });
f.on('message', function(msg, seqno) {
//console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
//console.log(prefix + 'Body');
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
//console.log(buffer);
stream.once('end', function() {
//console.log(prefix + 'Parsed header: %s', inspect());
var header = Imap.parseHeader(buffer);
exports.onBodyCb(header, buffer);
});
});
//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.end();
exports.onFinishedFetching();
});
});
},false);
});

imap.once('error', function(err) {
console.log(err);
});

imap.once('end', function() {
console.log('Connection ended');

});

exports.connect = function(){
imap.connect();
};

最佳答案

var Imap = require("imap");
var MailParser = require("mailparser").MailParser;
var Promise = require("bluebird");
Promise.longStackTraces();

var imapConfig = {
user: 'USERNAME',
password: 'PASSWORD',
host: 'HOST',
port: 993,
tls: true
};

var imap = new Imap(imapConfig);
Promise.promisifyAll(imap);

imap.once("ready", execute);
imap.once("error", function(err) {
log.error("Connection error: " + err.stack);
});

imap.connect();

function execute() {
imap.openBox("INBOX", false, function(err, mailBox) {
if (err) {
console.error(err);
return;
}
imap.search(["UNSEEN"], function(err, results) {
if(!results || results.length){console.log("No unseen email available"); imap.end();return;}

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();
});
});
});
}


function processMessage(msg, seqno) {
console.log("Processing msg #" + seqno);
// console.log(msg);

var parser = new MailParser();
parser.on("headers", function(headers) {
console.log("Header: " + JSON.stringify(headers));
});

parser.on('data', data => {
if (data.type === 'text') {
console.log(seqno);
console.log(data.text); /* data.html*/
}

// if (data.type === 'attachment') {
// console.log(data.filename);
// data.content.pipe(process.stdout);
// // data.content.on('end', () => data.release());
// }
});

msg.on("body", function(stream) {
stream.on("data", function(chunk) {
parser.write(chunk.toString("utf8"));
});
});
msg.once("end", function() {
// console.log("Finished msg #" + seqno);
parser.end();
});
}

{markRead: true} 标志有时不起作用,因此您需要显式调用 set imap.setFlags([uuids], ['\Seen'], cb)

关于node.js - 使用 Node.js 进行 Imap,将电子邮件标记为已读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325552/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com