我有一个使用node-imap运行9个邮件侦听器的应用程序。当一封新邮件到达其中一封邮件时,我将根据其标题和内容将其移至右侧框。问题在于并非所有邮件都正确移动。在代码之后,我将进一步说明:
function sortEmails (imap, box) {
let emails = [];
let lead;
let site, date;
if (box.messages.total) {
// fetch all of the current inbox's mails
var imapFetch = imap.seq.fetch(`1:${box.messages.total}`, {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'] // Get headers, body and struct
});
// For each message get the data
imapFetch.on('message', function(msg) {
msg.on('body', function(stream, info) {
stream.setEncoding('utf8');
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
// Once done, check if lead or not, and act accordingly.
stream.once('end', function() {
// If header
if (info.which !== 'TEXT') {
let header = Imap.parseHeader(buffer);
// If lead, set date & site of lead.
if (header.subject[0] === 'Lead') {
site = header.from[0].slice(header.from[0].indexOf("@") + 1, -1);
date = header.date[0];
};
// If body
} else {
// if lead (spam leads included) and not regular mail.
if (site && date) {
body = parseMailBody(buffer);
// Create a new lead
lead = new Lead(body.name, body.email, body.phone, date, site, body.url, body.ip);
if (lead.isRealLead()) {
emails.push(lead);
} else {
lead = 'spam';
}
}
}
});
});
msg.once('attributes', function(attrs) {
if (!lead) {
imap.move(attrs.uid, imap.boxes.all, function(err) {
if (err) {
logger.log('error', 'Mail Listeners', 'Move error: ' + err);
}
});
} else if (lead === 'spam') {
imap.move(attrs.uid, imap.boxes.spamLeads, function(err) {
if (err) {
logger.log('error', 'Mail Listeners', 'Move error: ' + err);
}
});
} else {
imap.move(attrs.uid, imap.boxes.leads, function(err) {
if (err) {
logger.log('error', 'Mail Listeners', 'Move error: ' + err);
}
});
}
});
});
imapFetch.once('error', function(err) {
logger.log('error', 'Mail Listeners', 'Fetch error: ' + err);
});
imapFetch.once('end', function() {
if (emails.length !== 0) {
sendBotMessage(`A new lead has just arrived.`);
sendToDB(emails);
}
});
}
}
基本上,如果邮件没有标题“ Lead”,它将被移至“ ALL”目录。此外,标题为“ Lead”且仅包含英文字符的邮件将被标记为垃圾邮件。
问题是,某些是线索的邮件(包含“ Lead”标题和名称不是英文)被移到了“ 'ALL” 文件夹中,表示(!lead)是正确的。
邮件是从Wordpress中的CF7表单接收的。这是未正确移动的邮件的MIME:
Return-Path: <my@host.co.il>
Delivered-To: my@mail.co.il
Received: from s-inbox.upress.io
by s-inbox.upress.io with LMTP
id OKTqFcSEIFzwBwAAmWbRAw
(envelope-from <my@host.co.il>)
for <my@mail.co.il>; Mon, 24 Dec 2018 09:03:32 +0200
Return-path: <my@host.co.il>
Envelope-to: my@mail.co.il
Delivery-date: Mon, 24 Dec 2018 09:03:32 +0200
Received: from my@host.co.il ([myip])
by s-inbox.upress.io with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256)
(Exim 4.90_1)
(envelope-from <my@host.co.il>)
id 1gbKGy-0000tR-6i
for my@mail.co.il; Mon, 24 Dec 2018 09:03:32 +0200
Received: from hostname by my@host.co.il with local (Exim 4.87)
(envelope-from <my@host.co.il>)
id 1gbKGw-0000Lk-Oz
for my@mail.co.il; Mon, 24 Dec 2018 09:03:30 +0200
To: my@mail.co.il
Subject: Lead
X-PHP-Originating-Script: 1029:class-phpmailer.php
Date: Mon, 24 Dec 2018 07:03:30 +0000
From: WordPress <admin@domain.co.il>
Reply-To: test@gmail.com
Message-ID: <7b2769f42336ec7bf49edce68de30074@domain.co.il>
X-Mailer: PHPMailer 5.2.22 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Sender: <my@host.co.il>
SPFCheck: Server passes SPF test, -30 Spam score
Forward-Confirmed-ReverseDNS: Reverse and forward lookup success on 185.217.97.242, -10 Spam score
SpamTally: Final spam score: -40
name: בדיקה
mail: test@gmail.com
phone: 0435599539
ip: 84.108.61.243
url: https://myurl.co.il/
对于一般的流概念,特别是对imap来说,我还是一个新手,我似乎无法弄清这里出了什么问题。
感谢您的阅读时间。