从节点开始使用IMAP后获取消息

时间:2017-07-28 08:53:21

标签: node.js email meteor imap node-imap

我使用npm的邮件监听程序包来获取自特定日期以来的所有收件箱邮件。

以下是获取收件箱邮件的代码,

'listener':function(credit,userid = this.userId,date = (new Date()).toString()){
        if(credit.port === "143" || credit.port === "993") {
            let mailListener2 = new MailListener({
                username: credit.user,
                password: credit.password,
                host: credit.host,
                port: credit.port, // imap port
                tls: (credit.port === "993"),
                connTimeout: 10000, // Default by node-imap
                authTimeout: 5000, // Default by node-imap,
                debug: null, // Or your custom function with only one incoming argument. Default: null
                tlsOptions: {rejectUnauthorized: false},
                mailbox: "INBOX", // mailbox to monitor
                searchFilter: [["SINCE", date]],
                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: false, // download attachments as they are encountered to the project directory
                attachmentOptions: {directory: "/data/attachments/"} // specify a download directory for attachments
            });

            mailListener2.on("server:connected", Meteor.bindEnvironment(function () {
                console.log("imapConnected");
            }));

            mailListener2.on("server:disconnected", Meteor.bindEnvironment(function () {
                console.log("imapDisconnected");
            }));

            mailListener2.on("error", Meteor.bindEnvironment(function (err) {
                console.log("error listener", err);
            }));
            mailListener2.on("mail", Meteor.bindEnvironment(function (mail) {
                console.log("receive message");
            }));

            mailListener2.start();
        }else if(credit.port === "587" || credit.port === "465"){
            if not imap ....
        }
        return true;
    },

我想允许我的用户在开始或特定日期开始时选择选项。所以我的问题是,我不知道如何从一开始就获取所有收件箱消息。我怎样才能做到这一点?

我还需要使用SINCE吗?

1 个答案:

答案 0 :(得分:0)

- 如果您想获取所有收件箱消息,您可以选择以下选项:searchFilter:[' UNSEEN']。

- 如果你想获得特定日期的所有消息,可以选择这样的选项(使用时刻库​​):searchFilter:[' UNSEEN',[' SINCE',矩(DATE_USER).format(' 11&#39)]],