go-mail/client_119.go
Winni Neessen 23c71d608f
Lock mutex before checking connection in Send method
Added mutex locking in the `Send` method for both `client_120.go` and `client_119.go`. This ensures thread-safe access to the connection checks and prevents potential race conditions.
2024-09-27 10:33:28 +02:00

49 lines
1.2 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 {
c.mutex.Lock()
defer c.mutex.Unlock()
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
}