(Feat): Initial Commit
This commit is contained in:
50
internal/mailer/transport/smtp_login_auth.go
Normal file
50
internal/mailer/transport/smtp_login_auth.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type loginAuth struct {
|
||||
username []byte
|
||||
password []byte
|
||||
host string
|
||||
}
|
||||
|
||||
func LoginAuth(username string, password string, host ...string) smtp.Auth {
|
||||
auth := &loginAuth{
|
||||
username: []byte(username),
|
||||
password: []byte(password),
|
||||
host: "",
|
||||
}
|
||||
|
||||
if len(host) > 0 {
|
||||
auth.host = host[0]
|
||||
}
|
||||
|
||||
return auth
|
||||
}
|
||||
|
||||
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
|
||||
if len(a.host) > 0 && server.Name != a.host {
|
||||
return "", nil, errors.New("wrong host name")
|
||||
}
|
||||
|
||||
return "LOGIN", a.username, nil
|
||||
}
|
||||
|
||||
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
|
||||
if more {
|
||||
switch string(fromServer) {
|
||||
case "Username:":
|
||||
return a.username, nil
|
||||
case "Password:":
|
||||
return a.password, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown challenge received from server: %q", fromServer)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user