[PATCH v2] Add utility function for generating Message-Id
Export this patch
---
go.mod | 1 +
go.sum | 7 +++++++
mail/mail.go | 31 +++++++++++++++++++++++++++++++
mail/mail_test.go | 15 +++++++++++++++
4 files changed, 54 insertions(+)
create mode 100644 go.sum
diff --git a/go.mod b/go.mod
index c4b1a3e..d077adc 100644
--- a/go.mod
+++ b/go.mod
@@ -4,5 +4,6 @@ go 1.12
require (
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe
+ github.com/martinlindhe/base36 v0.0.0-20190418230009-7c6542dfbb41
golang.org/x/text v0.3.2
)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..93c2bb9
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,7 @@
+github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe h1:40SWqY0zE3qCi6ZrtTf5OUdNm5lDnGnjRSq9GgmeTrg=
+github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
+github.com/martinlindhe/base36 v0.0.0-20190418230009-7c6542dfbb41 h1:CVsnY46BCLkX9XOhALJ/S7yb9ayc4eqjXSXO3tyB66A=
+github.com/martinlindhe/base36 v0.0.0-20190418230009-7c6542dfbb41/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8=
+golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
+golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
diff --git a/mail/mail.go b/mail/mail.go
index 2f9a12c..bf10a16 100644
--- a/mail/mail.go
+++ b/mail/mail.go
@@ -7,3 +7,34 @@
//
// RFC 5322 defines the Internet Message Format.
package mail
+
+import (
+ "bytes"
+ "encoding/binary"
+ "fmt"
+ "crypto/rand"
+ "os"
+ "time"
+
+ "github.com/martinlindhe/base36"
+)
+
+// Generates an RFC 2822-compliant Message-Id based on the informational draft
+// "Recommendations for generating Message IDs", for lack of a better
+// authoritative source.
+func GenerateMessageID() string {
+ var (
+ now bytes.Buffer
+ nonce []byte = make([]byte, 8)
+ )
+ binary.Write(&now, binary.BigEndian, time.Now().UnixNano())
+ rand.Read(nonce)
+ hostname, err := os.Hostname()
+ if err != nil {
+ hostname = "localhost"
+ }
+ return fmt.Sprintf("<%s.%s@%s>",
+ base36.EncodeBytes(now.Bytes()),
+ base36.EncodeBytes(nonce),
+ hostname)
+}
diff --git a/mail/mail_test.go b/mail/mail_test.go
index cfb30ca..04abffd 100644
--- a/mail/mail_test.go
+++ b/mail/mail_test.go
@@ -1,5 +1,12 @@
package mail_test
+import (
+ "regexp"
+ "testing"
+
+ "github.com/emersion/go-message/mail"
+)
+
const mailString = "Subject: Your Name\r\n" +
"Content-Type: multipart/mixed; boundary=message-boundary\r\n" +
"\r\n" +
@@ -33,3 +40,11 @@ const nestedMailString = "Subject: Fwd: Your Name\r\n" +
"\r\n" +
mailString +
"--outer-message-boundary--\r\n"
+
+func TestGenerateMessageID(t *testing.T) {
+ msgId := mail.GenerateMessageID()
+ regex := regexp.MustCompile(`^<.*@.*>$`)
+ if !regex.MatchString(msgId) {
+ t.Error("Generated message ID does not meet RFC requirement")
+ }
+}
--
2.21.0
Pushed:
To github.com:emersion/go-message
35cfd6879e15..e2aba7dea6f6 master -> master
Thanks!