1

I'm moving emails to another inbox using code that looks a lot like this:

function move_email($stream, $uid, $destination) {
    $result = imap_mail_move($stream, $uid, $destination, CP_UID);
    if($result) {
        imap_expunge($stream);
    }
    return $result;

Works great! But I'd like to be able to retrieve that particular email in the future.

My understanding is that the UID in the new box may be/will be different from the UID passed into the function. How do I retrieve the UID of the email from the mailbox specified in $destination?

And actually, how do I fetch an email by UID at all? imap_search() has a promising name, but it doesn't seem to be capable of returning an email by UID.

jeremiahs
  • 3,985
  • 8
  • 25
  • 30
  • Umm, unless the server supports UIDPLUS, you can't know the destination UID. But I don't know the PHP functions. You also want *fetch* to get messages by UID. – Max Jan 31 '14 at 01:33

1 Answers1

1

You are right in assuming that the UID in the new folder will be different. The UID for a message is unique only in that folder.

When you move the message to the new folder, you can probably assume that it will be the last one in that folder (unless some new message happens to be received at the same time, but let's assume this doesn't happen). Given that, you can use the following IMAP command to get the latest message:

UID FETCH * BODY[]

The * in IMAP means the last UID (unintuitively, since we commonly use it as a wildcard in other scenarios). In PHP, you probably want to use one of the imap_fetch functions, and pass in that "*" as the $sequence.

Note: I'm not sure if the * will actually work in PHP, since I'm reading the description for $sequence in imap_fetch_overview() and it says:

A message sequence description. You can enumerate desired messages with the X,Y syntax, or retrieve all messages within an interval with the X:Y syntax

No mention of *, so you'll just have to try it and see if it works. It would be a quite a limitation of PHP if it didn't allow you to use the full flexibility of sequence sets afforded by IMAP.

Gigi
  • 28,163
  • 29
  • 106
  • 188