sj26 / mailcatcher

Catches mail and serves it through a dream.
http://mailcatcher.me
MIT License
6.3k stars 575 forks source link

API response returns tab character in subject #481

Closed CristinaHP closed 3 years ago

CristinaHP commented 3 years ago

I have a problem when I call the api. I'm using .NET Mimekit library and a mailcatcher:latest docker image. The thing is I need to get the subject but when making a request to "/messages/:id.json" it comes with a tab character in it. And after deserializing it with Newtonsoft it's still there. BC5Oq8pVKC

I think it's because of how mailcatcher displays de subject in the UI leaving a tab at the start of each line of a header when it's folded. Epscf10Yu8

I've also build a new mailcatcher docker image using ruby.mail version 2.7.1 but still happens. I would like for that tab character not to be there after unfolding the header.

sj26 commented 3 years ago

Thanks for the report!

Could you please provide an email that replicates the issue when delivered?

CristinaHP commented 3 years ago
From: user@domain.com
Subject: This is the subject of an email testing the tab character in the
    subject when is too long and has to be folded
To: user@domain.com
MIME-Version: 1.0
Content-Type: text/plain;
    charset="utf-8"

This is the body.

Not sure if you need more than that.

sj26 commented 3 years ago

Yeah that seems to replicate, thanks.

220 EventMachine SMTP Server
EHLO domain
250-Ok EventMachine SMTP Server
250-NO-SOLICITING
250 SIZE 20000000
MAIL FROM: user@domain.com
250 Ok
RCPT TO: user@domain.com
250 Ok
DATA
354 Send it
From: user@domain.com
Subject: This is the subject of an email testing the tab character in the
    subject when is too long and has to be folded
To: user@domain.com
MIME-Version: 1.0
Content-Type: text/plain;
    charset="utf-8"

This is the body.

.
250 Message accepted
[{
  "id": 1,
  "sender": "user@domain.com",
  "recipients": ["user@domain.com"],
  "subject": "This is the subject of an email testing the tab character in the\tsubject when is too long and has to be folded",
  "size": "254",
  "created_at": "2021-08-04T11:26:31+00:00"
}]

RFC5322 describes long header treatment in section 2.2.3. Headers are unfolded by removing any CRLF in a header value, but preserves the prefixed whitespace as-is. It seems like both the smtp and mail parsers are behaving to specification here, and mail clients will do the same thing. The API is returning the subject as parsed. I don't think this is a bug, sorry.

CristinaHP commented 3 years ago

But when I create the message I´m not inserting any tab character in the subject. So I guess that tab character shouldn't be there in the first place. And if I send that message to a gmail or hotmail account, in their UIs its folded form is displayed without a tab character but with a CRLF and a space character, like this:

Subject: This is a long
 subject

So when I get its unfolded form, the CRLF is removed just like RFC5322 specifies, leaving the space character that separates the words "long" and "subject". So everything fine there. That's why I opened this issue, because the problem happens only with mailcatcher so far.

sj26 commented 3 years ago

How are you sending mail to mailcatcher? Whatever library or client you are using must be folding your subject and inserting tabs?

I’d suggest squishing white space and/or replacing tabs with spaces is a presentational concern. Wherever you’re presenting the subject might need to be able to do this, for compatibility with the spec?

sj26 commented 3 years ago

Sorry, I think mailcatcher is behaving correctly and according to spec here.

CristinaHP commented 3 years ago

Yeah sorry. I tried looking up at the email to see if the library I'm using is inserting the tab character before sending it, but found nothing. The subject is fine before being sent, so I don't really know who or what is doing that.

Either way, thank you for the quick replies.