42 lines
904 B
Go
42 lines
904 B
Go
|
// MIT license (c) andelf 2013
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"net/smtp"
|
||
|
)
|
||
|
|
||
|
type loginAuth struct {
|
||
|
username, password string
|
||
|
}
|
||
|
|
||
|
func LoginAuth(username, password string) smtp.Auth {
|
||
|
return &loginAuth{username, password}
|
||
|
}
|
||
|
|
||
|
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
|
||
|
return "LOGIN", []byte(a.username), nil
|
||
|
}
|
||
|
|
||
|
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
|
||
|
if more {
|
||
|
switch string(fromServer) {
|
||
|
case "Username:":
|
||
|
return []byte(a.username), nil
|
||
|
case "Password:":
|
||
|
return []byte(a.password), nil
|
||
|
default:
|
||
|
return nil, errors.New("Unkown fromServer")
|
||
|
}
|
||
|
}
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
// usage:
|
||
|
// auth := LoginAuth("loginname", "password")
|
||
|
// err := smtp.SendMail(smtpServer + ":25", auth, fromAddress, toAddresses, []byte(message))
|
||
|
// or
|
||
|
// client, err := smtp.Dial(smtpServer)
|
||
|
// client.Auth(LoginAuth("loginname", "password"))
|