Search This Blog

May 16, 2012

Retrieving Messages from your GMAIL account using PHP

Using imap library of PHP language you can access and retrieve messages in your gmail account. This is useful when you’re trying to back up or store your gmail messages in your local database or whatever purpose you want to do in the messages.

I created an example on how to do it in PHP and I will explain in detail on how to implement it.

Before we proceed:

1.Please make sure that you enabled the php_imap extension. If you are using wamp server, you can enable it by clicking wamp icon tray then go to "PHP->PHP extensions" and then look for php_imap on the list. Click it to enable then restart your wamp.

Now, let’s get down on the code!
<?php

//these are the variables that you need in order to 
//access your gmail account
//in this example, we were using gmail impa hostname 

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'yourgmail@gmail.com';  // please change this into your correct gmail address. 
$password = 'yourgmailpassword'; // gmail password


//connect to your gmail... die and show error if can't connect 
$inbox = imap_open($hostname,$username,$password,OP_READONLY) or die('Cannot connect to Gmail: ' . imap_last_error());

//search messages on your gmail inbox. You can set other parameter
//in imap_search function and you can also further filter 
//this search function
//@ FROM "emailaddress@gmail.com" - you can change this to 
//the email address you want to retrieve the message 
$emails = imap_search($inbox,'FROM "emailaddress@gmail.com"');

//check if there are results on imap_search.... 
if($emails){

 //declare limit variable... this will limit the # of email to show. 
 $limit = 0; 
 
 //loop each email retrieve.
 foreach($emails as $email_number){
  
  //fetch the email over view... if you want to see the overview of the email.(OPTIONAL)
  $overview = imap_fetch_overview($inbox,$email_number,0);
  
  //fetch the email content body... This will show only the email content. 
  $message = imap_fetchbody($inbox, $email_number,"1");
  //set the message format to UTF-8
  $message = imap_utf8($message);
  
  //strip existing HTML tags to display text content only.(JUST COMMENT IT IF YOU WANT TO DISPLAY IT IN AN HTML FORMAT)
  $message = strip_tags($message);
  
  //add break in each line of the image. 
  $message = nl2br($message);
  
  //Now, you can either echo the image or USE for for other functions like saving in database. 
  echo $message;
  
  // increment limit 
  $limit++;
  
  //if limit is true. Break the loop and exit. 
  if($limit === 1)
   break;
  }
 
}
 
//close the imap connection
imap_close($inbox);


?>
Change the username and the password variable value to your gmail credential.

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'yourgmail@gmail.com';  
$password = 'yourgmailpassword';

The following block of code connects you to gmail using your credential. Then, it will search the message using imap_search function.

$inbox = imap_open($hostname,$username,$password,OP_READONLY) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'FROM "emailaddress@gmail.com"');

If there are results on the imap_search. You are going to use the foreach loop to get each message content by using the imap_fetchbody. After that, you will need to set the message format to UTF-8, strip the message using strip_tags to exclude HTML tags. The nl2br function add break to your message. You also has option to limit the number of message by changing the "1" on the $limit === 1 conditional code. After the loop, imap_close is called to terminate the imap connection.

if($emails){

 
 $limit = 0; 
 
 foreach($emails as $email_number){
  
  $overview = imap_fetch_overview($inbox,$email_number,0);
  $message = imap_fetchbody($inbox, $email_number,"1");
  $message = imap_utf8($message);
  $message = strip_tags($message);
  $message = nl2br($message);
  
 
  echo $message;
  
  // increment limit 
  $limit++;
  
  
  if($limit === 1)
   break;
  }
 
}
 
imap_close($inbox);

3 comments:

LikeHowTo said...

That's really complicated for me. But thanks, I learn something new today =)

Arfa Maham said...

I have tried this code with my gmail username and password. But it will show this on browser:
"
#include
#include
#include
class stack
{
private:
const int size;
int A[10];
int top;
public:


stack():size(10)
{
top=0;
for(int i=0;i
"
Kindly reply me the reason and help me. I shall be very thankful to you.
Reply me on my gmail id: arfamaham.pieas@gmail.com

Janzell Jurilla said...

Hi Arfa,

Please send me an email @ janzell1989@gmail.com so that I can review your code.

Thanks,

Janzell