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.
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.
//For Windows,
;extension=php_imap.dll
//For Linux,
;extension=imap.so
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>
<?php
if (! function_exists('imap_open')) {
echo "IMAP is not configured.";
exit();
} else {
?>
<div id="listData" class="list-form-container">
<?php
/* Connecting Gmail server with IMAP */
$connection = imap_open('{imap.gmail.com:993/imap/ssl}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)) {
?>
<table>
<?php
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));
?>
<tr>
<td><span class="column">
<?php echo $overview[0]->from; ?>
</span></td>
<td class="content-div"><span class="column">
<?php echo $overview[0]->subject; ?> - <?php echo $partialMessage; ?>
</span><span class="date">
<?php echo $date; ?>
</span></td>
</tr>
<?php
} // End foreach
?>
</table>
<?php
} // end if
imap_close($connection);
}
?>
</div>
This Screenshot shows the output of the Gmail email inbox using PHP.