Saturday, August 26, 2017

PHP IMAP - Get Emails from GMAIL

Using PHP IMAP to retrieve emails on GMAIL.

Fetching emails from your GMIAL account is easier than what you expected. With the use of PHP IMAP Extension, you can easily fetch your emails from your GMIAL account easily without hassle.



Before we start, I will introduce to you the PHP function we will be using fetching emails from you GMAIL account and the requirements for us to be able to use the IMAP Extension:

  • PHP Version must be greater or equal to PHP5.
  • Enable IMAP Extension in your PHP installation (example for XAMPP: xampp/php/php.ini, search for ;extension=php_imap.dll and uncomment the semicolon ";" like this: extension=php_imap.dll).
  • In you Gmail account settings, IMAP configuration should be enabled.

To enable IMAP in Linux, you can install the PHP 5 IMAP module with this command:
 apt-get install php5-imap

then after that, you need to make it enabled since by default it is disabled:
 php5enmod imap

Then restart your apache server
 service apache2 restart



Below are the IMAP functions we will be using for extracting emails from our GMAIL account:

imap_open

Opens an IMAP stream to a mailbox given with the valid account/credentials.

imap_search

This function returns an array of messages matching the given search criteria.

imap_fetch_overview

Read an overview of the information in the headers of the given message.

imap_fetchbody

Fetch a particular section of the body of the message.


You will need to put your valid GMAIL account and fill the username and password. Let the hostname as it is since this is where we connect to GMAIL's IMAP.
 $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
 $username = 'youremail@gmail.com';
 $password = 'yourpassword';


We will then try to connect using the given hostname, username, and password above.
 $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());


Once we connected successfully, we can now grab emails from our GMAIL account using this function below:
 $emails = imap_search($inbox,'ALL');


If emails are returned, we will loop and extract the emails from the array
 if($emails) {        
         $output = '';        
         rsort($emails);        
         foreach($emails as $email_number) {                
                 $overview = imap_fetch_overview($inbox,$email_number,0);
                 $message = imap_fetchbody($inbox,$email_number,2);                
                 $output.= '
'; $output.= ''.$overview[0]->subject.' '; $output.= ''.$overview[0]->from.''; $output.= 'on '.$overview[0]->date.''; $output.= '
'; $output.= '
'.$message.'
'; } echo $output; } imap_close($inbox);


Overview of the code we created:
 $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
 $username = 'youremail@gmail.com';
 $password = 'yourpassword';;
 
 $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
 
 $emails = imap_search($inbox,'ALL');
 
 if($emails) {        
         $output = '';        
         rsort($emails);        
         foreach($emails as $email_number) {                
                 $overview = imap_fetch_overview($inbox,$email_number,0);
                 $message = imap_fetchbody($inbox,$email_number,2);                
                 $output.= '
'; $output.= ''.$overview[0]->subject.' '; $output.= ''.$overview[0]->from.''; $output.= 'on '.$overview[0]->date.''; $output.= '
'; $output.= '
'.$message.'
'; } echo $output; } imap_close($inbox);



Take note that there are some encoding issues and I am also having difficulties to resolve it, any comments, suggestions, and tips are very much appreciated!


Well that's it! Now I have shown you the basics of getting emails using PHP IMAP, try creating your own scripts depending on your usage and necessities :)



1 comment:

  1. Thanks for this tutorial bro! It really helped me out. I'm glad I have seen this tutorials!

    ReplyDelete

PHP IMAP - Get Emails from GMAIL

Fetching emails from your GMIAL account is easier than what you expected. With the use of PHP IMAP Extension, you can easily fetch your e...