I would like to know if it's possible to read an email (and mark it as read) in a Gmail account. If yes, does anyone have an example for this? I would like to achieve this using php and curl (which will be ran locally or on a server). I asked G(oogle) but no real answers.
If it helps, you can pick apart the class I created earlier for something else - I'm sure it can't be the best example since it was only started a few hours ago and I'd never done anything of this sort before. But it works for me getting emails from my mailbox. Usage should be obvious enough, constants need defining (IMAP_HOST,IMAP_PORT,USERNAME,PASSWORD) with your login details. Create a new object and it connects to your mailbox. getMsgList() fills the $msgList with a multi-dimensional array containing all messages info formatted to make sense. Once you have the ID from that, you can use getMsg($id) to fetch a single message. <?php class readMail { private $conn; public $msgList = array(); public function __construct() { // Connect to our mailbox $this->conn = imap_open('{'.IMAP_HOST.':'.IMAP_PORT.'}INBOX', USERNAME, PASSWORD) or die('Unable to open stream'); } public function getMsgList() { // Get list of messages $headers = imap_headers($this->conn); if ($headers == false) { // Mailbox empty return array(); } else { // Parse returned data foreach ( $headers as $row ) { if (! preg_match('/^ ([A-Z])\s+([0-9]+)\) ([0-9]{1,2}-[A-Z][a-z]+-[0-9]{4}) (.*)\s{2,}(.*) \(([0-9]+) chars\)$/i',$row,$matches) ) return; $this->msgList[] = array( 'status' => $matches[1], 'number' => $matches[2], 'date' => $matches[3], 'from' => trim($matches[4]), 'subject' => trim($matches[5]), 'length' => $matches[6] ); } } } public function getMsg($id,$mark=true) { return $mark ? imap_body($this->conn,$id) : imap_body($this->conn,$id,FT_PEEK); } public function delete($id) { return imap_delete($this->conn,$id); } public function __destruct() { imap_expunge($this->conn); imap_close($this->conn); $this->conn = false; } } PHP:
reading gmail emails with imap_xxx functions can be a complex task, especially on attachments. search for pear/php classes maybe you'll find something useful