vitalyp
vitalyp

Reputation: 691

Do emails have to have a messageid?

I'm retrieving emails from Gmail using PHP and IMAP; however, some emails don't have a messageid. Aren't all messages supposed to have a messageid?

I need a unique id for reference so i'm not sure how else to keep track of emails without it.

Am I doing something wrong? For example here is an email header I get

 [date] => Sun, 06 Nov 2011 21:21:56 -0500
    [subject] => Daylight Saving Time?  Chili's Saving Time!
    [to] => [email protected]
    [message_id] => 
    [from] => [email protected]
    [sender] => [email protected]
    [reply_toaddress] => [email protected]
    [size] => 14385
    [msgno] =>  156
    [status] => Unread

Upvotes: 13

Views: 13538

Answers (3)

Headbank
Headbank

Reputation: 400

Other answers have addressed the matter of Message-ID being sometimes absent, but I would just note that even in an ideal world of well-behaved MTAs, it would still not suffice as a unique identifier of messages within a given IMAP server.

Why? Because you may receive the "same" message twice. Within a single server, or mailbox within that server, there may be multiple recipient addresses for which mail is received. For example, the same IMAP mailbox might receive email addressed to both [email protected] and [email protected], and somebody might send a message whose To:, CC: and/or BCC: fields include more than one of them (the sender often won't know that they end up in the same place). That results in two messages being sent with the same Message-ID, and landing side-by-side in Fred's mailbox.

Even in your particular case of a GMail account (which likely means just a single receiving address), there's nothing to prevent a sender placing that one address in both To and CC, and unless their mailer is very smart and opinionated, the outcome is still you receiving two messages with the same Message-ID.

Within the IMAP context the UID is a more reliable unique ID, but of course it is folder-local so if your target message may move between different folders, then it's no help. In some circumstances the IMAP server may even reindex all the messages in a folder.

To truly uniquely fingerprint any given message, you likely need to use a combination of several different header values, which of course will make it progressively more resource-intensive to search for.

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270599

Any MTA I have ever encountered will add a Message-ID if one is not already present. However, if you need to keep track of messages or thread them, you will need to set a the Message-ID. The References header and the In-Reply-To header use the value of a previous Message-ID to relate messages together.

References contains a list of previous Message-ID values in the reply chain, and In-Reply-To contains the Message-ID to which the current message is a direct reply.

Note that according to the RFC-2822 specification, a Message-ID is technically not required. Well-behaved MTAs generally include one, but some commenters below describe instances where a Message ID was not present, causing failures in messaging clients.

Upvotes: 11

ChrisM
ChrisM

Reputation: 1124

The message ID has nothing to do with IMAP but is part of the mail itself and specified in RFC 2822 as "optional" (although it says that it should be present):

Though optional, every message SHOULD have a "Message-ID:" field.

So you are not doing anything wrong if some mails lack a Message-ID. It happens for all the mails that the MUA that originally sent the mail did not generate one for (which however every commonly used MUA should do).

Concerning a unique ID for identifying mails via IMAP, you may want to have a look at the UID field described in the standard.

Upvotes: 7

Related Questions