|
|
package email |
|
|
|
|
|
import ( |
|
|
"errors" |
|
|
"fmt" |
|
|
"net/smtp" |
|
|
) |
|
|
|
|
|
type loginAuth struct { |
|
|
host string |
|
|
user string |
|
|
pass string |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func LoginAuth(user, pass, host string) smtp.Auth { |
|
|
return &loginAuth{host: host, user: user, pass: pass} |
|
|
} |
|
|
|
|
|
func isLocalhost(name string) bool { |
|
|
return name == "localhost" || name == "127.0.0.1" || name == "::1" |
|
|
} |
|
|
|
|
|
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !server.TLS && !isLocalhost(server.Name) { |
|
|
return "", nil, errors.New("unencrypted connection") |
|
|
} |
|
|
if server.Name != a.host { |
|
|
return "", nil, errors.New("wrong host name") |
|
|
} |
|
|
return "LOGIN", nil, nil |
|
|
} |
|
|
|
|
|
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { |
|
|
if !more { |
|
|
return nil, nil |
|
|
} |
|
|
|
|
|
switch string(fromServer) { |
|
|
case "Username:": |
|
|
return []byte(a.user), nil |
|
|
case "Password:": |
|
|
return []byte(a.pass), nil |
|
|
} |
|
|
|
|
|
return nil, fmt.Errorf("unexpected server challenge: %s", string(fromServer)) |
|
|
} |
|
|
|