2

Im trying to fetch unread mails from a google apps mail account.

Ive been trying the follow script:

$email = 'name@domain.com';
$pass  = 'password';
$inbox = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', $email, $pass, NULL, 1) or die('Cannot connect to Gmail');

.. but I get the following error:

Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /var/www/zvinx.dk/test/gmail.php on line 5
Cannot connect to Gmail

I've contacted my webhost and he states that imap_open is supported by the webhotel. Is there a certain way to check this?

Basicly, how do I display and flag the mails as read with php?

Christoffer
  • 482
  • 7
  • 32

1 Answers1

0

PHP's imap_last_error() and imap_errors() functions may tell you what's going on.

<?php
$mbox = imap_open ("{www.yoursite.com:143}INBOX", "$username", "$password");
if ( $mbox === false ) {
       exit ("Can't connect: " . imap_last_error() ."\n");
}
else
{
       echo "Login worked!";
       //do stuff
}
?>

More: http://us.php.net/manual/en/function.imap-last-error.php

Camden S.
  • 2,185
  • 1
  • 22
  • 27
  • If I add imap_last_error I get the following error message: Can't open mailbox {imap.gmail.com:993/imap/ssl}INBOX: invalid remote specification Regarding your link, I have not enabled the 2-step verification :/ – Christoffer Apr 11 '12 at 17:42
  • Ah okay - I see you're trying to connect with SSL. Can you confirm if your PHP is compiled with these flags?: --with-imap-ssl --with-kerberos --with-openssl These are required for imap with SSL. – Camden S. Apr 11 '12 at 18:14
  • Also see: http://stackoverflow.com/questions/1274911/connecting-to-gmail-through-imap-with-php-ssl-context-failed – Camden S. Apr 11 '12 at 18:20
  • Im not sure if I've got the correct info, but with phpinfo() I read: '--with-openssl=/usr', '--with-imap=/usr/local/src/imap-2001a/' and the only place I can find "kerberos" is in the server path – Christoffer Apr 11 '12 at 18:21
  • Okay - then that's likely the problem, you could either ask your sys admin to compile PHP with those flags, or you could just try connecting to gmail without SSL. – Camden S. Apr 11 '12 at 21:01