update ci; update deps (show all smtp connection errors); migrate from make to just

This commit is contained in:
Aine
2023-02-11 20:49:45 +02:00
parent 12d2fee2d4
commit dc82d97aaa
7 changed files with 88 additions and 75 deletions

View File

@@ -35,17 +35,37 @@ func Connect(from, to string) (*smtp.Client, error) {
return client, nil
}
func unwrapErrors(errs []error) error {
if len(errs) == 0 {
return nil
}
tokens := strings.Repeat("%v; ", len(errs))
// make it compatible with < 1.18
ierrs := make([]interface{}, len(errs))
for _, err := range errs {
ierrs = append(ierrs, err)
}
return fmt.Errorf(tokens, ierrs...)
}
func initClient(localname, hostname string) (*smtp.Client, error) {
mxs, err := net.LookupMX(hostname)
if err != nil {
return nil, err
}
cerrs := []error{}
var client *smtp.Client
for _, mx := range mxs {
for _, addr := range SMTPAddrs {
client := trySMTP(localname, strings.TrimSuffix(mx.Host, "."), addr)
client, err = trySMTP(localname, strings.TrimSuffix(mx.Host, "."), addr)
if err != nil {
cerrs = append(cerrs, err)
}
if client != nil {
return client, nil
return client, unwrapErrors(cerrs)
}
}
}
@@ -54,29 +74,33 @@ func initClient(localname, hostname string) (*smtp.Client, error) {
// we're supposed to try talking directly to the host.
if len(mxs) == 0 {
for _, addr := range SMTPAddrs {
client := trySMTP(localname, hostname, addr)
client, err = trySMTP(localname, hostname, addr)
if err != nil {
cerrs = append(cerrs, err)
}
if client != nil {
return client, nil
return client, unwrapErrors(cerrs)
}
}
}
return nil, fmt.Errorf("target SMTP server not found")
return nil, unwrapErrors(cerrs)
}
func trySMTP(localname, mxhost, addr string) *smtp.Client {
conn, err := smtp.Dial(mxhost + addr)
func trySMTP(localname, mxhost, addr string) (*smtp.Client, error) {
target := mxhost + addr
conn, err := smtp.Dial(target)
if err != nil {
return nil
return nil, fmt.Errorf("%s: %w", target, err)
}
err = conn.Hello(localname)
if err != nil {
return nil
return nil, fmt.Errorf("%s: %w", target, err)
}
if ok, _ := conn.Extension("STARTTLS"); ok {
config := &tls.Config{ServerName: mxhost}
conn.StartTLS(config) //nolint:errcheck // if it doesn't work - we can't do anything anyway
}
return conn
return conn, nil
}