Mail-listener2 does not work

This is my code for the mail listener file.

I call the startListening function in my main file, and I can read "Imap connected" in the console, but then, even if some kind of message arrives, nothing happens.

Any idea?

var MailListener = require("mail-listener2"); var mailListener = new MailListener({ username: " myEmail@gmail.com ", password: "myPassword", host: "imap.gmail.com", port: 993, // imap port tls: true, 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, // all fetched email willbe marked as seen and not fetched next time }); module.exports.startListening = function(){ mailListener.start(); // start listening } // stop listening //mailListener.stop(); mailListener.on("server:connected", function(){ console.log("imapConnected"); }); mailListener.on("server:disconnected", function(){ console.log("imapDisconnected"); }); mailListener.on("error", function(err){ console.log(err); }); mailListener.on("mail", function(mail, seqno, attributes){ // do something with mail object including attachments console.log("emailParsed", mail); // mail processing code goes here }); mailListener.on("attachment", function(attachment){ console.log(attachment.path); }); 
+6
source share
2 answers

I had the same problem. The example does not work. Instead, I use mail-notifier :

 var notifier = require('mail-notifier'); var imap = { user: " _example@example.com _", password: "password", host: "imap.gmail.com", port: 993, tls: true, tlsOptions: { rejectUnauthorized: false } }; notifier(imap).on('mail',function(mail){ console.log("GOT MAIL"); }).start(); 

Works like a charm

+8
source

For the code snippet to work, delete:

 searchFilter: "FLAGGED" 

You are currently just looking for invisible, tagged emails, and probably the incoming email address is not marked as important. I had the exact implementation above, without the FLAGGED search filter, and this works great.

+3
source

Source: https://habr.com/ru/post/973912/


All Articles