...

Source file src/github.com/go-mail/mail/auth.go

Documentation: github.com/go-mail/mail

     1  package mail
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  	"net/smtp"
     8  )
     9  
    10  // loginAuth is an smtp.Auth that implements the LOGIN authentication mechanism.
    11  type loginAuth struct {
    12  	username string
    13  	password string
    14  	host     string
    15  }
    16  
    17  func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
    18  	if !server.TLS {
    19  		advertised := false
    20  		for _, mechanism := range server.Auth {
    21  			if mechanism == "LOGIN" {
    22  				advertised = true
    23  				break
    24  			}
    25  		}
    26  		if !advertised {
    27  			return "", nil, errors.New("gomail: unencrypted connection")
    28  		}
    29  	}
    30  	if server.Name != a.host {
    31  		return "", nil, errors.New("gomail: wrong host name")
    32  	}
    33  	return "LOGIN", nil, nil
    34  }
    35  
    36  func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
    37  	if !more {
    38  		return nil, nil
    39  	}
    40  
    41  	switch {
    42  	case bytes.Equal(fromServer, []byte("Username:")):
    43  		return []byte(a.username), nil
    44  	case bytes.Equal(fromServer, []byte("Password:")):
    45  		return []byte(a.password), nil
    46  	default:
    47  		return nil, fmt.Errorf("gomail: unexpected server challenge: %s", fromServer)
    48  	}
    49  }
    50  

View as plain text