-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_test.go
52 lines (44 loc) · 1.11 KB
/
email_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bytes"
"io"
"net/mail"
"strings"
"testing"
smtptest "github.com/davrux/go-smtptester"
"github.com/jordan-wright/email"
)
func TestSmtp(t *testing.T) {
smtpServer := smtptest.Standard() // port :2525
go smtpServer.ListenAndServe()
defer smtpServer.Close()
smtpbe := smtptest.GetBackend(smtpServer)
em := email.NewEmail()
em.From = "from@example.net"
em.To = []string{"to@example.net"}
em.Subject = "larigot: new user"
em.Text = []byte(`New user larigot!`)
address := "localhost:2525"
if err := em.Send(address, nil); err != nil {
if strings.Contains(strings.ToLower(err.Error()), "connection refused") {
t.Logf("<%s> - connection refused error, skipping", err)
t.Skip()
} else {
t.Fatal(err.Error())
}
}
message, ok := smtpbe.Load("from@example.net", []string{"to@example.net"})
if !ok {
t.Fatal("message not found")
}
buf1 := bytes.NewBuffer(message.Data)
message1, err := mail.ReadMessage(buf1)
if err != nil {
t.Fatal(err.Error())
}
message1Body, err := io.ReadAll(message1.Body)
if err != nil {
t.Fatal(err.Error())
}
t.Logf("%s", message1Body)
}