question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I seem to have some trouble understanding connection events

See original GitHub issue

I connect to gmail and can open Inbox. It works to fetch emails. But the mail event gets only fired once at start, when i fetch mails, it does not get fired when i send a new mail.

imap.once('mail', function(numNewMsgs) {
	l('NEW MESSAGE', numNewMsgs);
});

also subscribing does not seem to have any effect. I have all connection events logging their args to console, but they never get fired. My understanding would have been, that not ending imap would keep the connection open and new mail to a subscribed box would trigger mail or update or any other event. But nothing happens. What am I getting wrong?

This is my whole testcode:

global.l = console.log;
import Imap from 'imap';
import { inspect } from 'util';

const imap = new Imap({
	user: 'xxx@gmail.com',
	password: 'xxx',
	host: 'imap.gmail.com',
	port: 993,
	tls: true,
	tlsOptions: { servername: 'imap.gmail.com' },
	debug: console.log
});

imap.once('ready', function() {
	imap.getBoxes(function(error, boxes) {
		if (error) throw error;
//l('BOXES', boxes);
		for (let box in boxes) {
			imap.subscribeBox(box);
			l('SUBSCRIBED TO', box);
		}
	});
	imap.openBox('INBOX', true, function(error, box) {
		if (error) throw error;

		var f = imap.seq.fetch('1:50', {
			bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
			struct: true
		});
		f.on('message', function(msg, seqno) {
			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() {
					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('mail', function(numNewMsgs) {
	l('NEW MESSAGE', numNewMsgs);
});

imap.once('update', function(seqno, info) {
	l('UPDATE', seqno, info);
});

imap.once('expunge', function(seqno) {
	l('expunge', seqno);
});

imap.once('alert', function(message) {
	l('alert', message);
});

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

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

imap.connect();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mscdexcommented, Oct 16, 2020

Oh I see the issue now, you’re using imap.once() instead of imap.on(). Change that and you should see the new mail notifications.

0reactions
mscdexcommented, Oct 17, 2020

Actually subscribeBox() is a bit of a relic back when the creators of IMAP envisioned some sort of Usenet-like “mailbox” subscription experience.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connecting With People - What It Is and Isn't, And Why You ...
Psychological problems that cause difficulty connecting with others. There are several, including: low self-esteem.
Read more >
9 most common network issues and how to solve them
Find out how to troubleshoot common network issues, including slow network speeds, weak Wi-Fi signals and IP address problems.
Read more >
Aphasia: Types, Causes, Symptoms & Treatment
Aphasia is a brain disorder where a person has trouble speaking or understanding other people speaking. This happens with damage or ...
Read more >
Fix sync problems with the Google Calendar app - Android
Use this page if events you created or updated aren't showing on your computer or in the Google Calendar app. First, try these...
Read more >
Aphasia
Symptoms of aphasia. People with aphasia often have trouble with the 4 main ways people understand and use language. These are: reading; listening;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found