Author Topic: Return emails to use in another page  (Read 5156 times)

Offline wueb

  • Jr. Member
  • **
  • Posts: 10
Return emails to use in another page
« on: September 02, 2012, 05:36:45 PM »
It is possible to return emails from roundcube to use in another application/webpage? Like an API which return emails?

I mean, i have the roundcube installed and i have another part of the website which works like a crm where users can create and list clients. I would like to get the emails from roundcube and list them on the client details webpage (with the same adress email).

If i receive 10 emails from the address example@webpage.com i would like to see that 10 emails on the client with the address example@webpage.com. There is any chance to do this with roundcube hooks or something else?

Thanks!

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,879
    • SKaero - Custom Roundcube development
Re: Return emails to use in another page
« Reply #1 on: September 03, 2012, 01:33:59 AM »
I've done something similar for vTiger where a user in RoundCube can choose to save a email to to a leads profile in vTiger. If you want it to happen automaticity you'd need to setup a pipe system to store it the the CRM's database.

Offline wueb

  • Jr. Member
  • **
  • Posts: 10
Re: Return emails to use in another page
« Reply #2 on: September 07, 2012, 07:33:13 AM »
SKaero, do you have any example from a plugin which returns a list of emails? I would like to create a plugin which receives a arg "email" and returns (prints) a list with all the emails on the mailbox sent by that "email".

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,879
    • SKaero - Custom Roundcube development
Re: Return emails to use in another page
« Reply #3 on: September 07, 2012, 10:16:22 AM »
I don't have any example plugins available at the moment. I'm not sure exactly what you mean, to you just want to search a mailbox for all emails sent by someone?

Offline wueb

  • Jr. Member
  • **
  • Posts: 10
Re: Return emails to use in another page
« Reply #4 on: September 08, 2012, 07:28:35 AM »
Yes and return a list or a JSON for example, to use in another place outside roundcube.

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,879
    • SKaero - Custom Roundcube development
Re: Return emails to use in another page
« Reply #5 on: September 08, 2012, 08:08:18 AM »
You'd need to create another action so your other application had a url to call and then create an IMAP object to perform the search and return the results.

Offline wueb

  • Jr. Member
  • **
  • Posts: 10
Re: Return emails to use in another page
« Reply #6 on: September 08, 2012, 08:17:53 AM »
I tried to make this with the php function imap_search, but it was very slow.

So i try to make a roundcube plugin which return a json when a url (action) from roundcube is called.

The code until now looks something like this:

Code: [Select]
$rcmail = rcmail::get_instance();
       
$rcmail->get_storage()->search('INBOX', 'FROM "'.$_GET['_q'].'"', RCMAIL_CHARSET);
       
$results = $rcmail->get_storage()->get_search_set();
       
$result_h = $rcmail->get_storage()->list_messages('INBOX', 1);
       
$count = $rcmail->get_storage()->count('INBOX', $rcmail->get_storage()->get_threading() ? 'THREADS' : 'ALL');

print_r($result_h);

The print returns the correct results i expect, an array with the emails sent by the address on the $_GET['_q'].

You think this is ok? My problem now is disable the template on the plugin, because i'm getting an error saying i didn't define any template for it.

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,879
    • SKaero - Custom Roundcube development
Re: Return emails to use in another page
« Reply #7 on: September 08, 2012, 08:28:59 AM »
That looks fine, you don't need to get the $count to get the emails so you could delete that line. What you could do is use RoundCube's buit in json output system, you'd get some other variables back along with your data but I think it should work, just add the follow at the end:
Code: [Select]
$rcmail->output->command('emails', $result_h);

Offline wueb

  • Jr. Member
  • **
  • Posts: 10
Re: Return emails to use in another page
« Reply #8 on: September 08, 2012, 10:17:38 AM »
I tried that command but doesn't print nothing.

This is my plugin:

Code: [Select]
class get_emails extends rcube_plugin
{
    function init()
    {
        $rcmail = rcmail::get_instance();

        // register task
        $this->register_task('get_emails');

        // register actions
        $this->register_action('index', array($this, 'action'));
    }

    function action()
    {
        $rcmail = rcmail::get_instance();
       
        $rcmail->get_storage()->search('INBOX', 'FROM "'.$_GET['_q'].'"', RCMAIL_CHARSET);
       
        $results = $rcmail->get_storage()->get_search_set();
       
        $result_h = $rcmail->get_storage()->list_messages('INBOX', 1);
       
        //$count = $rcmail->get_storage()->count('INBOX', $rcmail->get_storage()->get_threading() ? 'THREADS' : 'ALL');
        //print_r($result_h);
       
        $rcmail->output->command('emails', $results);
    }
}

When i access the URL http://roundcube.localhost/?_task=get_emails&_q=address@webmail.pt i get only an error, no JSON.

Quote
PHP Error in /var/www/roundcube/program/include/rcube_template.php (431): Error loading template for get_emails
SERVICE CURRENTLY NOT AVAILABLE!

Error loading template for get_emails

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,879
    • SKaero - Custom Roundcube development
Re: Return emails to use in another page
« Reply #9 on: September 08, 2012, 10:42:11 AM »
Well so much for trying to be fancy. If you replace that line with the following does it work?
Code: [Select]
die(json_encode($results));

Offline wueb

  • Jr. Member
  • **
  • Posts: 10
Re: Return emails to use in another page
« Reply #10 on: September 08, 2012, 11:17:58 AM »
Thanks SKaero it works.  8)

Just one more thing, how can i now add authentication to this plugin? I would like to call the URL by cURL but i believe i need to prepare roundcube to accept remote authentication for it. Is it possible?

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,879
    • SKaero - Custom Roundcube development
Re: Return emails to use in another page
« Reply #11 on: September 08, 2012, 11:50:51 AM »
Take a look at auto login plugin that comes with RoundCube, you should be able to copy most of that into your plugin to do what you want.

Offline wueb

  • Jr. Member
  • **
  • Posts: 10
Re: Return emails to use in another page
« Reply #12 on: September 08, 2012, 12:29:03 PM »
Thanks again SKaero, i managed to make it work.  ;)