mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.17k stars 383 forks source link

Open other mailbox #166

Closed przemyslawpluta closed 11 years ago

przemyslawpluta commented 11 years ago

How can you open other mail boxes after the login into the main account? Thanks.

mscdex commented 11 years ago

Just call openBox() again. You can only have one mailbox open at a time per connection though.

przemyslawpluta commented 11 years ago

Just to clarify the logged in user can access other mailboxes from within their account so after the initial login they can select another one newmailbox@mail.com but with openBox() you can select a diffident directory but not a different mailbox?

mscdex commented 11 years ago

openBox() opens a mailbox within your account. You can get a list of them via getBoxes(). If you want to reach a mailbox on another account, then you'll have to either re-connect or create a separate connection and log in as that account.

So you might do: openBox('INBOX', ....); and then later on do openBox('Work', ....);, etc.

przemyslawpluta commented 11 years ago

Example I'm logging in as usera account but need to access inbox for userb http://goo.gl/yrmgR

mscdex commented 11 years ago

Ok so if you have separate accounts like that you need two separate connections because there is no way (AFAIK) to change users within a connection once you've logged in.

przemyslawpluta commented 11 years ago

Second accound is a shared mailbox but I've got full access to it mailbox but I can access it only after logging into the first one. When I target second mailbox directly with the crudentials of the first one I get login failed.

mscdex commented 11 years ago

What does imap.namespaces contain?

przemyslawpluta commented 11 years ago

After imap.connect logging into the first one namespaces: { personal: [ [Object] ], other: null, shared: null }

mscdex commented 11 years ago

Does imap.getBoxes(...) return the mailboxes for both 'user a' and 'user b' ?

przemyslawpluta commented 11 years ago

No. After imap.connect to 'user a' run imap.getBoxes(...) and lists all the directories for 'user a' with imap.status(...) then after the last directory

assert.js:102
  throw new assert.AssertionError({
        ^
AssertionError: false == true
    at CleartextStream.ondata (/pathtoproject/node_modules/imap/lib/imap.js:594:9)
    at CleartextStream.EventEmitter.emit (events.js:96:17)
    at CleartextStream.CryptoStream._push (tls.js:540:12)
    at SecurePair.cycle (tls.js:898:20)
    at EncryptedStream.CryptoStream.write (tls.js:285:13)
    at Socket.ondata (stream.js:38:26)
    at Socket.EventEmitter.emit (events.js:96:17)
    at TCP.onread (net.js:397:14)
mscdex commented 11 years ago

Can you post the code you're using and additionally set debug: console.log in the constructor and post the resulting output before the error occurs?

przemyslawpluta commented 11 years ago
var Imap = require('imap');

var imap = new Imap({
    user: '',
    password: '',
    host: '',
    port: 993,
    secure: true
});

imap.connect(function(err) { 
    if (err) throw err;
    imap.getBoxes(function (err, boxes) {
        if(err) throw err;
        for(var key in boxes) {
            console.log('status: ' + key);
            imap.status(key, function(err, box) {
                console.log(key, err, box);
            });
        }
    });
});

just before the assert fail

<== 'A6 OK LIST completed.\r\n'

[parsing incoming] saw tagged response
status: Calendar
...
...
status: Tasks

==> A7 STATUS "Calendar" (MESSAGES RECENT UNSEEN UIDVALIDITY)

<== '* STATUS Calendar (MESSAGES 249 RECENT 249 UNSEEN 0 UIDVALIDITY 31741) \r\nA7 OK STATUS completed.\r\n'

[parsing incoming] saw unexpected untagged response: '* STATUS Calendar (MESSAGES 249 RECENT 249 UNSEEN 0 UIDVALIDITY 31741) '
mscdex commented 11 years ago

Hrmm, I cannot seem to reproduce this with the latest node-imap version. What node-imap version are you using?

Also, you should probably use something like this if your account has a hierarchy:

  imap.getBoxes(function more(err, boxes, path) {
    if (err) throw err;
    if (!path)
      path = '';
    for (var key in boxes) {
      if (boxes[key].children)
        more(undefined, boxes[key].children, path + key + boxes[key].delimiter);
      else {
        console.log('status: ' + key);
        imap.status(path + key, function(err, box) {
          console.log(key, err, box);
        });
      }
    }
  });
przemyslawpluta commented 11 years ago

Versions node@0.8.18 npm@1.2.2 imap@0.7.7 and using code:

imap.connect(function(err) {
  if(err) throw err;
    imap.getBoxes(function more(err, boxes, path) {
        if (err) throw err;
        if (!path)
          path = '';
        for (var key in boxes) {
          if (boxes[key].children)
            more(undefined, boxes[key].children, path + key + boxes[key].delimiter);
          else {
            console.log('status: ' + key);
            imap.status(path + key, function(err, box) {
              console.log(key, err, box);
            });
          }
        }
    });
});

assert fail after last directory for 'user a' ...

==> A7 STATUS "Calendar/Inbox/Inbox/info" (MESSAGES RECENT UNSEEN UIDVALIDITY)

<== '* STATUS "Calendar/Inbox/Inbox/info" (MESSAGES 0 RECENT 0 UNSEEN 0 UIDVALIDITY 182636) \r\nA7 OK STATUS completed.\r\n'

[parsing incoming] saw unexpected untagged response: '* STATUS "Calendar/Inbox/Inbox/info" (MESSAGES 0 RECENT 0 UNSEEN 0 UIDVALIDITY 182636) '

assert.js:102
  throw new assert.AssertionError({
        ^
AssertionError: false == true
    at CleartextStream.ondata (/path/node_modules/imap/lib/imap.js:594:9)
    at CleartextStream.EventEmitter.emit (events.js:96:17)
    at CleartextStream.CryptoStream._push (tls.js:540:12)
    at SecurePair.cycle (tls.js:898:20)
    at EncryptedStream.CryptoStream.write (tls.js:285:13)
    at Socket.ondata (stream.js:38:26)
    at Socket.EventEmitter.emit (events.js:96:17)
    at TCP.onread (net.js:397:14)
mscdex commented 11 years ago

Try the master branch now and let me know how that does/doesn't work.

przemyslawpluta commented 11 years ago

Based on the master branch it lists all the directories for the 'user a' there is no assert fail, goes idle after last directory


<== 'A46 OK STATUS completed.\r\n'

[parsing incoming] saw tagged response
Tasks null { name: 'Tasks',
  uidvalidity: 31770,
  messages: { total: 0, new: 0, unseen: 0 } }

==> IDLE IDLE

<== '+ IDLE accepted, awaiting DONE command.\r\n'

[parsing incoming] saw continuation response

... it doesn't list anything for 'user b'

mscdex commented 11 years ago

Ok, so 'user a' has no direct access to the mailboxes for 'user b.' The only way is to log in as 'user b' and access the mailboxes from there.

przemyslawpluta commented 11 years ago

Thanks.