How to Fetch Emails from Gmail Inbox Using PHP

Recently one of my client requested me to fetch all of mails from his Gmail Inbox.Because he want to save some time and directly wanna show them in his website’s admin panel . I Thought it is quite hard but fortunately reading email from the gmail was easier than i think all thanks to IMAP extension.

So below I will tell you how to retrieving emails from the gmail Inbox:

NOTE: This will work only if you meet the following requirements

• PHP version should be PHP5 or latest
• PHP IMAP Extension should be enabled in your PHP installation
• IMAP should be enabled in your Gmail settings.

How to enable IMAP in XAMPP

By default IMAP is not enabled in Xampp distribution, so to enable it, Go to the file \xampp\php\php.ini (or open xampp control panel > click on “config” in front of Apache > select php.ini see image below) and search for ;extension=php_imap.dll (Line no. 609) and by removing the beginning semicolon at the line ,it’s get enabled ,it should be: extension=php_imap.dll. after doing this restart APACHE

How to Fetch Emails from Gmail Using PHP

How to enable IMAP in Linux

You can install the PHP5 IMAP module with this command :
apt-get install php5-imap

However it’s not enabled by default so enable it with:
php5enmod imap

To see the changes ,restart Apache
service apache2 restart

Here is the PHP script for retrieve Your Gmail Emails Using IMAP.

and run your file in browser.it will print all the mails from your inbox.

If you are getting this warning :

Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox in /var/www/clint/gmail/gmail.php on line 10
Can't connect to '{ {imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox': TLS/SSL failure for imap.gmail.com: SSL context failed

Then The Solution Is :

1) Logged in via browser to gmail account.
2) Open this url “https://www.google.com/settings/security/lesssecureapps
3) Click at “turn on”
4) and try above code again

How to Fetch Emails from Gmail Using PHP

Best of Luck

Hope it will Help You and please like my facebook page :

and make me proud.By the way if you face any problem then let me know in comment section below

Leave a Reply

Your email address will not be published. Required fields are marked *