go-mail/client_120.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

37 lines
706 B
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) (returnErr error) {
c.mutex.Lock()
defer c.mutex.Unlock()
if err := c.checkConn(); err != nil {
returnErr = &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err)}
return
}
var errs []error
defer func() {
returnErr = errors.Join(errs...)
}()
for id, message := range messages {
if sendErr := c.sendSingleMsg(message); sendErr != nil {
messages[id].sendError = sendErr
errs = append(errs, sendErr)
}
}
return
}