Email notifications fail if duplicate headers exist

(Disclaimer: not a ruby programmer)

I think this is particularly nasty behaviour in the mail library that discourse is using. Here’s the header_value function:

As far as I can tell, @message.header[name] is calling this method:

https://www.rubydoc.info/github/mikel/mail/Mail%2FHeader:[]

As per RFC, many fields can appear more than once, we will return a string of the value if there is only one header, or if there is more than one matching header, will return an array of values in order that they appear in the header ordered from top to bottom.

Discourse automatically sets a Precedence header, so because you’re adding one as well via the email_custom_headers setting, there are now two Precedence headers, and @message.header["Precedence"] is returning an array instead of a string.

I think this bug will be triggered any time email_custom_headers contains a header that already exists on the message object.

5 Likes