• Uncategorized
  • 2 MINUTES READ

Parsing and Retrieving Emails from Gmail Inbox Using PHP5 IMAP

  • POSTED ON
  • June 24, 2016
  • POSTED BY
  • Aayan Arif
  • POSTED ON June 24, 2016
  • POSTED BY Aayan Arif

Recently, one of our clients requested a functionality to parse and retrieve emails from his Gmail inbox using PHP that should be based on the email content. vteams engineer completed this request using Internet Message Access Protocol (IMAP) – an Internet standard protocol used by e-mail clients to retrieve e-mail messages from a mail server over a TCP/IP....

Recently, one of our clients requested functionality to parse and retrieve emails from his Gmail inbox using PHP which should be based on the email content. vteams engineer completed this request using Internet Message Access Protocol (IMAP) – an Internet standard protocol used by email clients to retrieve email messages from a mail server over a TCP/IP connection.

The example mentioned below will only work for retrieving emails from Gmail inbox if the following requirements are met:

  1. PHP version should be PHP5 (or the latest one)
  2. PHP IMAP Extension should be enabled in your PHP installation
  3. IMAP should be enabled in your Gmail settings
    .

Enable IMAP in XAMPP

IMAP is not enabled by default in XAMPP distribution. To enable it, go to the file xamppphpphp.ini and search for;extension=php_imap.dll. Now remove the beginning semicolon of;extension=php_imap.dll. Once you are done, it gets enabled and now it should be like extension=php_imap.dll.

Enable IMAP in Linux

In Linux, you can install the PHP5 IMAP module with the following command:

apt-get install php5-imap

It is also not enabled by default. You can do so using the php5enmod imap command. The changes can be seen after restarting Apache, using the following command:

service apache2 restart

PHP Script

Following is the PHP script for retrieving emails using IMAP:

/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'gmail_email_address';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {        
        /* begin output var */
        $output = '';        
        /* put the newest emails on top */
        rsort($emails);        
        /* for every email... */
        foreach($emails as $email_number) {                
                /* get information specific to this email */
                $overview = imap_fetch_overview($inbox,$email_number,0);
                $message = imap_fetchbody($inbox,$email_number,2);                
                /* output the email header information */
                $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
                $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
                $output.= '<span class="from">'.$overview[0]->from.'</span>';
                $output.= '<span class="date">on '.$overview[0]->date.'</span>';
                $output.= '</div>';
                
                /* output the email body */
                $output.= '<div class="body">'.$message.'</div>';
        }        
        echo $output;
}
imap_close($inbox);

Connect Email Servers with PHP imap_open

To connect email servers with PHP imap_open, you need to follow the steps mentioned below:

  1. Connect to Gmail Inbox with IMAP Settings using this code:
     $gmail_inbox = imap_open('{imap.gmail.com:993/imap/ssl}INBOX','example@gmail.com','password') or die('Cannot connect to gmail');
  2. Connect to Yahoo Inbox with IMAP Settings using this code:
    $yahoo_inbox = imap_open('{imap.mail.yahoo.com:993/imap/ssl}INBOX','example@yahoo.com','password') or die('Cannot connect to yahoo');
  3. Connect to AOL Inbox with IMAP Settings using this code:
    $aol_inbox = imap_open('{imap.aol.com:993/imap/ssl}INBOX','example@aol.com','password') or die('Cannot connect to aol');
  4. Connect to Local Mail Server using localhost IMAP Settings. Use the following code for it:
    $local_inbox = imap_open('{localhost:993/imap/ssl}INBOX','example@exampledomain.com','password') or die('Cannot connect to example domain');
  5. To connect Custom Mail Server using IMAP Settings, you may need to connect to your company mail server first using a function. You need to have the correct imap server, SSL, or tls enabled and a correct port. At times even if you provide the correct details, the server will generate a notification of certificate failure. In this situation, you have to use the novalidate-cert option to allow access to the email server. For connecting Custom Mail Server using IMAP Settings, use the following code:
    $local_inbox = imap_open('{imap.example.com:993/imap/ssl/novalidate-cert} INBOX','example@exampledomain.com','password') or die('Cannot connect to example domain');

Hire Php Developer with vteams who is fluent in Parsing and Retrieving Emails from Gmail Inbox Using PHP5 IMAP.

ABOUT THE AUTHOR

Aayan Arif

Content Strategist at vteams - Aayan has over 8 years of experience of working with multiple industries.

0 Comments

Leave a Reply

More Related Article
We provide tips and advice on delivering excellent customer service, engaging your customers, and building a customer-centric business.