Skip to content

Commit 9e949e0

Browse files
committed
readme: check auth
By design, go-smtp won't check whether the session is authenticated during MAIL/RCPT/DATA commands. Make this more explicit by checking whether the session is authenticated in the example. Closes: #216
1 parent a5a8e30 commit 9e949e0

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,38 @@ func (bkd *Backend) NewSession(_ *smtp.Conn) (smtp.Session, error) {
103103
}
104104

105105
// A Session is returned after EHLO.
106-
type Session struct{}
106+
type Session struct {
107+
auth bool
108+
}
107109

108110
func (s *Session) AuthPlain(username, password string) error {
109111
if username != "username" || password != "password" {
110112
return smtp.ErrAuthFailed
111113
}
114+
s.auth = true
112115
return nil
113116
}
114117

115118
func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
119+
if !s.auth {
120+
return ErrAuthRequired
121+
}
116122
log.Println("Mail from:", from)
117123
return nil
118124
}
119125

120126
func (s *Session) Rcpt(to string) error {
127+
if !s.auth {
128+
return ErrAuthRequired
129+
}
121130
log.Println("Rcpt to:", to)
122131
return nil
123132
}
124133

125134
func (s *Session) Data(r io.Reader) error {
135+
if !s.auth {
136+
return ErrAuthRequired
137+
}
126138
if b, err := ioutil.ReadAll(r); err != nil {
127139
return err
128140
} else {

0 commit comments

Comments
 (0)