Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Promise = require("bluebird");
- var Imap = require('imap'),
- inspect = require('util').inspect;
- let buffer = '';
- let OTPCode = '';
- let OTPRegex = /\d{8,}/;
- var imap = new Imap({
- user: "example@gmail.com",
- password: "etc",
- host: "mail.google.net",
- port: 993,
- tls: true,
- 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: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved
- markSeen: true,
- fetchUnreadOnStart: false,
- mailParserOptions: { streamAttachments: false },
- attachments: false,
- attachmentOptions: { directory: "attachments/" }
- });
- function openInbox(cb) {
- imap.openBox('INBOX', false, cb);
- }
- imap.once('ready', function () {
- openInbox(function (err, box) {
- if (err) throw err;
- imap.search(['UNSEEN', ['SUBJECT', 'Requested LexisNexis(R) One-Time Passcode (OTP)']], function (err, results) {
- if (err) throw err;
- var f = imap.fetch(results, { bodies: '1', markSeen: true });
- f.on('message', function (msg, seqno) {
- msg.on('body', function (stream, info) {
- stream.on('data', function (chunk) {
- buffer += chunk.toString('utf8');
- });
- stream.once('end', function () {
- if (info.which === '1') {
- var res = buffer.match(OTPRegex);
- OTPCode = res[0];
- console.log(OTPCode);
- }
- });
- });
- msg.once('attributes', function (attrs) {
- });
- msg.once('end', function () {
- });
- });
- f.once('error', function (err) {
- console.log('Fetch error.');
- });
- f.once('end', function () {
- console.log('Done fetching OTP codes.');
- imap.end();
- });
- });
- });
- });
- imap.once('error', function (err) {
- console.log('An error occured fetching the OTP code.');
- });
- imap.once('end', function () {
- console.log('Connection terminated.');
- });
- module.exports = {
- fetchOTP: async function() {
- imap.connect();
- return OTPCode.toString();
- }
- }
Add Comment
Please, Sign In to add comment