Home  »  Projects »  GmAtom

GmAtom

Tags:  

GmAtom - Gmail ATOM feed parsing package

This package queries Gmail accounts by parsing the ATOM feed provided by Gmail. It consists of three classes - GmCurlConnection (connection class), GmAtom (main class, extends GmCurlConnection) and GmMessageInfo (individual message class).

GmAtom is made to be a quick and easy solution to a simple task of programmatically checking new mail on Gmail accounts, using Gmail Atom feeds. You can check for new mail on one or more accounts and obtain the basic info on new messages - from, subject, date, link to full message and a body excerpt. You can check new mail for the entire inbox, or just for certain label(s).

GmAtom was intended to be a quick way of checking one's Gmail inbox, but in more predictable and reliable manner then mere screen scraping, as well as to avoid common pitfalls of using Gmail POP access (requires explicit enabling, clashes with other POP applications accessing the same account). However, this approach has certain...

Limitations

Since the package relies on ATOM feed provided by Gmail, there is currently no way to fetch full message body using GmAtom package, simply because it is not provided in Gmail ATOM feed. You can, however, fetch the message excerpt and a link to the message in Gmail interface.

Update: Somewhere in January 2009 I started getting support pleas regarding mail limits. It seems that Gmail team introduced a limit of 20 messages to their ATOM feed, and since the class is dependant on what Gmail provides in the feed, it is also a new limit of unread mail you can fetch using GmAtom.

Requirements

You need cURL extension enabled for this to work.

Example usage

These are the basic usage scenarios. For more details on how things work, check out docs on particular classes: GmCurlConnection, GmAtom or GmMessageInfo.


1) Checking a single account

include ('../gmatom.php');
$g = new GmAtom ('username', 'password');
$msgCount = $g->check();
if (false === $msgCount) {
     die ('Error');
} else {
     echo "<h1>".$msgCount." new messages</h1>";
}

2) Checking multiple accounts

include ('../gmatom.php');
$accounts = array (
     '0' => array ('username'=>'username1', 'password'=>'password1'),
     '1' => array ('username'=>'username2', 'password'=>'password2'),
);
$g = new GmAtom();
foreach ($accounts as $a) {
     $g->setLoginInfo ($a['username'], $a['password']);
     // We need fresh connection for each account, so we supply
     // (bool)true to check() method.
     $msgCount = $g->check(true);
     if (false !== $msgCount) {
         echo $msgCount . ' messages for ' . $g->username . '<br />';
     } else {
         echo 'There has been an error checking "' . $g->username . '" account<br />';
     }
}

3) Receive all mail from Gmail Inbox

include ('../gmatom.php');
$g = new GmAtom ('username', 'password');
$msgCount = $g->check();
if (false === $msgCount) {
    die ('Error');
} else {
    echo "<h1>".$msgCount." messages</h1>";
    $g->receiveAll();
    $g->sortBy('subject', true);
    echo '<dl>';
    foreach ($g->messages as $mid=>$msg) {
         echo '<dt>'.$mid.': <B>'.$msg->from.'</b> '.$msg->subject.'</dt>';
         echo '<dd>'.$msg->body.'</dd>';
         echo '<dd>'.@date('r', $msg->timestamp)." ({$msg->date})".'</dd>';
         echo '<dd><a href="'.$msg->link.'">'.$msg->link.'</a></dd>';
}
    echo '</dl>';
}

4) Receive default number of new messages from label 'mylabel'

Note: no body excerpts are gathered in this example.

include ('../gmatom.php');
$g = new GmAtom ('username', 'password');
$g->setLabel('mylabel');
$msgCount = $g->check();
if (false === $msgCount) {
     die ('Error');
} else {
     echo "<h1>".$msgCount." messages</h1>";
     $g->receive();
     $g->sortBy('subject', true);
     echo '<dl>';
     foreach ($g->messages as $mid=>$msg) {
         /* If you need message bodies as well, you could do $msg->getBody(); here. */
         /* Then, you could echo $msg->body, or whatever */
         echo '<dt>'.$mid.': <B>'.$msg->from.'</b> '.$msg->subject.'</dt>';
         echo '<dd>'.date('r', $msg->timestamp)." ({$msg->date})".'</dd>';
         echo '<dd><a href="'.$msg->link.'">'.$msg->link.'</a></dd>';
    }
    echo '</dl>';
}

5) Receive specific number of new messages from Gmail Inbox

include ('../gmatom.php');
$g = new GmAtom ('username', 'password');
$msgCount = $g->check();
if (false === $msgCount) {
    die ('Error');
} else {
    echo "<h1>".$msgCount." messages</h1>";
    // If you don't need the message body, omit the third parameter:
    $g->receive(2,5, true);
    $g->sortBy('subject', true);
    echo '<dl>';
    foreach ($g->messages as $mid=>$msg) {
        echo '<dt>'.$mid.': <B>'.$msg->from.'</b> '.$msg->subject.'</dt>';
        echo '<dd>'.$msg->body.'</dd>';
        echo '<dd>'.date('r', $msg->timestamp)." ({$msg->date})".'</dd>';
        echo '<dd><a href="'.$msg->link.'">'.$msg->link.'</a></dd>';
    }
    echo '</dl>';
}


Vasso 1925 - days ago 
I have 72 new message, but get with GmAtom only 20... I think it is Atom limit.
How can I change this limit?
Post a comment

Your Name or E-mail ID (mandatory)

Note: Your comment will be published after approval of the owner.





 RSS of this page