3

I'd like to fetch mail from a mailbox regularly using a Node daemon. The call to the connection method is made in app.js.

The javascript file I use to connect to my mailbox (mail.js):

var imap = new Imap({
    user: '[email protected]',
    password: config.get.gmail_password,
    host: 'xxxxx',
    port: 993,
    tls: true
});

var fetchMail = function()
{
    console.log('Connection');
    imap.connect();
};

//fetchMail();

imap.once('ready', function() {
   console.log('Ready'); 

   imap.search([ 'UNSEEN', ['FROM', 'xxxx'] ], function(err, results)
   {
       // Do Stuff
   }

exports.fetchMail = fetchMail;

If I use fetchMail() directly from mail.js, everything is fine.

However, when I try to call it from app.js:

var mail = require('./js/mail');
mail.fetchMail() 

Then, the method stay in the fetchMail() function from mail.js and the imap.once('ready', function())is never triggered.

I guess it is a scope issue with the imap var in mail.js.

How can I fix this?

EDIT

I solved this in a way I don't like. I wrote everything's related to the imap var inside the fecthMail()function.

Please, do not hesitate to write a more efficient answer to this.

5
  • Did you tried module.exports = fetchMail; in mail.js ? Commented May 3, 2016 at 10:16
  • Yes but fetchMail is not a function in app.js. Commented May 3, 2016 at 10:47
  • Not in app.js, you need to first export from mail.js then use it app.js by doing require Commented May 3, 2016 at 11:09
  • Yep. That's what I've done :) Commented May 3, 2016 at 11:10
  • Unfortunately, no. Commented May 3, 2016 at 11:19

2 Answers 2

1

You would need to bind the event every time you connect. So like so:

var fetchMail = function()
{
    console.log('Connection');

    imap.once('ready', function() {
      console.log('Ready');         
      imap.search([ 'UNSEEN', ['FROM', 'xxxx'] ], function(err, results)
      {
        // Do Stuff
      }
    }
    imap.connect();
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! I still am blocked in the connection phase. It would also help me if you could may be explain a bit your solution ? :)
0

The approach and idea is great. All you need is to change the syntax of mail.js file to return a module. In another words when you do

var mail = require('./js/mail');

what do you expect to be in the mail variable?

You might need to change logic around, but try this:

var MailHandler = function () {}

var imap = new Imap({
    user: '[email protected]',
    password: config.get.gmail_password,
    host: 'xxxxx',
    port: 993,
    tls: true
});

MailHandler.init = function(){
  imap.once('ready', function() {
     console.log('Ready'); 

     imap.search([ 'UNSEEN', ['FROM', 'xxxx'] ], function(err, results)
     {
         // Do Stuff
     }
  }
}

MailHandler.fetchMail = function()
{
  console.log('Connection');
  imap.connect();
};

//fetchMail();

module.exports = new MailHandler()

4 Comments

Thank you :) How yould I then call the fetchMail() function inside app.js?
Easy: mail.fetchMail()
I do not think so. Since that when you export the MailHandler module, you create a instance of itself. This instance is created by calling imap.once('ready', function(){}); which is triggered when imap.connect() is done. I think there is something not right in this approach. But I may be wrong, could you explain?
Hmm. You'll have to find out what works for you. But what's for sure, when you do the require it will load whatever is inside, but it will not register the once event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.