Retrieve emails from gmail using PHP

Posted on Posted in Blog

Before you go over the article please check whether the server you are using has loaded the php_imap extension otherwise the following codes will not work.

IMAP or Internet Message Access Protocol is an application layer protocol used to check and retrieve emails from an email server. This is one of two protocols commonly used to access emails in the server, the other one being POP3 (Post Office Protocol 3). This protocol is capable of only retrieving email. If one is to send an email, the protocol to be used is SMTP or Simple Mail Transfer Protocol.

Let us check first the IMAP settings of gmail before we go to the code proper. We get the following information from that link:

Server: imap.gmail.com
Requires SSL: yes
Port: 993

This information is necessary once we set the mailbox input of the imap_open function.

Let us view an important snippet of the code:
[php]
$host='{imap.gmail.com:993/imap/ssl}INBOX’; //Host to connect
$user=’yourname@gmail.com’;
$pass=’yourpassword’;

$inbox = @imap_open($host,$user,$pass) or die(“Can’t connect: ” . imap_last_error());
[/php]

From this snippet, we let the host be equal to the string:

“{imap.gmail.com:993/imap/ssl}INBOX”

The string actually is in the form:

“{[server]:[port]/[protocol][/ssl or blank]}[folder]”

Its easy to figure out the role of the gmail settings above and applied to that string. The folder used is INBOX which is where the messages are placed.

[php]
$emails = imap_search($inbox,’ALL’);
[/php]

The imap_search function on the snippet returns an array of email number. The second input to the function are criteria to search in the email. A comprehensive list is found here.

An example of a commonly called criteria is: ‘FROM “Cron”‘ which means the emails to fetch comes from Cron (email sent on execution of a cron job).

The imap_search function by the way arranges the array of emails in reversed order with the lowest numbered email being the oldest.

One can then go over the overview or the content of all the emails using imap_fetch_overview or imap_fetchbody functions respectively. The imap_fetch_overview returns an array of objects of the email’s header.

The imap_fetchbody on the other hand returns the content of the email.

Here is a sample code that checks into gmail and prints out the header information and content of the latest email received.

[php]
$host='{imap.gmail.com:993/imap/ssl}INBOX’; //Host to connect
$user=’yourname@gmail.com’;
$pass=’yourpassword’;

$inbox = @imap_open($host,$user,$pass) or die(“Can’t connect: ” . imap_last_error());

$emails = imap_search($inbox,’ALL’);
rsort($emails); //reverse sort so latest comes first…
$overview = imap_fetch_overview ($inbox, $emails[0]);
$body = imap_fetchbody ($inbox, $emails[0], 1);

/* Lets just display information about the header and the body of the email for now */
print_r ($overview);
print_r ($body);
[/php]

And that is it. Its up to you now on what to do with the $overview and $body variable. Next up is sending email from gmail using SMTP.

One thought on “Retrieve emails from gmail using PHP

Leave a Reply

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