Gmail

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()

PHP info

 

PHP Info

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

$ sudo apt-get install php5-imap

Imap

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

$ sudo php5enmod imap

$sudo service apache2 restart

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

 

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.

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

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

<?php
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
?>

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

<?php
rsort($emails);
 
foreach($emails as $emailVal) {
 
$overview = imap_fetch_overview($inbox,$emailVal,0);
$message = imap_fetchbody($inbox,$emailVal,2);// HTML list code or send it to view
}
?>

After listing close the IMAP connection

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