-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from wneessen/feature/90_provide-a-way-of-know…
…ing-which-emails-failedsucceeded-in-sending Introduction of new error type for sending errors
- Loading branch information
Showing
7 changed files
with
430 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ package mail | |
import ( | ||
"context" | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"net/smtp" | ||
"os" | ||
|
@@ -989,6 +990,93 @@ func TestValidateLine(t *testing.T) { | |
} | ||
} | ||
|
||
// TestClient_Send_MsgSendError tests the Client.Send method with a broken recipient and verifies | ||
// that the SendError type works properly | ||
func TestClient_Send_MsgSendError(t *testing.T) { | ||
if os.Getenv("TEST_ALLOW_SEND") == "" { | ||
t.Skipf("TEST_ALLOW_SEND is not set. Skipping mail sending test") | ||
} | ||
var msgs []*Msg | ||
rcpts := []string{"[email protected]", "[email protected]"} | ||
for _, rcpt := range rcpts { | ||
m := NewMsg() | ||
_ = m.FromFormat("go-mail Test Mailer", os.Getenv("TEST_FROM")) | ||
_ = m.To(rcpt) | ||
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") | ||
msgs = append(msgs, m) | ||
} | ||
|
||
c, err := getTestConnection(true) | ||
if err != nil { | ||
t.Skipf("failed to create test client: %s. Skipping tests", err) | ||
} | ||
|
||
ctx, cfn := context.WithTimeout(context.Background(), DefaultTimeout) | ||
defer cfn() | ||
if err := c.DialWithContext(ctx); err != nil { | ||
t.Errorf("failed to dial to sending server: %s", err) | ||
} | ||
if err := c.Send(msgs...); err == nil { | ||
t.Errorf("sending messages with broken recipients was supposed to fail but didn't") | ||
} | ||
if err := c.Close(); err != nil { | ||
t.Errorf("failed to close client connection: %s", err) | ||
} | ||
for _, m := range msgs { | ||
if !m.HasSendError() { | ||
t.Errorf("message was expected to have a send error, but didn't") | ||
} | ||
se := &SendError{Reason: ErrSMTPRcptTo} | ||
if !errors.Is(m.SendError(), se) { | ||
t.Errorf("error mismatch, expected: %s, got: %s", se, m.SendError()) | ||
} | ||
if m.SendErrorIsTemp() { | ||
t.Errorf("message was not expected to be a temporary error, but reported as such") | ||
} | ||
} | ||
} | ||
|
||
// TestClient_DialAndSendWithContext_withSendError tests the Client.DialAndSendWithContext method | ||
// with a broken recipient to make sure that the returned error satisfies the Msg.SendError type | ||
func TestClient_DialAndSendWithContext_withSendError(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("[email protected]") | ||
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.Skipf("failed to create test client: %s. Skipping tests", err) | ||
} | ||
ctx, cfn := context.WithTimeout(context.Background(), DefaultTimeout) | ||
defer cfn() | ||
err = c.DialAndSendWithContext(ctx, m) | ||
if err == nil { | ||
t.Errorf("expected DialAndSendWithContext with broken mail recipient to fail, but didn't") | ||
return | ||
} | ||
var se *SendError | ||
if !errors.As(err, &se) { | ||
t.Errorf("expected *SendError type as returned error, but didn't") | ||
return | ||
} | ||
if se.IsTemp() { | ||
t.Errorf("expected permanent error but IsTemp() returned true") | ||
return | ||
} | ||
} | ||
|
||
// 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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
package mail | ||
|
||
// VERSION is used in the default user agent string | ||
const VERSION = "0.3.6" | ||
const VERSION = "0.3.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.