JohnDotOwl
JohnDotOwl

Reputation: 3755

Storing Email from IMAP(Unique Email ID)?

How do i store a UNIQUE ID from that email in my database.

I tried $overview = imap_fetch_overview($inbox,$email_number,0); and i received a bunch of numbers but the problem is the number will change when one of the email is deleted.

How do I store it properly? MD5 the message or something?

Basically i'm trying to receive email on my personal web application where i can manage and access my own email. It calls gmail using imap.

Anyway where i can check and store only new emails?

/* connect to gmail */
        $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
        $username = '[email protected]';
        $password = '';

        /* try to connect */
        $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

        /* grab emails */
        $emails = imap_search($inbox,'ALL');
        dd($emails);
        /* if emails are returned, cycle through each... */
        if($emails) {

            /* begin output var */
            $output = '';

            /* put the newest emails on top */
            rsort($emails);

            /* for every email... */
            foreach($emails as $email_number) {

                /* get information specific to this email */
                $overview = imap_fetch_overview($inbox,$email_number,0);
                $message = imap_fetchbody($inbox,$email_number,2);

                /* output the email header information */
                $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 the email body */
                $output.= '<div class="body">'.$message.'</div>';
            }

            echo $output;
        } 

        /* close the connection */
        imap_close($inbox);

Upvotes: 1

Views: 451

Answers (1)

Nameless One
Nameless One

Reputation: 1646

The UID exists for this purpose, it should remain unchanged unless the server gives a different UIDVALIDITY value.

https://www.rfc-editor.org/rfc/rfc3501#section-2.3.1.1

The UID is part of the IMAP standard so it should be correctly implemented by all IMAP servers. If you are only working against gmail, then you might want to look into another value though, since the same message can be seen under different labels, and then the UID might be different for that logically identical message. I don't know the gmail API though.

Upvotes: 2

Related Questions