aboutsummaryrefslogtreecommitdiff
path: root/lib/smtp/mail_tx_example_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-02-27 22:24:52 +0700
committerShulhan <ms@kilabit.info>2022-02-28 14:39:40 +0700
commitf8e4959352cd6653b8bdb10c445d06196406006a (patch)
treeccc9ccb52a831f2856dfe2ef4250da19494921fd /lib/smtp/mail_tx_example_test.go
parent3a1a2715b25f15fe6403af286f91e2b9168a33c9 (diff)
downloadpakakeh.go-f8e4959352cd6653b8bdb10c445d06196406006a.tar.xz
lib/email: set the Date and Message-ID on Message Pack
Calling Pack now set the Date header if its not exist, using the local time; and the message-id header if its not exist using the following format: <epoch>.<random-8-chars>@<local-hostname> The random-8-chars is Seed-ed from Epoch(), so does the boundary.
Diffstat (limited to 'lib/smtp/mail_tx_example_test.go')
-rw-r--r--lib/smtp/mail_tx_example_test.go26
1 files changed, 17 insertions, 9 deletions
diff --git a/lib/smtp/mail_tx_example_test.go b/lib/smtp/mail_tx_example_test.go
index 33ebbee3..a4b6df0a 100644
--- a/lib/smtp/mail_tx_example_test.go
+++ b/lib/smtp/mail_tx_example_test.go
@@ -8,7 +8,7 @@ import (
"bytes"
"fmt"
"log"
- "math/rand"
+ "os"
"regexp"
"time"
@@ -39,12 +39,12 @@ func ExampleNewMailTx() {
mboxes []*email.Mailbox
msg *email.Message
mailtx *MailTx
+ reDate *regexp.Regexp
+ hostname string
data []byte
err error
)
- rand.Seed(42)
-
mboxes, err = email.ParseMailboxes(toAddresses)
if err != nil {
log.Fatal(err)
@@ -75,15 +75,22 @@ func ExampleNewMailTx() {
fmt.Printf("Tx Recipients: %s\n", mailtx.Recipients)
// In order to make the example Output works, we need to replace all
- // CRLF with LF and "date:" with the system timezone.
+ // CRLF with LF, "date:" with the system timezone, and message-id
+ // hostname with fixed "hostname".
data = bytes.ReplaceAll(mailtx.Data, []byte("\r\n"), []byte("\n"))
//fmt.Printf("timeNowUtc: %s\n", timeNowUtc)
//fmt.Printf("dateNowUtc: %s\n", dateNowUtc)
- reDate := regexp.MustCompile(`^date: Wed(.*) \+....`)
+ reDate = regexp.MustCompile(`^date: Wed(.*) \+....`)
data = reDate.ReplaceAll(data, []byte(`date: `+dateNowUtc))
+ hostname, err = os.Hostname()
+ if err != nil {
+ log.Fatal(err)
+ }
+ data = bytes.Replace(data, []byte(hostname), []byte("hostname"), 1)
+
fmt.Printf("Tx Data:\n%s", data)
//Output:
//Tx From: Postmaster <postmaster@mail.example.com>
@@ -94,19 +101,20 @@ func ExampleNewMailTx() {
//to: John <john@example.com>, Jane <jane@example.com>
//subject: Example subject
//mime-version: 1.0
- //content-type: multipart/alternative; boundary=1b4df158039f7cce49f0a64b0ea7b7dd
+ //content-type: multipart/alternative; boundary=QoqDPQfzDVkv5R49vrA78GmqPmlfmBHf
+ //message-id: <1645600000.QoqDPQfz@hostname>
//
- //--1b4df158039f7cce49f0a64b0ea7b7dd
+ //--QoqDPQfzDVkv5R49vrA78GmqPmlfmBHf
//mime-version: 1.0
//content-type: text/plain; charset="utf-8"
//content-transfer-encoding: quoted-printable
//
//Email body as plain text
- //--1b4df158039f7cce49f0a64b0ea7b7dd
+ //--QoqDPQfzDVkv5R49vrA78GmqPmlfmBHf
//mime-version: 1.0
//content-type: text/html; charset="utf-8"
//content-transfer-encoding: quoted-printable
//
//Email body as <b>HTML</b>
- //--1b4df158039f7cce49f0a64b0ea7b7dd--
+ //--QoqDPQfzDVkv5R49vrA78GmqPmlfmBHf--
}