Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Imap = require('imap'),
- inspect = require('util').inspect;
- var imap = new Imap({
- user: 'yourname@gmail.com',
- password: 'yourpassword',
- host: 'imap.gmail.com',
- port: 993,
- tls: true
- });
- function openInbox(cb) {
- imap.openBox('[Gmail]/All Mail', false, cb);
- }
- imap.once('ready', function () {
- openInbox(function (err, box) {
- if (err) throw err;
- var f = imap.seq.fetch('1:' + box.messages.total, {
- bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE X-Uniform-Type-Identifier)',
- struct: true
- });
- f.on('message', function (msg, seqno) {
- var isNote = false;
- console.log('Message #%d', seqno);
- var prefix = '(#' + seqno + ') ';
- msg.on('body', function (stream, info) {
- var buffer = '';
- stream.on('data', function (chunk) {
- buffer += chunk.toString('utf8');
- });
- stream.once('end', function () {
- var headers = Imap.parseHeader(buffer);
- if (headers.hasOwnProperty('x-uniform-type-identifier') && headers['x-uniform-type-identifier'].length > 0 && headers['x-uniform-type-identifier'][0] == 'com.apple.mail-note') {
- console.log(Imap.parseHeader(buffer));
- isNote = true;
- }
- });
- });
- msg.once('attributes', function (attrs) {
- if (isNote) {
- imap.addLabels(attrs.uid, 'Notes', function (err) {
- if (err) console.log('setLabels error: ', err);
- });
- 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.closeBox(function (err) {
- if (err) console.log(err);
- imap.end();
- });
- });
- });
- });
- imap.once('error', function (err) {
- console.log(err);
- });
- imap.once('end', function () {
- console.log('Connection ended');
- });
- imap.connect();
Add Comment
Please, Sign In to add comment