Gmail Email Inbox using PHP with IMAP

by Vincy. Last modified on May 26th, 2023.

Gmail is the God of all email services. It took the world by storm by providing unlimited storage and exceptional interface. It would be nice if we can create a custom web UI interface for the Gmail service.

In this article I will present you the PHP code for creating inbox with the emails from the Gmail. We can enhance this code further and make this as a complete email web client using PHP.

Accessing a mail server and reading messages can be done by the protocols like IMAP, POP. The messages data read from the mail server will be used for listing emails in a mailbox, creating notification alert about unread messages and for many purposes.

In this code, by using PHP IMAP extension the Gmail server is accessed to fetch the email data. The connection is created by sending the access request with the credentials like host, username, password and more.

With this connection, the PHP example code sends a search request with a keyword based on which the array of Gmail messages will be returned. To enhance this application as a full fledged Gmail web client using PHP, we need to send email using Gmail SMTP also. You need to collage the linked article to build a complete application. Also I have written about sending email with attachment using Gmail SMTP.


IMAP Configuration in PHP Environment and Gmail

PHP contains in-built imap_* functions for connecting and getting accesses with an external mail server. Before executing PHP imap_* functions, make sure that the IMAP is installed and enabled in the PHP configuration file.

The phpinfo() function will provide us the PHP configuration information about the installed libraries, extensions and more. For executing this example code in your local environment, follow these steps to install and enable IMAP in your PHP. The last step shows the navigation and the screenshot to configure Gmail settings to allow IMAP access.

  1. Install PHP IMAP library. If it is already installed, ignore this step.
  2. Enable IMAP library extension in the PHP configuration file removing the semicolon(;) at the beginning of the line.
    //For Windows,
    //For Linux,
  3. Increase the limit for the max_execution_time directive in the php.ini file
  4. Restart apache to make these changes effective.
  5. Open Gmail and go to Settings -> Forwarding and POP/IMAP and enable IMAP access.

PHP Code to List Emails from Gmail

The following PHP code is used to establish the connection with the Gmail server to fetch the email data. PHP imap_open() function is used to create the connection object by sending the host, username, password and more.

With the reference of the connection object, the array email reference is returned based on the specified search criteria. The email reference object array is iterated to get the mail overview and the content. The retrieved Gmail messages are displayed in an inbox in a tabular view.

<h1>Gmail Email Inbox using PHP with IMAP</h1>
    if (! function_exists('imap_open')) {
        echo "IMAP is not configured.";
    } else {
<div id="listData" class="list-form-container">
        /* Connecting Gmail server with IMAP */
        $connection = imap_open('{}INBOX', 'Your Gmail Username', 'Password') or die('Cannot connect to Gmail: ' . imap_last_error());
        /* Search Emails having the specified keyword in the email subject */
        $emailData = imap_search($connection, 'SUBJECT "Article "');
        if (! empty($emailData)) {
            foreach ($emailData as $emailIdent) {
                $overview = imap_fetch_overview($connection, $emailIdent, 0);
                $message = imap_fetchbody($connection, $emailIdent, '1.1');
                $messageExcerpt = substr($message, 0, 150);
                $partialMessage = trim(quoted_printable_decode($messageExcerpt)); 
                $date = date("d F, Y", strtotime($overview[0]->date));
            <td><span class="column">
                    <?php echo $overview[0]->from; ?>
            <td class="content-div"><span class="column">
                    <?php echo $overview[0]->subject; ?> - <?php echo $partialMessage; ?>
            </span><span class="date">
                    <?php echo $date; ?>
            } // End foreach
        } // end if

PHP Gmail Email Inbox Output

This Screenshot shows the output of the Gmail email inbox using PHP.



Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page