# 每封电子邮件都应具有唯一的 Message-ID

**URL:** https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746
**Category:** Bug
**Tags:** email
**Created:** [2018年七月19日 19:02 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746 "2018-07-19T19:02:05Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [2018年七月19日 19:02 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/1 "2018-07-19T19:02:05Z")

</div>

Every single email that Discourse generates [must have a unique Message-ID](https://tools.ietf.org/html/rfc2822#section-3.6.4).

The current IDs we generate are not unique - the same Message-ID will get used for every distinct email going out for a particular post (i.e. if it’s sent to 10 users, that Message-ID gets repeated 10 times):

```ruby
            if post.post_number == 1
              "<topic/#{topic_id}@#{host}>"
            else
              "<topic/#{topic_id}/#{post.id}@#{host}>"
            end

```

⬇

```plaintext
Message-ID: <topic/3981/53574@meta.discourse.org>

```

We should append some randomness onto the Message ID (which we can obviously remove when parsing incoming email).

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2018年七月19日 19:26 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/2 "2018-07-19T19:26:52Z")

</div>

Does that cause any issues? I mean the message id is unique per “message”.

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [2018年七月19日 19:30 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/3 "2018-07-19T19:30:58Z")

</div>

I _suspect_ it may be causing problems but am not entirely sure it is. 99.9% of the time things will be fine, but I know it _would_ have caused problems at companies I’ve worked at previously (e.g. email scanning gateways may reject duplicate Message-IDs with different content).

Note the differing content comes from the post getting emailed to different people.

---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [2018年七月19日 19:33 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/4 "2018-07-19T19:33:30Z")

</div>

> [@supermathie](#):
>
> email scanning gateways may reject duplicate Message-IDs with different content

This may explain an email issue I’ve been looking into.

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [2018年七月19日 21:52 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/5 "2018-07-19T21:52:55Z")

</div>

> [@zogstrip](#):
>
> the message id is unique per “message”.

No, it’s unique per Discourse post. The “message” in an e-mail message ID is the e-mail.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2018年七月19日 22:19 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/6 "2018-07-19T22:19:07Z")

</div>

I know, I was half joking / half trolling 😉

I didn’t know (think) about the email scanning gateway issue. I will get it fixed 👌

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2018年七月19日 22:52 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/8 "2018-07-19T22:52:15Z")

</div>

I suggest appending the target user ID in the identifier, so the identifier actually makes sense versus “here’s a random string!”

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [2018年七月20日 20:20 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/9 "2018-07-20T20:20:27Z")

</div>

> [@codinghorror](#):
>
> so the identifier actually makes sense versus “here’s a random string!”

It really doesn’t need to make sense - “random string” is VERY typical.

If you want it to, I would do both - add the target user.id as well as random data (who knows - perhaps it’s possible now or in the future for the same user to be emailed about a post twice).

---

<div class="post-metadata">

### Author: ![dan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dan/32/101549_2.png) [@dan](https://meta.discourse.org/u/dan)
#### Post date: [2018年七月21日 18:26 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/10 "2018-07-21T18:26:39Z")

</div>

I am not exactly sure what’s up with the secondary emails feature, but if all of them are used to notify the user your approach may cause issues. If a user has multiple email addresses then it is going to receive multiple emails with the same Message-ID.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2018年七月21日 19:06 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/11 "2018-07-21T19:06:59Z")

</div>

I think they’re only used for login, not notifications.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2022年三月1日 07:30 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/12 "2022-03-01T07:30:52Z")

</div>

我觉得 @martin 最近的工作已经很好地解决了这个问题？

例如：

```plaintext
Message ID	<topic/219646.66ed0fe487b002b5c60c3a6b@meta.discourse.org>

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2022年三月1日 07:30 UTC](https://meta.discourse.org/t/every-email-should-have-a-unique-message-id/92746/13 "2022-03-01T07:30:54Z")

</div>


