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.