IMAP : PHP : How can I get and store message numbe

2019-06-08 03:33发布

问题:

I have to read my server mail inbox using IMAP, then I have to show some mails (mails with predefined contents) in the site for administrator, Later the admin can delete or shift mails from the admin panel, but how can delete or shift a particular mail from the admin panel ? Is there any way to store the message id in the mail ? How can I get the id of a particular mail in the inbox ?

Thanks a lot for your time

回答1:

There's no such thing as a store message number in IMAP, so I assume you used that term colloquially for any number that uniquely identifies a message. If I'm correct with that assumption, you're out of luck: there's no such number either, at least not across IMAP sessions.

Here's what you can try: the IMAP protocol requires that each message has a unique identifier and each mailbox has a unique identifier validity value. The combination of a unique identifier and the unique identifier validity value of the message's mailbox is guaranteed to never refer to different messages. So if you use that to identify messages, you are at least guaranteed not to accidentally delete the wrong message.

The problem is, the unique identifier validity value is guaranteed to stay the same only for the duration of the IMAP session. PHP usually creates a new IMAP session with every page request and closes the session at the end of the request. On the other hand, the IMAP specification suggests, that unique identifiers stay the same even across sessions. This makes the following approach seem practical:

  1. Identify messages by unique identifier (e.g. by using them as value-attributes on checkboxes that are used to select messages.).
  2. Store the unique identifier validity value of the mailbox as a hidden field in the form.
  3. When processing form submissions, check whether the unique identifier validity value submitted via the form is the same as the current one.
  4. Beware: there are IMAP servers, that use a different unique identifier validity value for every session. You won't be able to do anything useful if you happen to have such an implementation.

You can get the current unique identifier validity value by calling imap_mailboxmsginfo. Most other functions from PHP's IMAP extension have a parameter $options, that you can use specify that you are using the unique identifier instead of the message sequence number (an other means to identify messages, that is even more volatile than the unique identifier).

See RFC 3501 for details about the IMAP protocol, especially 2.3.1 Message Numbers.



标签: php imap