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

Introduction to Programming Languages
What is a Programming Language? A programming language is a formal computer language or constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages...

Migration from other languages to Salesforce
Anyone can easily migrate from other language to Salesforce. People must have skills to understand business logic.

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 Would Be Life Cycle Of A Fresher After Campus In An IT Company?
1. Basic Technical Training: Since freshers are not subject matter experts so gone through 3 - 6 months basic technical training within Organization. 2. Technical Assessment: HR sends freshers to various...

Components of Digital Marketing
Content marketing Search Engine Optimization (SEO) Paid Search (PPC) Email marketing Social Media Marketing Digital Display Marketing (Ad Ops) Web Analytics and Reporting Mobile Marketing 30...
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