-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update README server example, add server example in tests
- Loading branch information
Showing
2 changed files
with
85 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,18 @@ | |
package smtp_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"strings" | ||
|
||
"github.com/emersion/go-sasl" | ||
"github.com/emersion/go-smtp" | ||
) | ||
|
||
func Example() { | ||
func ExampleDial() { | ||
// Connect to the remote SMTP server. | ||
c, err := smtp.Dial("mail.example.com:25") | ||
if err != nil { | ||
|
@@ -57,7 +60,7 @@ var ( | |
recipients = []string{"[email protected]"} | ||
) | ||
|
||
func ExamplePlainAuth() { | ||
func ExampleSendMail_PlainAuth() { | ||
// hostname is used by PlainAuth to validate the TLS certificate. | ||
hostname := "mail.example.com" | ||
auth := sasl.NewPlainClient("", "[email protected]", "password") | ||
|
@@ -84,3 +87,65 @@ func ExampleSendMail() { | |
log.Fatal(err) | ||
} | ||
} | ||
|
||
// The Backend implements SMTP server methods. | ||
type Backend struct{} | ||
|
||
// Login handles a login command with username and password. | ||
func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) { | ||
if username != "username" || password != "password" { | ||
return nil, errors.New("Invalid username or password") | ||
} | ||
return &Session{}, nil | ||
} | ||
|
||
// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails | ||
func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) { | ||
return nil, smtp.ErrAuthRequired | ||
} | ||
|
||
// A Session is returned after successful login. | ||
type Session struct{} | ||
|
||
func (s *Session) Mail(from string) error { | ||
log.Println("Mail from:", from) | ||
return nil | ||
} | ||
|
||
func (s *Session) Rcpt(to string) error { | ||
log.Println("Rcpt to:", from) | ||
return nil | ||
} | ||
|
||
func (s *Session) Data(r io.Reader) error { | ||
if b, err := ioutil.ReadAll(r); err != nil { | ||
return err | ||
} else { | ||
log.Println("Data:", string(b)) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Session) Reset() {} | ||
|
||
func (s *Session) Logout() error { | ||
return nil | ||
} | ||
|
||
func ExampleNewServer() { | ||
be := &Backend{} | ||
|
||
s := smtp.NewServer(be) | ||
|
||
s.Addr = ":1025" | ||
s.Domain = "localhost" | ||
s.MaxIdleSeconds = 300 | ||
s.MaxMessageBytes = 1024 * 1024 | ||
s.MaxRecipients = 50 | ||
s.AllowInsecureAuth = true | ||
|
||
log.Println("Starting server at", s.Addr) | ||
if err := s.ListenAndServe(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |