The case of missing message ID
Tags: opensource, hacktoberfest, mattermost, community
Published: 2022-11-04 16:22:37

## 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](https://datatracker.ietf.org/doc/html/rfc5322) specification, is optional but still should be present [mixed signals!].

![Section Divider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxnpclaf95g9spgn4ehm.png)

## What needed to be done
- Add `message-id` to all outgoing email as specified in [Identification Fields](https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.4):

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25lmj5x2g9ibf54ptb2x.png)

## 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](https://pkg.go.dev/time) to [generate timestamp](https://pkg.go.dev/time#Time.Unix) from [current time object](https://pkg.go.dev/time#Now).

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)
```

4. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxnpclaf95g9spgn4ehm.png)

## Next step
- Have any questions/suggestions? leave a comment.
- Never used mattermost? [Give it a go](https://mattermost.com/).
- Want to contribute like I did? [Here's the repo](https://github.com/mattermost/mattermost-server).

![Section Divider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25lmj5x2g9ibf54ptb2x.png)

## References
- Issue: [Still having MTA for Googlemail with 7.2.0 #20828](https://github.com/mattermost/mattermost-server/issues/20828)
- Pull-request: [Mm 46416 add default random message #21176
](https://github.com/mattermost/mattermost-server/pull/21176)

![Footer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ll9sic32gug22pv1opj4.png)