Search code examples
phpemailimapibm-cloud

Accesing gmail from bluemix in php imap


I am trying to get mails from gmail account with php imap in bluemix application. I believe it is a bluemix config issue.

I have enabled imap in the gmail acount.

In bluemix I added a file .user.ini and I wrote : extension=php_imap.dll

The php code is:

<?p

    hp
     set_time_limit(60); 
     /* connect to gmail with your credentials */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';

    $username = '[email protected]';  

    $password = 'mypassword';
    /* try to connect */

    print_r('debug: just before accessing gmail');

    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

    print_r('debug: i did not die when accesing gmail ');

    /*here is more code but I do not access this...  */

    ?>

When I run this code I get the first debug message and that's all. no error or other debug.

I might guess that something with the imap.dll setting is wrong.

Can I have advice on this? :)


Solution

  • In the application root directory create the following directory and file if not present:

    .bp-config/options.json
    

    The options.json file should have the following content:

    {
       "PHP_EXTENSIONS": ["imap"]
    }
    

    Push your application again using:

    cf push
    

    Make sure you have IMAP enabled in your Google account and also turn on access for less secure apps per link below:

    https://support.google.com/accounts/answer/6010255

    DISCLAIMER: Google does not recommend to turn on access for less secure apps.