Add new Send function to client.go and remove duplicates

The new Send function in client.go adds thread safety by using a mutex. This change also removes duplicate Send functions from client_119.go and client_120.go, consolidating the logic in one place for easier maintenance.
This commit is contained in:
Winni Neessen 2024-11-22 15:38:04 +01:00
parent 3e504e6338
commit 3553b65769
Signed by: wneessen
GPG key ID: 385AC9889632126E
3 changed files with 17 additions and 11 deletions

View file

@ -1108,6 +1108,12 @@ func (c *Client) DialAndSendWithContext(ctx context.Context, messages ...*Msg) e
return nil
}
func (c *Client) Send(messages ...*Msg) (returnErr error) {
c.sendMutex.Lock()
defer c.sendMutex.Unlock()
return c.SendWithSMTPClient(c.smtpClient, messages...)
}
// auth attempts to authenticate the client using SMTP AUTH mechanisms. It checks the connection,
// determines the supported authentication methods, and applies the appropriate authentication
// type. An error is returned if authentication fails.

View file

@ -7,7 +7,11 @@
package mail
import "errors"
import (
"errors"
"github.com/wneessen/go-mail/smtp"
)
// Send attempts to send one or more Msg using the Client connection to the SMTP server.
// If the Client has no active connection to the server, Send will fail with an error. For each
@ -26,12 +30,13 @@ import "errors"
// Returns:
// - An error that represents the sending result, which may include multiple SendErrors if
// any occurred; otherwise, returns nil.
func (c *Client) Send(messages ...*Msg) error {
func (c *Client) SendWithSMTPClient(client *smtp.Client, messages ...*Msg) error {
escSupport := false
if c.smtpClient != nil {
escSupport, _ = c.smtpClient.Extension("ENHANCEDSTATUSCODES")
if client != nil {
escSupport, _ = client.Extension("ENHANCEDSTATUSCODES")
}
if err := c.checkConn(); err != nil {
if err := c.checkConn(client); err != nil {
return &SendError{
Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err),
errcode: errorCode(err), enhancedStatusCode: enhancedStatusCode(err, escSupport),
@ -39,7 +44,7 @@ func (c *Client) Send(messages ...*Msg) error {
}
var errs []*SendError
for id, message := range messages {
if sendErr := c.sendSingleMsg(message); sendErr != nil {
if sendErr := c.sendSingleMsg(client, message); sendErr != nil {
messages[id].sendError = sendErr
var msgSendErr *SendError

View file

@ -28,11 +28,6 @@ import (
//
// Returns:
// - An error that aggregates any SendErrors encountered during the sending process; otherwise, returns nil.
func (c *Client) Send(messages ...*Msg) (returnErr error) {
c.sendMutex.Lock()
defer c.sendMutex.Unlock()
return c.SendWithSMTPClient(c.smtpClient, messages...)
}
func (c *Client) SendWithSMTPClient(client *smtp.Client, messages ...*Msg) (returnErr error) {
escSupport := false