Problem fixed. The solution was to call the imap_init-function, from the rcmail-instance.
Like this:
$rcmail = rcmail::get_instance();
$rcmail->imap_init(); //This piece of code does the trick
Then, as previously stated, getting the object, looping it to create the string, then returning it to the JavaScript. Feels really simple now in hindsight.