go-mail/client_119.go
Winni Neessen 277ae9be19
Refactor message sending logic
Consolidated the message sending logic into a single `sendSingleMsg` function to reduce duplication and improve code maintainability. This change simplifies the `Send` method in multiple Go version files by removing redundant code and calling the new helper function instead.
2024-09-19 10:56:01 +02:00

40 lines
1 KiB
Go

// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
//go:build !go1.20
// +build !go1.20
package mail
// Send sends out the mail message
func (c *Client) Send(messages ...*Msg) error {
if cerr := c.checkConn(); cerr != nil {
return &SendError{Reason: ErrConnCheck, errlist: []error{cerr}, isTemp: isTempError(cerr)}
}
var errs []*SendError
for _, message := range messages {
if sendErr := c.sendSingleMsg(message); sendErr != nil {
messages[id].sendError = sendErr
errs = append(errs, sendErr)
}
}
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
}