https://github.com/k1low/smtptest
smtptest provides SMTP server for testing.
https://github.com/k1low/smtptest
go mail smtp testing
Last synced: about 1 year ago
JSON representation
smtptest provides SMTP server for testing.
- Host: GitHub
- URL: https://github.com/k1low/smtptest
- Owner: k1LoW
- License: mit
- Created: 2022-04-11T04:07:14.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-13T04:35:23.000Z (over 1 year ago)
- Last Synced: 2025-03-29T05:41:34.444Z (about 1 year ago)
- Topics: go, mail, smtp, testing
- Language: Go
- Homepage:
- Size: 61.5 KB
- Stars: 5
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# smtptest [](https://github.com/k1LoW/smtptest/actions/workflows/ci.yml)   
`smtptest` provides SMTP server for testing.
## Usage
``` go
package main
import (
"fmt"
"net/smtp"
"strings"
"testing"
"github.com/k1LoW/smtptest"
)
const testMsg = "To: alice@example.net\r\n" +
"Subject: Hello Gophers!\r\n" +
"\r\n" +
"This is the email body.\r\n"
func TestSendMail(t *testing.T) {
ts, auth, err := smtptest.NewServerWithAuth()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
ts.Close()
})
addr := ts.Addr()
if err := smtp.SendMail(addr, auth, "sender@example.org", []string{"alice@example.net"}, []byte(testMsg)); err != nil {
t.Fatal(err)
}
if len(ts.Messages()) != 1 {
t.Errorf("got %v\nwant %v", len(ts.Messages()), 1)
}
msgs := ts.Messages()
got := msgs[0].Header.Get("To")
want := "alice@example.net"
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
}
```
## References
- https://github.com/influxdata/kapacitor/tree/master/services/smtp/smtptest