Reading Gmail emails with curl possible?

Discussion in 'PHP' started by Choller, May 9, 2007.

  1. #1
    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.
     
    Choller, May 9, 2007 IP
  2. fouadz

    fouadz Peon

    Messages:
    132
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Gmail provide a POP3 interface I think.
    It will be more easy to use that way.


    good luck
     
    fouadz, May 9, 2007 IP
    commandos likes this.
  3. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    well i want to let a script acces it, i don't want to read it myself.
     
    Choller, May 9, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    nico_swd, May 9, 2007 IP
  5. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for that gonna read up a bit.

    Any other sources/example are welcome.
     
    Choller, May 9, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    nico_swd, May 9, 2007 IP
  7. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    rodney88, May 9, 2007 IP
    commandos likes this.
  8. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #8
    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
     
    gibex, May 10, 2007 IP
  9. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I usually enable POP3 on the account and use Net_POP3
     
    CodyRo, May 11, 2007 IP