【问题标题】:Node JS - nodemailer & imap-simple - Identify threadsNode JS - nodemailer & imap-simple - 识别线程
【发布时间】:2020-03-30 16:29:04
【问题描述】:

(我为我糟糕的英语感到抱歉,但我会尽力而为!):)

我正在尝试为个人项目设置本地邮箱,并且我正在尝试使用 imap-simple 和 nodemailer 来执行此操作。

我希望能够在我发送电子邮件时识别线程。

这就是我真正想做的:

在我的应用程序中,我可以向特定的人发送电子邮件(让我们承认 foo@bar.com)

发送邮件时,回调函数会将邮件内容和主题存储在数据库中(例如,在 CRM 应用程序中,我会将发送的邮件存储在我的数据库中的特定记录中)。

复杂的部分就在那之后:

当这个人回复这封电子邮件时,我想使用 IMAP 来识别这个人正在回复我以前的邮件,然后将它也存储在数据库中,还链接到我在第一封电子邮件中使用的同一记录-邮件。

我实际上在沙盒文件夹中有这个(对于 IMAP):

var imaps = require('imap-simple');
var nodemailer = require('nodemailer');

var config = {
    imap: {
        user: 'catchall@xxxxxxxx.fr',
        password: 'xxxxxxxx',
        host: 'imap.gmail.com',
        port: 993,
        tls: true,
        authTimeout: 3000
    }
};

imaps.connect(config).then(function (connection) {

    return connection.openBox('INBOX').then(function () {
        var searchCriteria = [
            'UNSEEN'
        ];

        var fetchOptions = {
            bodies: ['HEADER', 'TEXT'],
            markSeen: false,
        };
        console.log('Passing there');
        return connection.search(searchCriteria, fetchOptions).then(function (results) {
            var subjects = results.map(function (res) {
                return res.parts.filter(function (part) {
                    return part.which === 'HEADER';
                })[0].body.subject[0];
            });
            console.log('BASE');
            console.log(results[0]);
            console.log('FIRST');
            console.log(results[0].parts[0]);
            console.log('SECOND');
            console.log(results[0].parts[1]);
        });
    });
});

这里是 SMTP 部分:

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
           user: 'catchall@xxxxxxxx.fr',
           pass: 'xxxxxxxx'
       }
});

const mailOptions = {
    from: 'catchall@xxxxxxxx.fr', // sender address
    to: 'xxxxxxxx@gmail.com', // list of receivers
    subject: 'Subject of your email', // Subject line
    html: '<p>Here is my test !</p>',// plain text body
    headers: {
        'collection': 'Pipelines',
        'doc-ID': 'mydocid'
    }
};

transporter.verify(function(error, success) {
    if (error) {
      console.log(error);
    } else {
      console.log("Server is ready to take our messages");
    }
});

transporter.sendMail(mailOptions, function (err, info) {
    if(err)
      console.log(err)
    else
      console.log(info);
});

这是我希望能够做到的屏幕:

IMAP 上,我在控制台中得到了我想要的信息:

BASE
{ attributes:
   { date: 2019-12-05T16:53:07.000Z,
     flags: [],
     uid: 94,
     modseq: '27800',
     'x-gm-labels': [ '\\Important' ],
     'x-gm-msgid': '1652099423356172171',
     'x-gm-thrid': '1652099362185438260' },

x-gm-thrid 允许我识别线程。也许,我在 nodemailer 回调函数中找不到这些信息:

Server is ready to take our messages
{ accepted: [ 'xxxxxxxxxxxxxxxxx@gmail.com' ],
  rejected: [],
  envelopeTime: 400,
  messageTime: 819,
  messageSize: 346,
  response: '250 2.0.0 OK  1575566223 m3sm12955793wrs.53 - gsmtp',
  envelope:
   { from: 'catchall@xxxxxxxxxx.fr',
     to: [ 'xxxxxxxxxxxx@gmail.com' ] },
  messageId: '<f93b3970-e17a-84b5-d0d1-ebb4fd6efe46@xxxxxxxxxx.fr>' }

有人知道我该如何进行吗?

非常感谢!

编码愉快:)

【问题讨论】:

  • 您能否将其更改为 function (err, info) {..}function (err, info, response) {..} 并检查来自 response 参数的内容。此外,x-gm-* 是 gmail 特定的标头。它不是由 nodemailer 返回的
  • 您的外发邮件应包含message-id 字段。响应将包含 in-reply-toreferences 或两者都包含您的 outgoing 消息的 message-id,因此您可以轻松地将传出消息与响应相关联。
  • @AritraChakraborty ,我尝试了响应参数,但其中显然没有任何内容:/ @amt ,好的,我明白了,你知道这是否存在于任何消息中,没有任何特定字段,如@ AritraChakraborty 为x-gm-* ?我试试这个看看

标签: node.js smtp imap


【解决方案1】:

对于那些将阅读这篇文章的人,这里是答案,感谢@arnt,

正如@arnt 所说,对于 IMAP 协议,有一个 reference 属性,用于存储存储在 results[0].parts[1] 中的上一个消息 ID:

references: [ '<23df0af1-8ff1-0ffa-091e-a645a38e4a67@foobar.fr>' ],
     'in-reply-to': [ '<23df0af1-8ff1-0ffa-091e-a645a38e4a67@foobar.fr>' ],

这个也可以在你发送消息时使用,info

  envelope:
   { from: 'foobar@foobar.fr',
     to: [ 'foobar@gmail.com' ] },
  messageId: '<23df0af1-8ff1-0ffa-091e-a645a38e4a67@foobar.fr>' }

这样,我们就能关联邮件对话。

非常感谢您的帮助:)

话题结束

【讨论】:

    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多