

























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.

If you want to learn PHP, buy a great deal on it Globally and in India

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

Enable Imap : Goto Security > Forwarding and POP/IMAP > Imap access and enable it.

Login your gmail and open URL https://myaccount.google.com/lesssecureapps?pli=1 and then enable it for less secure apps.

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

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

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

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!!!

Share this: Tweet





Like this: Like Loading...