The case of missing message ID
Azanul Haque
Posted on November 4, 2022
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!].
What needed to be done
- Add
message-id
to all outgoing email as specified in Identification Fields:
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.
How I did it
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).date-time
: Used Go's time package to generate timestamp from current time object.host_name
: There is aSMTPConfig
object which has all the SMTP config including host name.
So, joining all these usingfmt.Sprintf
:
msgID := fmt.Sprintf("<%s-%d@%s>", model.NewRandomString(randomStringLength), time.Now().Unix(), config.Hostname)
- 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 amessage-id
.
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.
References
Posted on November 4, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.