Learn PHP from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Read Your Gmail Emails Using PHP and IMAP

12/01/2017 0 5

Hello Friends ,
Recently I have worked on drip email campaign project for that I need to read emails using PHP From the gmail inbox.

Here is the simple example for Read emails from the gmail:

To start with we should have the following minimum requirements

  1. PHP5
  2. IMAP enabled in your Gmail settings.
  3. PHP IMAP Extension is enabled

How to enable IMAP in PHP

How to Enable IMAP in XAMPP : –

IMAP is not enabled by default in Xampp distribution, so to enable it go to the file “\xampp\php\php.ini” and search for “;extension=php_imap.dll” and by removing the beginning semicolon at the line ,it’s get enabled ,it should be: extension=php_imap.dll.

How to Enable IMAP in Wampp : –

1] \wamp\bin\apache\apache2.2.22\bin
Enable php_imap.dll extension by removing ; at beginning of string

2] \wamp\bin\php\php5.3.13
Enable php_imap.dll extension by removing ; at beginning of string

Configuration Setting

/* connect to gmail with your credentials */
$hostname = ‘{imap.gmail.com:993/imap/ssl}INBOX’;
$username = ‘YOUR_GMAIL_USERNAME’; # e.g helloworld@gmail.com
$password = ‘YOUR_GMAIL_PASSWORD’; # your gmail password

Connection using Gmail’s IMAP
$inbox = imap_open($hostname,$username,$password,NULL,1) or die(‘Cannot connect to Gmail: ‘ . print_r(imap_errors()));

Here is the full PHP script for read Your Gmail Inbox Emails Using IMAP.


// Connect to gmail
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'yourmail@gmail.com';
$password = 'bond007';

// try to connect
$inbox = imap_open($hostname,$username,$password,NULL,1) or die('Cannot connect to Gmail: ' . print_r(imap_errors()));

/* ALL - return all messages matching the rest of the criteria
ANSWERED - match messages with the \\ANSWERED flag set
BCC "string" - match messages with "string" in the Bcc: field
BEFORE "date" - match messages with Date: before "date"
BODY "string" - match messages with "string" in the body of the message
CC "string" - match messages with "string" in the Cc: field
DELETED - match deleted messages
FLAGGED - match messages with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set
FROM "string" - match messages with "string" in the From: field
KEYWORD "string" - match messages with "string" as a keyword
NEW - match new messages
OLD - match old messages
ON "date" - match messages with Date: matching "date"
RECENT - match messages with the \\RECENT flag set
SEEN - match messages that have been read (the \\SEEN flag is set)
SINCE "date" - match messages with Date: after "date"
SUBJECT "string" - match messages with "string" in the Subject:
TEXT "string" - match messages with text "string"
TO "string" - match messages with "string" in the To:
UNANSWERED - match messages that have not been answered
UNDELETED - match messages that are not deleted
UNFLAGGED - match messages that are not flagged
UNKEYWORD "string" - match messages that do not have the keyword "string"
UNSEEN - match messages which have not been read yet*/

// search and get unseen emails, function will return email ids
$emails = imap_search($inbox,'ALL');

$output = '';

foreach($emails as $mail) {

$headerInfo = imap_headerinfo($inbox,$mail);
$overview = imap_fetch_overview($inbox,$mail,0);
$message = imap_fetchbody($inbox,$mail,2);
$output .= ($overview[0]->seen ? 'read' : 'unread').'
$output .= $headerInfo->subject.'
$output .= $headerInfo->toaddress.'
$output .= $headerInfo->date.'
$output .= $headerInfo->reply_to[0]->mailbox.'@'.$headerInfo->reply_to[0]->host.'
$output .= $headerInfo->reply_toaddress.'
$output.= '



$emailStructure = imap_fetchstructure($inbox,$mail);

if(!isset($emailStructure->parts)) {
$output .= imap_body($inbox, $mail, FT_PEEK);
} else {

echo $output;
$output = '';

// colse the connection

0 Dislike
Follow 0

Please Enter a comment



Priyanka | 07/05/2019

I didn't get image.

0 0

Akash | 26/04/2019

Thanks for this code..

0 0

Manoj Sinha | 23/02/2017

Thanks for this code

1 0

Punam | 21/01/2017

wow this is great code. Congratulations!

1 0

Jagdeesh | 21/01/2017

Nice article

0 0

Other Lessons for You

PHP Intro.
What is PHP? PHP is an acronym for "PHP Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP costs nothing, it is free to download and...

What is Array in PHP | Types of Array
An array is a special variable, which can hold more than one value at a time or you can say An array stores multiple values in one single variable. If you have a list of items (a list of car names, for...

Necessity of Theory and Practical in Computer Science.
Upon studying a subject both theory and practical are important. Usually many schools concentrate more on theory and the marks not on the practical. Other and opposite kind of people prefer practical...

Student Project Development: How To Go About It?
A lot of students (count me as well) in their final year of IT course are confused and overwhelmed about their Project development. Final Year IT Projects are a crucial milestone in every IT students educational...

Web Technology Tutorial
Web Technology has 2 category Front End (HTML,CSS,JS Etc) Back End/Server Side (PHP,JSP,ASP,ROR Etc) Front End technologies are - HTML, CSS, Bootstrap, Javascript, jQuery etc. Using front end...

Looking for PHP Classes?

The best tutors for PHP Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn PHP with the Best Tutors

The best Tutors for PHP Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more