accessing my gmail inbox via php code

user156073 picture user156073 · Sep 6, 2009 · Viewed 35.7k times · Source

how i can access my gmail account through my php code? I need to get the subject and the from address to from my gmail account.And then i need to mark the accessed as read on gmail Should i use gmail pop3 clint?is that any framework that i can use for accessing gmail pop3 server.

Answer

Jerry picture Jerry · Sep 6, 2009

I would just use the PHP imap functions and do something like this:

<?php
    $mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "[email protected]", "PASSWORD");
    $mail = imap_search($mailbox, "ALL");
    $mail_headers = imap_headerinfo($mailbox, $mail[0]);
    $subject = $mail_headers->subject;
    $from = $mail_headers->fromaddress;
    imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
    imap_close($mailbox);
?>

This connects to imap.googlemail.com (googlemail's imap server), sets $subject to the subject of the first message and $from to the from address of the first message. Then, it marks this message as read. (It's untested, but it should work :S)