Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concurrency issue in DialAndSendWithContext #386

Merged
merged 11 commits into from
Nov 22, 2024
Prev Previous commit
Next Next commit
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.
  • Loading branch information
wneessen committed Nov 22, 2024
commit 3553b657697cc9fef3180b91121523d0750494c0
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,10 @@

client, err := smtp.NewClient(connection, c.host)
if err != nil {
return nil, err

Check warning on line 984 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L984

Added line #L984 was not covered by tests
}
if client == nil {
return nil, fmt.Errorf("SMTP client is nil")

Check warning on line 987 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L987

Added line #L987 was not covered by tests
}

if c.logger != nil {
Expand Down Expand Up @@ -1108,6 +1108,12 @@
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.
Expand Down
17 changes: 11 additions & 6 deletions client_119.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,20 +30,21 @@ 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),
}
}
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
Expand Down
5 changes: 0 additions & 5 deletions client_120.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading