仅获取那些通过电子邮件回复发送的带有节点js附件的邮件
get only those mail which are send through email reply with attachment in node js
你好,我试图只接收那些作为电子邮件回复收到的电子邮件,这些电子邮件是由我的 system.for 发送的,更具体地说,我正在发送带有附件的电子邮件。收件人回复带有附件的电子邮件。
我想要我的系统中的电子邮件历史记录,它显示针对哪封电子邮件收到了哪个附件。例如,如果我已向 pearson B.Pearson 发送电子邮件,B 将回复 email.i 想要这方面的历史记录。
我可以发送和接收 email.But 问题是我正在获取 a 和 b 之间的所有电子邮件(我只想要那些作为特定电子邮件的回复发送的电子邮件)。下面我给了给出获取电子邮件的代码。
async function mailreply(req, res) {
Employee.email_reply(req.params.email, function (err, employee) {
var imapConfig = {
user: '*****',
password: '****',
host: 'imap-mail.outlook.com',
port: 993,
tls: true,
tlsOptions: {
rejectUnauthorized: false
}
};
var imap = new Imap(imapConfig);
imap.once("ready", checkMail);
imap.once("error", function (err) {
console.log("Connection error: " + err.stack);
});
imap.connect();
function checkMail() {
imap.openBox("INBOX", false, function (err, mailBox) {
if (err) {
console.error(err);
return;
}
imap.search(["UNSEEN", ["FROM", req.params.email]], function (err, results) {
if (!results || !results.length) {
req.flash('error', 'No reply from selected user.')
res.locals.message = req.flash();
res.redirect(nodeSiteUrl+'/dashboard');
imap.end(); return; }
});
});
}
var mailListener = new MailListener({
username: "******",
password: "******",
host: "imap-mail.outlook.com",
port: 993, // imap port
tls: true,
connTimeout: 10000, // Default by node-imap
authTimeout: 5000, // Default by node-imap,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
searchFilter: [ 'UNSEEN', ['FROM', req.params.email] ], // the search filter being used after an IDLE notification has been retrieved ,
markSeen: false, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
attachments: true, // download attachments as they are encountered to the project directory
attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});
mailListener.start();
mailListener.on("server:connected", function(){
console.log("imapConnected");
});
mailListener.on("error", function(err){
console.log('hello');
});
mailListener.on("mail", function(mail, seqno, attributes){
console.log(seqno)
let attachments = [];
var attachment = mail.attachments;
if(attachment) {
for(var i = 0, len = attachment.length; i < len; ++i){
attachments.push(attachment[i].fileName);
}
}
passDatatoPage(mail, attachments);
mailListener.stop();
})
mailListener.on("attachment", function(attachment){
let filepath = './public/attachment/receive/';
let output = fs.createWriteStream(filepath + attachment.fileName);
attachment.stream.pipe(output).on("end", function () {
console.log("All the data in the file has been read");
}).on("close", function (err) {
console.log("Stream has been cloesd.");
});
});
function passDatatoPage(mail, attachments) {
var sender = mail.to[0].address;
var senderName = mail.from[0].name;
var reciver = mail.from[0].address;
const date = moment(mail.date).format('LL');
var subject = mail.subject;
var message = mail.text;
//console.log(attachments);
var result = attachments.map(function(val) {
return val;
}).join(',');
var attachmentArr = result.split(',');
console.log(attachmentArr)
res.render('email-reply', { title: 'Email Reply', to: sender, senderName: senderName, from: reciver, date: date, subject: subject, msg: message, attachments: attachmentArr});
}
});
};
exports.mailreply = mailreply;
在这段代码中,我得到 header 此处显示的错误
code error
我可以知道我的代码有什么问题吗?
请在此代码中提供可能的解决方案或提供我如何完成任务的方式。
提前致谢。
你需要做两件事
当您发送电子邮件时,在数据库中存储消息 ID
获取消息 ID 并根据以下代码更改搜索过滤器
(使用下面的搜索过滤器按 ID 查找消息)
var mailListener = new MailListener({
username: "HR2@almashriq.edu.sa",
password: "Tab13510",
host: "imap-mail.outlook.com",
port: 993, // imap port
tls: true,
connTimeout: 10000, // Default by node-imap
authTimeout: 5000, // Default by node-imap,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
searchFilter: [ ['HEADER','IN-REPLY-TO',messageId] ], // the search filter being used after an IDLE notification has been retrieved ,
markSeen: false, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
attachments: true, // download attachments as they are encountered to the project directory
attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});
你好,我试图只接收那些作为电子邮件回复收到的电子邮件,这些电子邮件是由我的 system.for 发送的,更具体地说,我正在发送带有附件的电子邮件。收件人回复带有附件的电子邮件。
我想要我的系统中的电子邮件历史记录,它显示针对哪封电子邮件收到了哪个附件。例如,如果我已向 pearson B.Pearson 发送电子邮件,B 将回复 email.i 想要这方面的历史记录。我可以发送和接收 email.But 问题是我正在获取 a 和 b 之间的所有电子邮件(我只想要那些作为特定电子邮件的回复发送的电子邮件)。下面我给了给出获取电子邮件的代码。
async function mailreply(req, res) {
Employee.email_reply(req.params.email, function (err, employee) {
var imapConfig = {
user: '*****',
password: '****',
host: 'imap-mail.outlook.com',
port: 993,
tls: true,
tlsOptions: {
rejectUnauthorized: false
}
};
var imap = new Imap(imapConfig);
imap.once("ready", checkMail);
imap.once("error", function (err) {
console.log("Connection error: " + err.stack);
});
imap.connect();
function checkMail() {
imap.openBox("INBOX", false, function (err, mailBox) {
if (err) {
console.error(err);
return;
}
imap.search(["UNSEEN", ["FROM", req.params.email]], function (err, results) {
if (!results || !results.length) {
req.flash('error', 'No reply from selected user.')
res.locals.message = req.flash();
res.redirect(nodeSiteUrl+'/dashboard');
imap.end(); return; }
});
});
}
var mailListener = new MailListener({
username: "******",
password: "******",
host: "imap-mail.outlook.com",
port: 993, // imap port
tls: true,
connTimeout: 10000, // Default by node-imap
authTimeout: 5000, // Default by node-imap,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
searchFilter: [ 'UNSEEN', ['FROM', req.params.email] ], // the search filter being used after an IDLE notification has been retrieved ,
markSeen: false, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
attachments: true, // download attachments as they are encountered to the project directory
attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});
mailListener.start();
mailListener.on("server:connected", function(){
console.log("imapConnected");
});
mailListener.on("error", function(err){
console.log('hello');
});
mailListener.on("mail", function(mail, seqno, attributes){
console.log(seqno)
let attachments = [];
var attachment = mail.attachments;
if(attachment) {
for(var i = 0, len = attachment.length; i < len; ++i){
attachments.push(attachment[i].fileName);
}
}
passDatatoPage(mail, attachments);
mailListener.stop();
})
mailListener.on("attachment", function(attachment){
let filepath = './public/attachment/receive/';
let output = fs.createWriteStream(filepath + attachment.fileName);
attachment.stream.pipe(output).on("end", function () {
console.log("All the data in the file has been read");
}).on("close", function (err) {
console.log("Stream has been cloesd.");
});
});
function passDatatoPage(mail, attachments) {
var sender = mail.to[0].address;
var senderName = mail.from[0].name;
var reciver = mail.from[0].address;
const date = moment(mail.date).format('LL');
var subject = mail.subject;
var message = mail.text;
//console.log(attachments);
var result = attachments.map(function(val) {
return val;
}).join(',');
var attachmentArr = result.split(',');
console.log(attachmentArr)
res.render('email-reply', { title: 'Email Reply', to: sender, senderName: senderName, from: reciver, date: date, subject: subject, msg: message, attachments: attachmentArr});
}
});
};
exports.mailreply = mailreply;
在这段代码中,我得到 header 此处显示的错误 code error
我可以知道我的代码有什么问题吗? 请在此代码中提供可能的解决方案或提供我如何完成任务的方式。 提前致谢。
你需要做两件事
当您发送电子邮件时,在数据库中存储消息 ID
获取消息 ID 并根据以下代码更改搜索过滤器
(使用下面的搜索过滤器按 ID 查找消息)
var mailListener = new MailListener({
username: "HR2@almashriq.edu.sa",
password: "Tab13510",
host: "imap-mail.outlook.com",
port: 993, // imap port
tls: true,
connTimeout: 10000, // Default by node-imap
authTimeout: 5000, // Default by node-imap,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
searchFilter: [ ['HEADER','IN-REPLY-TO',messageId] ], // the search filter being used after an IDLE notification has been retrieved ,
markSeen: false, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
attachments: true, // download attachments as they are encountered to the project directory
attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});