<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kerkness.ca &#187; gmail</title>
	<atom:link href="http://www.kerkness.ca/tagged/gmail/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerkness.ca</link>
	<description>flexing my kerkness, among other things</description>
	<lastBuildDate>Wed, 14 Oct 2009 15:12:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GmailReader.php : A PHP IMAP Class for Reading your Gmail Account</title>
		<link>http://www.kerkness.ca/gmailreaderphp-a-php-imap-class-for-reading-your-gmail-account/</link>
		<comments>http://www.kerkness.ca/gmailreaderphp-a-php-imap-class-for-reading-your-gmail-account/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 01:09:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[gmail]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=54</guid>
		<description><![CDATA[One of the requirements I had for a recent project involved keeping track of email communication from multiple Gmail users. My first thought was to simply forward a copy of all incoming and outgoing email to an email account on my server and accessing the email that way.  There was one problem with this [...]]]></description>
			<content:encoded><![CDATA[<p>One of the requirements I had for a recent project involved keeping track of email communication from multiple Gmail users. My first thought was to simply forward a copy of all incoming and outgoing email to an email account on my server and accessing the email that way.  There was one problem with this approach.  Gmail only allows you to forward a copy of incoming email somewhere else.  Emails composed in Gmail stay in Gmail.</p>
<p>My solution:  Use Gmail&#8217;s IMAP capabilities and monitor the accounts using <a href="http://ca3.php.net/manual/en/ref.imap.php">PHP-IMAP</a>.  This turned out to be a fairly easy to implement solution but I thought I would post my GmailReader class to save other people some initial headaches of getting things rolling.</p>
<p>First of all you&#8217;ll need to enable IMAP for you Gmail Account.  Log into Gmail and goto Settings &gt; Forwarding and POP/IMAP</p>
<p>Next you&#8217;ll need to make sure php is compiled with IMAP enabled.  This can be done on ubuntu with the following command and then restart apache for good measure.</p>
<pre>sudo apt-get install php5-imap
sudo /etc/init.d/apache2 restart</pre>
<p>In my particular scenario I run a script every so often to look for new emails since the last time I checked. So the class file is structured to check for new emails since a specific date string.</p>
<p><span style="font-size:130%;"><span style="font-weight: bold;">Usage Example</span></span></p>
<pre>include_once( 'GmailReader.php' );
$gmail = new GmailReader( 'username@gmail.com', 'password' );
$email = $gmail-&gt;getEmailSince('Fri, 5 Sep 2008 9:00:00');

print_r($email);</pre>
<p>The above example will print out an array of all emails ( still in the Inbox ) which arrived no earlier than 9am on the 5th of September.</p>
<p>You can also check your sent mail using the following example.</p>
<pre>$gmail = new GmailReader( 'username@gmail.com', 'password' );
$gmail-&gt;openSentMail();
$email = $gmail-&gt;getEmailSince('Fri, 5 Sep 2008 9:00:00');</pre>
<p>Also, if you use Gmail to apply labels to certain email you can monitor just these emails as well.</p>
<pre>$gmail = new GmailReader( 'username@gmail.com', 'password' );
$gmail-&gt;openMailBox( 'MYLABEL' );
$email = $gmail-&gt;getEmailSince('Fri, 5 Sep 2008 9:00:00');</pre>
<p><span style="font-size:130%;"><span style="font-weight: bold;">Here is the full GmailReader Class File.</span></span></p>
<pre name="code" class="php">class GmailReader
{
var $mbox;

function GmailReader( $user, $pass )
{
 $this-&gt;mbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX",$user,$pass)
  or die("can't connect: " . imap_last_error());
}

function openSentMail()
{
 imap_reopen($this-&gt;mbox, "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail" )
  or die("Failed to open Sent Mail: " . imap_last_error());
}

function openMailBox($mailbox)
{
 imap_reopen($this-&gt;mbox, "{imap.gmail.com:993/imap/ssl}$mailbox" )
  or die("Failed to open $mailbox: " . imap_last_error());
}

function getMailboxInfo()
{
 $mc = imap_check($this-&gt;mbox);
 return $mc;
}

/**
 * $date should be a string
 * Example Formats Include:
 * Fri, 5 Sep 2008 9:00:00
 * Fri, 5 Sep 2008
 * 5 Sep 2008
 * I am sure other's work, just test them out.
 */
function getHeadersSince($date)
{
 $uids = $this-&gt;getMessageIdsSinceDate($date);
 $messages = array();
 foreach( $uids as $k=&gt;$uid )
 {
  $messages[] = $this-&gt;retrieve_header($uid);
 }
 return $messages;
}

/**
 * $date should be a string
 * Example Formats Include:
 * Fri, 5 Sep 2008 9:00:00
 * Fri, 5 Sep 2008
 * 5 Sep 2008
 * I am sure other's work, just test them out.
 */
function getEmailSince($date)
{
 $uids = $this-&gt;getMessageIdsSinceDate($date);
 $messages = array();
 foreach( $uids as $k=&gt;$uid )
 {
  $messages[] = $this-&gt;retrieve_message($uid);
 }
 return $messages;
}

function getMessageIdsSinceDate($date)
{
 return imap_search( $this-&gt;mbox, 'SINCE "'.$date.'"');
}

function retrieve_header($messageid)
{
   $message = array();

   $header = imap_header($this-&gt;mbox, $messageid);
   $structure = imap_fetchstructure($this-&gt;mbox, $messageid);

   $message['subject'] = $header-&gt;subject;
   $message['fromaddress'] =   $header-&gt;fromaddress;
   $message['toaddress'] =   $header-&gt;toaddress;
   $message['ccaddress'] =   $header-&gt;ccaddress;
   $message['date'] =   $header-&gt;date;

   return $message;
}

function retrieve_message($messageid)
{
   $message = array();

   $header = imap_header($this-&gt;mbox, $messageid);
   $structure = imap_fetchstructure($this-&gt;mbox, $messageid);

   $message['subject'] = $header-&gt;subject;
   $message['fromaddress'] =   $header-&gt;fromaddress;
   $message['toaddress'] =   $header-&gt;toaddress;
   $message['ccaddress'] =   $header-&gt;ccaddress;
   $message['date'] =   $header-&gt;date;

  if ($this-&gt;check_type($structure))
  {
   $message['body'] = imap_fetchbody($this-&gt;mbox,$messageid,"1"); ## GET THE BODY OF MULTI-PART MESSAGE
   if(!$message['body']) {$message['body'] = '[NO TEXT ENTERED INTO THE MESSAGE]nn';}
  }
  else
  {
   $message['body'] = imap_body($this-&gt;mbox, $messageid);
   if(!$message['body']) {$message['body'] = '[NO TEXT ENTERED INTO THE MESSAGE]nn';}
  }

  return $message;
}

function check_type($structure) ## CHECK THE TYPE
{
  if($structure-&gt;type == 1)
    {
     return(true); ## YES THIS IS A MULTI-PART MESSAGE
    }
 else
    {
     return(false); ## NO THIS IS NOT A MULTI-PART MESSAGE
    }
}

}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/gmailreaderphp-a-php-imap-class-for-reading-your-gmail-account/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
