Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Gmail with PHP & IMAP

Tags:

php

ssl

gmail

imap

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.

like image 750
Makis Avatar asked Aug 26 '15 09:08

Makis


People also ask

How do I connect my Gmail outbox to PHP IMAP?

Open Gmail and go to Settings -> Forwarding and POP/IMAP and enable IMAP access.

Can I send email from localhost PHP?

Send Email from Localhost with PHPSet SMTP credentials (host, username, password, and port). Specify sender name and email ( $mail->setFrom ). Set recipient email address ( $mail->addAddress ). Set email subject ( $mail->Subject ).

Is Gmail API free?

Gmail API is available for free, but it has certain daily usage limits for API calls. Daily usage: 1 billion API calls per day. Per User Rate Limit: 250 API calls per user per second.


2 Answers

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

like image 165
Makis Avatar answered Oct 16 '22 21:10

Makis


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'.

like image 3
Johan Velthuis Avatar answered Oct 16 '22 20:10

Johan Velthuis