mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 05:40:50 +01:00
Add actual mail sending test to client_test.go
This commit is contained in:
parent
c3298d7181
commit
f15d3ca25e
1 changed files with 36 additions and 0 deletions
|
@ -14,6 +14,9 @@ import (
|
|||
// DefaultHost is used as default hostname for the Client
|
||||
const DefaultHost = "localhost"
|
||||
|
||||
// TestRcpt
|
||||
const TestRcpt = "go-mail@mytrashmailer.com"
|
||||
|
||||
// TestNewClient tests the NewClient() method with its default options
|
||||
func TestNewClient(t *testing.T) {
|
||||
host := "mail.example.com"
|
||||
|
@ -526,6 +529,39 @@ func TestClient_DialWithContextOptions(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestClient_Send tests the Dial(), Send() and Close() method of Client
|
||||
func TestClient_DialSendClose(t *testing.T) {
|
||||
if os.Getenv("TEST_ALLOW_SEND") == "" {
|
||||
t.Skipf("TEST_ALLOW_SEND is not set. Skipping mail sending test")
|
||||
}
|
||||
m := NewMsg()
|
||||
_ = m.FromFormat("go-mail Test Mailer", os.Getenv("TEST_FROM"))
|
||||
_ = m.To(TestRcpt)
|
||||
m.Subject(fmt.Sprintf("This is a test mail from go-mail/v%s", VERSION))
|
||||
m.SetBulk()
|
||||
m.SetDate()
|
||||
m.SetMessageID()
|
||||
m.SetBodyString(TypeTextPlain, "This is a test mail from the go-mail library")
|
||||
|
||||
c, err := getTestConnection(true)
|
||||
if err != nil {
|
||||
t.Errorf("Send() failed: could not get new client connection: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cfn := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cfn()
|
||||
if err := c.DialWithContext(ctx); err != nil {
|
||||
t.Errorf("Dail() failed: %s", err)
|
||||
}
|
||||
if err := c.Send(m); err != nil {
|
||||
t.Errorf("Send() failed: %s", err)
|
||||
}
|
||||
if err := c.Close(); err != nil {
|
||||
t.Errorf("Close() failed: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// getTestConnection takes environment variables to establish a connection to a real
|
||||
// SMTP server to test all functionality that requires a connection
|
||||
func getTestConnection(auth bool) (*Client, error) {
|
||||
|
|
Loading…
Reference in a new issue