How to list emails from your Gmail account using PHP and IMAP

Going deep into my curiosity I was researching on creating my own email server. I created an Email server on my local system and enabled it to send and receive emails and listed them using small PHP script. Since it was on my local system, that’s why it was very slow. I had an azure ubuntu server in my hand and I deployed my email server on Azure server. That was great experience for me and will make it for public in some days.

Coming on topic, while researching on the above I got a thought that I should try listing my emails from gmail account using PHP code as well.

So let’s discuss about listing email from your gmail account using PHP code.

# Prerequisite

  • PHP — PHP should be installed on your system. I tried using PHP 5.5.9
  • IMAP — Imap should be installed, If not then you need to install it using :

$ sudo apt-get install dovecot-imapd

# Installing PHP-IMAP package

Since, you are listing emails using PHP, you need to install PHP-imap package.

If you want to check whether PHP-imap is installed or not, please check your phpinfo()

If it is not installed then, you need to run following command:

$ sudo apt-get install php5-imap

Once installation is complete you need to enable imap and restart apache

$ sudo php5enmod imap

$sudo service apache2 restart

# Remove Gmail Security

Now you are ready to create your php script to list your emails. But, wait is not over because before 2–3 years Gmail allows us to list emails with these scripts but now it make the server too secure so that less secure apps can’t have access to gmails. So first you need to enable imap to access gmail and then you need to make it less secure.

# Writing PHP Scripts

While writing PHP code make sure that your error warnings are ON because It happened to me that error warning was not ON, webpage was showing HTTP 500 error and I was not sure what was the error.

Now first you need to open the IMAP connection with Gmail. If you are using it from your local use ‘novalidate-cert’ parameter in URl because your are not hitting gmail by secured URL.

Source code

<?php $gmailURL = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox";   $username = 'email@gmail.com; $password = 'password'; ?>

If you have any SSL enabled URL then, you don’t need to use ‘novalidate-cert’ as URL parameter.

Source code

<?php $gmailURL = '{imap.gmail.com:993/imap/ssl}INBOX';   $username = 'email@gmail.com; $password = 'password'; ?>

Now you just need to open connection using imap_open() function as below and grab ALL emails.

Now Use the reverse sorting to get the latest email and run foreach to list and put it into your design as you want.

After listing close the IMAP connection

Source code

<?php imap_close($inbox); ?>

That’s all about Gmail account mail listing. Please write me back if you got any problem.

I will tell you How make your system a email hosting server and How to setup Azure system in my another article.

Cheers!!!


Originally published at www.spokenbyyou.com on August 25, 2017.

SPOKEN by YOU

Write your technical, Programming , Startup story

)
Digvijay Kumar Tiwari

Written by

Software engineer, blogger and reader www.spokenbyyou.com

SPOKEN by YOU

Write your technical, Programming , Startup story

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade