go-mail/client_119.go
Winni Neessen bd5a8a40b9
Fix error handling in Send method in client_119.go
Refine Send method to correctly typecast and accumulate SendError instances. Introduce "errors" package import to utilize errors.As for precise error type checking, ensuring accurate error lists. This regression was introduced with PR #301
2024-09-20 16:40:12 +02:00

46 lines
1.1 KiB
Go

// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
//go:build !go1.20
// +build !go1.20
package mail
import "errors"
// Send sends out the mail message
func (c *Client) Send(messages ...*Msg) error {
if err := c.checkConn(); err != nil {
return &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err)}
}
var errs []*SendError
for id, message := range messages {
if sendErr := c.sendSingleMsg(message); sendErr != nil {
messages[id].sendError = sendErr
var msgSendErr *SendError
if errors.As(sendErr, &msgSendErr) {
errs = append(errs, msgSendErr)
}
}
}
if len(errs) > 0 {
if len(errs) > 1 {
returnErr := &SendError{Reason: ErrAmbiguous}
for i := range errs {
returnErr.errlist = append(returnErr.errlist, errs[i].errlist...)
returnErr.rcpt = append(returnErr.rcpt, errs[i].rcpt...)
}
// We assume that the isTemp flag from the last error we received should be the
// indicator for the returned isTemp flag as well
returnErr.isTemp = errs[len(errs)-1].isTemp
return returnErr
}
return errs[0]
}
return nil
}