Connect to Gmail with PHP & IMAP
Asked Answered
S

3

13

I have enabled OpenSSL and IMAP functions from my php.ini file and phpinfo() confirms it.

By using the code below i can connect to a Hotmail account but not to a Gmail account.(of course i change the $connect_to to point to Hotmail.)

$connect_to = '{imap.gmail.com:993/imap/ssl}INBOX';
$connection = imap_open($connect_to, $user, $password)
  or die("Can't connect to '$connect_to': " . imap_last_error());
imap_close($connection);

The returned error is

Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /opt/lampp/htdocs/webmail_client_practise/index.php on line 6
Can't connect to '{imap.gmail.com:993/imap/ssl}INBOX': Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
Notice: Unknown: Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA (errflg=2) in Unknown on line 0

Unfortunately i cannot find a complete tutorial of how to use the IMAP functions.

Any ideas, solutions or suggestions will be helpful.

Schiro answered 26/8, 2015 at 9:2 Comment(3)
You missed "{" {imap.gmail.com:993/imap/ssl}INBOXNil
I was going to ask if you can access any other folder other than INBOXDougall
I have updated the post with the new error, @SarDauMort - Thank you, i have edited my error.Schiro
S
21

UPDATE:

After searching for my problem on the Internet, i did not found a solution that solves my problem completely. Although if

1) I use the insecure $connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';

2)And allow gmail to be accessed by less secured apps,

solves my problem for now.

Please check this https://support.google.com/accounts/answer/6010255?hl=en

Schiro answered 26/8, 2015 at 10:39 Comment(1)
less secured apps no longer works, but using app passwords will allow you to still use a password just not the account password: support.google.com/accounts/answer/185833?hl=enJablon
C
3

If you get blocked, this can be because google sees a login from a new device.

Go to your google account and check https://myaccount.google.com/device-activity and let them know the 'Unknown device is ok to use'.

Castellanos answered 24/4, 2018 at 15:20 Comment(3)
And in my case: turn on 'Unsave apps allowed' at myaccount.google.com/u/2/lesssecureapps?pageId=noneCastellanos
This did it for me - thanks. Although not sure why I couldn't find that link you posted!Bangtail
In my case the link still works, but you have to be logged in. It's possible that it depends on other settings in your account, so just try to find the 'Unsave apps allowed'Castellanos
C
1

First check your date and time settings on your server are correct.

Alternatively try:

$connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX',

This will stop it from trying to validate certificate.

I will probably get downvotes from people telling me this is bad practice as this leaves you open to Man in the Middle attacks. Which is true.

Credits answered 26/8, 2015 at 9:12 Comment(5)
Haha! Yes i know! Warning: imap_open(): Couldn't open stream {imap.gmail.com::993/imap/ssl/novalidate-cert}INBOX in /opt/lampp/htdocs/webmail_client_practise/index.php on line 6 Can't connect to '{imap.gmail.com::993/imap/ssl/novalidate-cert}INBOX': Can't open mailbox {imap.gmail.com::993/imap/ssl/novalidate-cert}INBOX: invalid remote specification Notice: Unknown: Can't open mailbox {imap.gmail.com::993/imap/ssl/novalidate-cert}INBOX: invalid remote specification (errflg=2) in Unknown on line 0 this is my new errorSchiro
You have put two colons...Or I have in my answer @Makis, sorry!Dougall
and goes on... `Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX in /opt/lampp/htdocs/webmail_client_practise/index.php on line 6 Can't connect to '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX': Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: support.google.com/mail/acco ...........Schiro
At least now it is correctly talking to the gmail server and attempting to authenticate.Dougall
I think you need to go to: accounts.google.com/b/0/DisplayUnlockCaptcha This Google now being extra protective...Dougall

© 2022 - 2024 — McMap. All rights reserved.