# Imap for CakePHP 2.6?

**URL:** <https://discourse.cakephp.org/t/imap-for-cakephp-2-6/6445>\
**Category:** Uncategorized\
**Created:** [July 31, 2019, 1:51pm UTC](https://discourse.cakephp.org/t/imap-for-cakephp-2-6/6445 "2019-07-31T13:51:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [July 31, 2019, 1:51pm UTC](https://discourse.cakephp.org/t/imap-for-cakephp-2-6/6445/1 "2019-07-31T13:51:48Z")

</div>

Hi,  
I found solution for Imap server “Cakephp-tools” :

> <https://github.com/dereuromark/cakephp-tools/blob/2.x/Model/Datasource/ImapSource.php>

Could somebody to show how to use it?

I’ve created:  
In database.php :

```auto
public $imapSrc = array(
        'datasource' => 'ImapSource',
        'host' => 'localhost',
        'login' => 'root',
        'password' => '',
// 'database' => 'cook',
// 'encoding' => 'utf8',
            'username' => 'meuser@gmail.com',
            'password' => 'pass',
            'email' => 'meuser@gmail.com',
            'server' => 'imap.gmail.com',
            'type' => 'imap',
            'ssl' => true,
            'mailbox' => 'INBOX',
            'retry' => 3,
            'error_handler' => 'php',
// 'auto_mark_as' => ['seen'],
            'auto_transform' => true, // transform attachments back into the original file content
        'imap' => ['port' => 993],
        'pop3' => ['port' => 465],
    );
}

```

In Imapme.php:

```auto
<?php
App::uses('AppModel', 'Model');
/**
 * Category Model
 *
 * @property Package $Package
 */
class Imapme extends AppModel {

    public $name = 'Imapme';
    public $useDbConfig = 'imapSrc';
    public $useTable = false;
    public $displayField = "subject";

    //Model::exists() must return true
    function exists($reset = false) {
        return true;
    }

}

```

In ImapmeController.php :

```auto
<?php
App::uses('AppController', 'Controller');
/**
 * Packages Controller
 *
 * @property Package $Package
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class ImapmesController extends AppController {

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator', 'Session', 'Data.CountryProvinceHelper');
    public $helpers = array('Tools.Format', 'Data.Data', 'Js');

    public function index()
    {
        $emails = $this->Imapme ->find('all', array(
            'order' => array('message_date' => 'DESC')
        ));

      debug($emails);

    }

}

```

I got :  
Fatal error: Call to a member function find() on null in

Help me who knows how it works , please.

Thank you.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [July 31, 2019, 4:06pm UTC](https://discourse.cakephp.org/t/imap-for-cakephp-2-6/6445/2 "2019-07-31T16:06:52Z")

</div>

There’s nothing here that would seem to initialize `$this->CompanyEmail`. That particular error has nothing whatsoever to do with IMAP.

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [July 31, 2019, 6:06pm UTC](https://discourse.cakephp.org/t/imap-for-cakephp-2-6/6445/3 "2019-07-31T18:06:36Z")

</div>

Thank you.  
It was miss typing.  
I can connect to Google now.  
But for me unclear -\>find( $conditions ).  
What I can write as $conditions. For example, if I use

```auto
$emails = $this->Imapme->find('all', array(
            'order' => array('message_date' => 'DESC')
        ));

```

I get :

```auto
[**Notice** (8)](javascript:void(0);): Undefined property: stdClass::$messageId [**APP\Model\Datasource\ImapSource.php** , line **971**]

```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [July 31, 2019, 6:20pm UTC](https://discourse.cakephp.org/t/imap-for-cakephp-2-6/6445/4 "2019-07-31T18:20:47Z")

</div>

Does it work if you don’t provide any conditions or order? Just `->find('all');`?

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [July 31, 2019, 8:43pm UTC](https://discourse.cakephp.org/t/imap-for-cakephp-2-6/6445/5 "2019-07-31T20:43:44Z")

</div>

It’s uselees , it works more than 10 min.  
If I use $this-\>Imapme-\>find(‘first’); I see one message like :

```auto
array(
	'Imapme' => array(
		'id' => (int) 577,
		'message_id' => null,
		'email_number' => ' 536',
		'from' => 'no-reply@accounts.google.com',
		'from_name' => 'Google',
		'reply_to' => 'no-reply@accounts.google.com',
		'reply_to_name' => 'Google',
		'sender' => 'no-reply@accounts.google.com',
		'sender_name' => 'Google',
		'subject' => 'Critical security alert',
		'slug' => 'Critical-security-alert',
		'header' => 'Delivered-To: myEmail@gmail.com
Received: by 2002:a17:90a:d982:0:0:0:0 with SMTP id d2csp6531357pjv; Wed, 31
 Jul 2019 10:20:50 -0700 (PDT)
X-Received: by 2002:a9d:19cc:: with SMTP id
 k70mr88861165otk.304.1564593650878; Wed, 31 Jul 2019 10:20:50 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1564593650; cv=none; d=google.com;
 s=arc-20160816;
 b=XSx4R+6M0pj4Lfk2L3WrMNM1mAxNdGJ/h0lKNCR7Afpggf1IslYhVxMDVIH4On2O8P
 cSB+BnfzK6VaaXsYZ2o4KoQDh+yiKE2KiP7I9BnaAnm31xEYfgWfFSAYJfzPgznpkiKX

```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [July 31, 2019, 9:19pm UTC](https://discourse.cakephp.org/t/imap-for-cakephp-2-6/6445/6 "2019-07-31T21:19:51Z")

</div>

Is there more, apart from the actual message body, that follows this? If not, I don’t see any `message_date` field that you might be able to sort on, as you tried to do in the earlier code example.

No firm idea on what you might be doing wrong here, as I know nothing at all about the plugin in question. Hopefully, someone that has experience with it will come along. Or contact the plugin author directly?
