storj/private/post/message_test.go
Márton Elek 2ae78db660 satellite/email: add delimiter to close the last part of Multipart emails
The existing implementation doesn't send proper rfc1341 compatible mails
according to https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html as
the closing `--boundary_id--` is missing from our mails. (see 7.2.1 from
the standard).

Mailservers which are not very flexible, deny the mails.

Example mail log: (see the
missing boundary delimiter at the end).

```
Subject: Activate your email
From: "Storj DCS - EU1" <noreply@eu1.storj.io>
To: "..." <...>
...

--26d7220f6f1c9f6fb47b535319eb15dce513bb6e1d941a0efddf25e96712
Content-Type: text/html; charset=UTF-8

....

</body>
</html>
.
smtp: DATA error	{"msg_id":"6d82c9e3","reason":"unexpected EOF"}
smtp: 554 5.0.0 Internal server error (msg ID = 6d82c9e3)
```

This patch moves all the Multipart writing to a function to make sure
that the `wr.Close()` (which writes out the last part) is executed
BEFORE `body.Bytes()` (defer added it AFTER `body.Bytes()` was calculated)

Change-Id: I8f18fc81b1857b646470eab32e73d6cbdc50d2ad
2022-02-01 09:55:29 +00:00

40 lines
1.0 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information
package post
import (
"net/mail"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestMessage_ClosingLastPart(t *testing.T) {
from := mail.Address{Name: "No reply", Address: "noreply@eu1.storj.io"}
m := &Message{
From: from,
To: []mail.Address{{Name: "Foo Bar", Address: "foo@storj.io"}},
Subject: "This is a proper test mail",
PlainText: "",
Parts: []Part{
{
Type: "text/html; charset=UTF-8",
Content: string("<head><body><h1>ahoj</h1></body></head>"),
},
},
}
data, err := m.Bytes()
require.NoError(t, err)
lines := strings.Split(string(data), "\n")
// last part should be closed. see 7.2.1 of https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
final := regexp.MustCompile("--.+--")
lastNonEmptyLine := lines[len(lines)-2]
require.True(t, final.MatchString(lastNonEmptyLine), "Last line '%s' doesn't include RFC1341 distinguished delimiter", lastNonEmptyLine)
}