imap_uid

(PHP 3 >= 3.0.3, PHP 4, PHP 5)

imap_uid --  This function returns the UID for the given message sequence number

Description

int imap_uid ( resource imap_stream, int msgno )

This function returns the UID for the given message sequence number. An UID is an unique identifier that will not change over time while a message sequence number may change whenever the content of the mailbox changes. This function is the inverse of imap_msgno().

注: This is not supported by POP3 mailboxes.

See also: imap_msgno().


add a note add a note User Contributed Notes
akb at akb dot me dot uk
29-Jan-2005 08:40
Another way of getting a unique id for a message in a pop3 box.
This will not be to everybodies tastes, but hey, it works for me ;-)

<?php
$header
= imap_headerinfo($mbox, $nMsgIndex);
$uid = md5(print_r($header,true));
?>
thomas at bytefreak dot de
04-Nov-2002 03:22
RFC1939 defines CRLF for a newline.

you should use code like this:

fputs($fp, "USER ".$account."\r\n");
$buf = fgets($fp, 1024);

fputs($fp, "PASS ".$password."\r\n");
$buf = fgets($fp, 1024);

fputs($fp, "UIDL ".$message_number."\r\n");
$retval=fgets($fp, 1024);

fputs($fp, "QUIT\r\n");

thomas
steve_foley at excite dot com
05-Sep-2002 01:27
Please note that message_id in the header is NOT the UID - and its dangerous to use as many mail progs don't put a message id in
robert dot blanch at hsc dot utah dot edu
17-Jul-2002 10:25
Another way to get the UID without using imap_uid that works with pop3:

$header = imap_headerinfo( $mbox, $msgno );
$UID = $header->message_id;
21-Jun-2002 04:53
For those using a POP3 mailbox, this function, as noted, does not work.

One way around this is to use direct FTP communication with the mail server.  For example, the following function takes a mailbox/password and message sequence number, and from this returns the message UID.

function fetch_UID($account, $password, $message_number)
{
   $retval = 0;
   $fp = fsockopen($POPMAILSERVER, 110);
   if ($fp > 0)
   {
       $buf = fgets($fp, 1024);

       fputs($fp, "USER ".$account."\n");
       $buf = fgets($fp, 1024);

       fputs($fp, "PASS ".$password."\n");
       $buf = fgets($fp, 1024);

       fputs($fp, "UIDL ".$message_number."\n");
       $retval=fgets($fp, 1024);

       fputs($fp, "QUIT\n");
       $buf = fgets($fp,1024);

       fclose($fp);
   }
   return substr($retval,6,30);
}

Note: 110 is the TCP port commonly associated with a POP3 mailbox.  Also the substr() function is used to trim the +OK and the message number from the beginning of the server's response.

I'm sure there are other (better) ways to do this.  But after a fruitless search of the internet, this is what I came up with, and it seems to work for me.