b68/packages/wh/helper/imap.js

60 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const Imap = require('node-imap')
const inspect = require('util').inspect
let imap = new Imap({
user: 'mygmailname@gmail.com',
password: 'mygmailpassword',
host: 'imap.gmail.com',
port: 993,
tls: true,
})
function openInbox(cb) {
imap.openBox('INBOX', true, cb)
}
imap.once('ready', function () {
openInbox(function (err, box) {
if (err) throw err
let f = imap.seq.fetch('1:3', {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
struct: true,
})
f.on('message', function (msg, seqno) {
console.log('Message #%d', seqno)
let prefix = '(#' + seqno + ') '
msg.on('body', function (stream, info) {
let buffer = ''
stream.on('data', function (chunk) {
buffer += chunk.toString('utf8')
})
stream.once('end', function () {
console.log(
prefix + 'Parsed header: %s',
inspect(Imap.parseHeader(buffer))
)
})
})
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()
})
})
})
imap.once('error', function (err) {
console.log(err)
})
imap.once('end', function () {
console.log('Connection ended')
})
imap.connect()