Get emails from Gmail inbox with PHP

Rakesh Kumar

Retrieve Your Gmail Emails Using PHP and IMAP

function loadGmailMails($from=0,$to=0){
$hostname = ‘{}INBOX’;
$username = ‘’;
$password = ”;

$inbox = imap_open($hostname,$username,$password) or die(‘Cannot connect to Gmail: ‘ . imap_last_error());
$e = imap_search($inbox,’ALL’);

$emails = array();

if($e) {
rsort($e);//put the newest emails on top

if($to == 0) $to = sizeof($e);

$overview = imap_fetch_overview($inbox,$e[$i],0);
$message = imap_fetchbody($inbox,$e[$i],2);

preg_match('/(?P[a-zA-Z ]+)<(?P


$emails[] = array(‘read’ => $overview[0]->seen, ‘subject’ => $overview[0]->subject, ‘from’ => array(‘name’ => $match[‘name’], ‘address’ => $match[‘address’]), ‘date’ => $overview[0]->date, ‘message’ => $message);


return $emails;

View original post


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s