Get Gmail labels via PHP IMAP?
Asked Answered
U

1

7

I am accessing Gmail messages via IMAP using PHP. I'd like to know what label(s) each message is tagged with. Apparently Google has an IMAP extension that will do exactly what I need:

https://developers.google.com/google-apps/gmail/imap_extensions#access_to_gmail_labels_x-gm-labels

However, I'm not sure how to use this extension via PHP. There are PHP IMAP functions for fetch_header, etc., but I don't see a raw "fetch" that would let me grab this extension information. Any advice?

Upraise answered 21/2, 2013 at 2:52 Comment(3)
you just get the folder nameIzaak
Right but Gmail allows you to "tag" messages with multiple labels (folders). I'd like a list of every label associated with a particular message. I could grab each message from each folder of interest but that's a little cumbersome as I'd be getting some messages more than once (I think) and would then have to merge them somehow. I think the extension will give me what I need, I'm just not sure how to access it via PHP.Upraise
This is implemented here github.com/anod/gmail-imap-php/blob/master/src/Anod/Gmail/…Relief
A
-1

Try this one

define('EMAIL_HOSTNAME', '{imap.gmail.com:993/imap/ssl}INBOX');
define('EMAIL_USERNAME', '[email protected]');
define('EMAIL_PASSWORD', 'password');


$inbox = imap_open(EMAIL_HOSTNAME,EMAIL_USERNAME,EMAIL_PASSWORD) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
if($emails) {

    rsort($emails);
    foreach($emails as $email_number) {
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);


        $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<span class="from">'.$overview[0]->from.'</span>';
        $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';


        $output.= '<div class="body">'.$message.'</div>';
    }

}
imap_close($inbox);
Alpinist answered 4/5, 2014 at 14:6 Comment(1)
This does not answer the question.Relief

© 2022 - 2024 — McMap. All rights reserved.