Integrate gmail on your website using IMAP

This tutorial demonstrates how to integrate the basic functionality of the gmail system like displaying mailboxes and reading emails using IMAP.

php imap

Install php5-imap

First thing we should do is to install php5-imap because it doesn't come with the default php installation. To install, use this command:

# apt-get install php5-imap

This will enable us to use imap functions but only in php cli. We will need to add the imap extension directive in php.ini.

extension=imap.so

Connect to gmail server

$server = "{imap.gmail.com/imap/ssl}";
$username = "[email protected]";
$password = "password";

$conn = imap_open ( $server , $username , $password );

Retrieve mailboxes

Next, we will use the imap_list function to retrieve the mailbox names so we can access them and retrieve the emails.

$mailboxes = imap_list( $conn, $server, "*" );

Output:

Array
(
    [0] => {imap.gmail.com/imap/ssl}INBOX
    [1] => {imap.gmail.com/imap/ssl}[Gmail]/All Mail
    [2] => {imap.gmail.com/imap/ssl}[Gmail]/Drafts
    [3] => {imap.gmail.com/imap/ssl}[Gmail]/Important
    [4] => {imap.gmail.com/imap/ssl}[Gmail]/Sent Mail
    [5] => {imap.gmail.com/imap/ssl}[Gmail]/Spam
    [6] => {imap.gmail.com/imap/ssl}[Gmail]/Starred
    [7] => {imap.gmail.com/imap/ssl}[Gmail]/Trash
)

Connect to a mailbox

After we got the list of mailboxes, we can open a mailbox using the imap_reopen function:

imap_reopen( $conn, $server . '[Gmail]/Sent Mail" );

Retrieve emails from the mailbox

php gmail imap

The imap_num_msg function returns the number of emails in a mailbox. We need that number so we can know when to stop reading. Next, we need to retrieve each subject of an email along with other information like from, date, seen, etc. We can do this with the imap_fetch_overview function which returns the following data:

Array
(
    [0] => stdClass Object
        (
            [subject] => Test mail
            [from] => noreply: 
            [to] => [email protected]
            [date] => Mon, 24 Jun 2019 19:25:23 -0000
            [message_id] => <#[email protected]>
            [size] => 4216
            [uid] => 12
            [msgno] => 1
            [recent] => 0
            [flagged] => 0
            [answered] => 0
            [deleted] => 0
            [seen] => 1
            [draft] => 0
        )
)

And for the last, we will retrieve the message body using the imap_fetchbody function

function read_mailbox( $conn ){
	$data = array();
	$count = imap_num_msg( $conn );
	
	for( $number = 1; $number <= $count; $number++ ) {
		$overview = imap_fetch_overview( $conn, $number, 0 );
		$data[$number]['seen'] = $overview[0]->seen;
		$data[$number]['subject'] = $overview[0]->subject;
		$data[$number]['from'] =  $overview[0]->from;
		$data[$number]['date'] = $overview[0]->date;		
		$data[$number]['message'] = quoted_printable_decode( imap_fetchbody( $conn, $number, 2 ) );
	}
	
	return $data;
}

Another useful function is imap_search which gives us an option to search through the emails.

$emails = imap_search( $conn, 'ALL' );

On the following link you can find a list of search criteria for this function: http://www.php.net/manual/en/function.imap-search.php

That's it. Try to tweak the code and add more functionalities like marking the emails as read/unread.

- Posted by Eva to Php
tags: imap gmail