The case of missing message ID

Azanul HaqueAzanul Haque
2 min read

Intro

This article assumes that you know how modern email works. Even if you don't, you'll get what the issue was and you also might get some idea by the end of this article. The title might seem unrelated but the issue occured when google MTAs stopped putting message Emails sent by Mattermost (An opensource Slack alternative) were being marked as spam by many spam filters. This was happening because the emails were missing message-id which according to the Internet Message Format specification, is optional but still should be present [mixed signals!].

Section Divider

What needed to be done

a good method is to put the domain name (or a domain literal IP address) of the host on which the message identifier was created on the right-hand side of the "@" (since domain names and IP addresses are normally unique), and put a combination of the current absolute date and time along with some other currently unique (perhaps sequential) identifier available on the system (for example, a process id number) on the left-hand side.

simply, the format was going to be: <unique_string-date_time@host_name\>

  • Write unit tests to verify that a message-id is always present.

Section Divider

How I did it

  1. unique_string: A function was already written to generate a random string, so that's the one that I used. Although, a random string is not guaranteed to be unique as the same string can be generated again (the probability of repetition of a 16 char random string which has all 10 digits is ideally once out of 10^16).

  2. date-time: Used Go's time package to generate timestamp from current time object.

  3. host_name: There is a SMTPConfig object which has all the SMTP config including host name. So, joining all these using fmt.Sprintf:

msgID := fmt.Sprintf("<%s-%d@%s>", model.NewRandomString(randomStringLength), time.Now().Unix(), config.Hostname)
  1. Test case: The was a test case which ensured that emails without message-id are allowed. I just edited it to validate if every email has a message-id.

Section Divider

Next step

  • Have any questions/suggestions? leave a comment.

  • Never used mattermost? Give it a go.

  • Want to contribute like I did? Here's the repo.

Section Divider

References

Footer

1
Subscribe to my newsletter

Read articles from Azanul Haque directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Azanul Haque
Azanul Haque