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

  • Pingback: How to send push notification to iOS using PHP | SPOKEN by YOU()

  • Sanjit Kumar Mishra

    Please let me know wt is host

    • admin

      Are you asking about webhost?

  • Pingback: Easy steps to create Microsoft virtual machine | SPOKEN by YOU()

  • There is noticeably a lot of money to understand this. I assume you made particular nice points in features also.

  • There a few fascinating points with time in the following paragraphs but I do not know if I see these center to heart. You can find some validity but I will take hold opinion until I investigate it further. Very good post , thanks and then we want much more! Included in FeedBurner also

  • I am really loving the theme/design of your site. Do you ever run into any web browser compatibility problems? A small number of my blog audience have complained about my blog not operating correctly in Explorer but looks great in Firefox. Do you have any solutions to help fix this issue?

  • It?¦s truly a nice and helpful piece of information. I?¦m happy that you simply shared this useful information with us. Please stay us up to date like this. Thank you for sharing.