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
- PHP5
- IMAP enabled in your Gmail settings.
- 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.= '
';
 
 $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);
 
   
 
 
  5
5
 
  
  
  
  
  
  
  
 
