UrbanPro
true

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

Swadesh
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.

set_time_limit(4000);


// 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 = '';
rsort($emails);

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.= '

'.$message.'

';

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

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

}
echo $output;
$output = '';
}

// colse the connection
imap_expunge($inbox);
imap_close($inbox);

0 Dislike
Follow 0

Please Enter a comment

Submit

P

Priyanka | 07/05/2019

I didn't get image.

0 0
A

Akash | 26/04/2019

Thanks for this code..

0 0
M

Manoj Sinha | 23/02/2017

Thanks for this code

1 0
P

Punam | 21/01/2017

wow this is great code. Congratulations!

1 0
J

Jagdeesh | 21/01/2017

Nice article

0 0

Other Lessons for You

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...

Core PHP v/s PHP Frameworks
PHP Frameworks: Framework is a piece of software by which one can develop large enterprise, robust & secure software applications. Frameworks provide straight forward flow and strict guidelines by...

How To Connect To Server Using PHP?
It is very easy to connect to server using PHP and MYSQL. PHP is a very friendly Programming Language that any one can learn who know the basics of the Front End Development. PHP is a programming Language...

SQL Tips (1 to 3)
SQL tip 1: Instead of Full Outer Join, you can use Union All. Difference is, Full Outer Join provides results side by side. Union All provides result s one by one. Note: MYSQL does not support Full...

Top Programming Languages 2017: Every Beginner Should Learn
Every year a plethora of job opportunities are being created for skilled programmers. So if you are thinking of honing your coding skills it is really a bright idea. But with so many programming languages...
X

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