Created
November 28, 2017 14:39
-
-
Save RobsonX4/7b8bed7b1ca48e63d6309924afb14780 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Imap = require('imap'); | |
| const moment = require('moment'); | |
| const fs = require('fs'); | |
| const imap = new Imap({ | |
| user: 'ENDERECO_DE_EMAIL', | |
| password: 'SENHA', | |
| host: 'imap-mail.outlook.com', // 'imap.gmail.com' | |
| port: 993, | |
| tls: true, | |
| connTimeout: 500000, | |
| authTimeout: 500000, | |
| socketTimeout: 500000 | |
| }); | |
| function openInbox(cb) { | |
| imap.openBox('INBOX', true, cb); | |
| } | |
| imap.on('ready', () => { | |
| openInbox((err, box) => { | |
| if (err) throw err; | |
| const DATE = new Date(); | |
| const MMM = moment(DATE).format('MMM'); | |
| const DD = DATE.getDate(); | |
| const YYYY = DATE.getFullYear(); | |
| const FILTER_DATE = `${MMM} ${DD}, ${YYYY}`; | |
| console.log('======================================================'); | |
| console.log(moment().format('DD-MM-YYYY hh:mm:ss')); | |
| console.log('LENDO E-MAILS DO DIA'); | |
| console.log('======================================================'); | |
| // Pesquisa e-mails da data, Ex.: Nov 28, 2017 | |
| imap.search([['SINCE', FILTER_DATE]], (err, results) => { | |
| if (err) throw err; | |
| console.log( | |
| '======================================================' | |
| ); | |
| console.log(`(${results.length}) E-MAILS ENCONTRADOS.`); | |
| const f = imap.fetch(results, { bodies: '' }); | |
| f.on('message', (msg, seqno) => { | |
| msg.on('body', stream => | |
| stream.pipe( | |
| fs.createWriteStream( | |
| `${MMM}-${YYYY}-${DD}_${seqno}-email.txt` | |
| ) | |
| ) | |
| ); | |
| msg.once('end', () => console.log(`E-MAIL ${seqno} -> LIDO.`)); | |
| }); | |
| f.once('error', err => console.error(`Ops Erro: ${err}`)); | |
| f.once('end', () => imap.end()); | |
| }); | |
| }); | |
| }); | |
| imap.on('error', err => console.error(err)); | |
| imap.on('end', () => { | |
| console.log('CONEXÃO FINALIZADA'); | |
| console.log('======================================================'); | |
| }); | |
| imap.connect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment