如何获取发送到和接收自电子邮件ID的所有电子邮件?

时间:2019-07-19 14:06:27

标签: javascript google-api node-imap

我正在尝试实现node-imap,我需要获取所有从中接收并发送到电子邮件ID的电子邮件。

我已经能够使用https://developers.google.com/oauthplayground来生成刷新令牌,但是它会在一个小时内过期,因此我需要一种直接从代码中生成刷新令牌的方法,在搜索请求响应中我没有得到任何刷新令牌邮件主题或其他内容。

let Imap = require("imap"),
  inspect = require("util").inspect,
  config = require('lt-config'),
  xoauth2 = require("xoauth2"),
  xoauth2gen,imap;

xoauth2gen = xoauth2.createXOAuth2Generator({
    user: "mymailid",
    clientId: config.MAIL_GOOGLE_CLIENT_ID,
    clientSecret: config.MAIL_GOOGLE_CLIENT_SECRET,
    refreshToken: config.MAIL_REFRESH_TOKEN,
    accessToken: config.MAIL_ACCESS_TOKEN
});
function getAuthorizedImap() {
    return new Promise((resolve, reject) => {
        xoauth2gen.getToken(function(err, token) {
            if (err) {
              return console.log(err);
            }
            console.log("AUTH XOAUTH2 " + token);
            config.xoauth2 = token;
            imap = new Imap({
                user: "ishan.75way@loadtap.com",
                xoauth2: token,
                host: "imap.gmail.com",
                port: 993,
                tls: true
              });
              imap.once("ready", function() {
                resolve(imap);
              });

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

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

              imap.connect();
        });
        setTimeout(() => reject('Timeout get token process'), 6000);
    })
}



function filterMails(emailId) {
  return new Promise(async resolve => {
      try {
        imap = await getAuthorizedImap();
        openInbox(function(err, box) {
          if (err) throw err;
          imap.search([ ['HEADER', 'FROM', emailId] ], function(err, results) {
            if (err) throw err;
            console.log("results", results);
            var f = imap.fetch(results, { bodies: "HEADER.FIELDS (FROM TO SUBJECT DATE)" });
            f.on("message", function(msg, seqno) {
              console.log("Message #%d", seqno);
              var prefix = "(#" + seqno + ") ";
              msg.on("body", function(stream, info) {
                console.log(prefix + "Body");
                console.log("Body received", info);                
              });
              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();
            });
          });
        });
      } catch(e) {
          console.log("Errror while getting emails from GMAIL ====>>", e);
      }

  });
}

function openInbox(cb) {
    imap.openBox("INBOX", true, cb);
}

module.exports = filterMails;

我想要搜索的JSON响应,并动态生成刷新令牌。任何帮助都将适用。

0 个答案:

没有答案