Skip to content

Commit

Permalink
Refactor Client methods to handle smtp.Client parameter
Browse files Browse the repository at this point in the history
Updated several Client methods to accept a smtp.Client pointer, allowing for more flexible and explicit SMTP client management. Added detailed parameter descriptions and extended error handling documentation.
  • Loading branch information
wneessen committed Nov 22, 2024
1 parent 3553b65 commit 4c107f4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
68 changes: 66 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ func (c *Client) SetSSLPort(ssl bool, fallback bool) {
}

// SetDebugLog sets or overrides whether the Client is using debug logging. The debug logger will log incoming
// and outgoing communication between the Client and the server to os.Stderr.
// and outgoing communication between the client and the server to log.Logger that is defined on the Client.
//
// Note: The SMTP communication might include unencrypted authentication data, depending on whether you are using
// SMTP authentication and the type of authentication mechanism. This could pose a data protection risk. Use
Expand All @@ -815,6 +815,17 @@ func (c *Client) SetDebugLog(val bool) {
c.SetDebugLogWithSMTPClient(c.smtpClient, val)
}

// SetDebugLogWithSMTPClient sets or overrides whether the provided smtp.Client is using debug logging.
// The debug logger will log incoming and outgoing communication between the client and the server to
// log.Logger that is defined on the Client.
//
// Note: The SMTP communication might include unencrypted authentication data, depending on whether you are using
// SMTP authentication and the type of authentication mechanism. This could pose a data protection risk. Use
// debug logging with caution.
//
// Parameters:
// - client: A pointer to the smtp.Client that handles the connection to the server.
// - val: A boolean value indicating whether to enable (true) or disable (false) debug logging.
func (c *Client) SetDebugLogWithSMTPClient(client *smtp.Client, val bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
Expand All @@ -836,6 +847,15 @@ func (c *Client) SetLogger(logger log.Logger) {
c.SetLoggerWithSMTPClient(c.smtpClient, logger)
}

// SetLoggerWithSMTPClient sets or overrides the custom logger currently used by the provided smtp.Client.
// The logger must satisfy the log.Logger interface and is only utilized when debug logging is enabled on
// the provided smtp.Client.
//
// By default, log.Stdlog is used if no custom logger is provided.
//
// Parameters:
// - client: A pointer to the smtp.Client that handles the connection to the server.
// - logger: A logger that satisfies the log.Logger interface to be set for the Client.
func (c *Client) SetLoggerWithSMTPClient(client *smtp.Client, logger log.Logger) {
c.mutex.Lock()
defer c.mutex.Unlock()
Expand Down Expand Up @@ -1024,6 +1044,19 @@ func (c *Client) Close() error {
return c.CloseWithSMTPClient(c.smtpClient)
}

// CloseWithSMTPClient terminates the connection of the provided smtp.Client to the SMTP server,
// returning an error if the disconnection fails. If the connection is already closed, this
// method is a no-op and disregards any error.
//
// This function checks if the smtp.Client connection is active. If not, it simply returns
// without any action. If the connection is active, it attempts to gracefully close the
// connection using the Quit method.
//
// Parameters:
// - client: A pointer to the smtp.Client that handles the connection to the server.
//
// Returns:
// - An error if the disconnection fails; otherwise, returns nil.
func (c *Client) CloseWithSMTPClient(client *smtp.Client) error {
if client == nil || !client.HasConnection() {
return nil
Expand All @@ -1042,11 +1075,24 @@ func (c *Client) CloseWithSMTPClient(client *smtp.Client) error {
// the command fails, an error is returned.
//
// Returns:
// - An error if the connection check fails or if sending the RSET command fails; otherwise, returns nil.
// - An error if the connection check fails or if sending the RSET command fails;
// otherwise, returns nil.
func (c *Client) Reset() error {
return c.ResetWithSMTPClient(c.smtpClient)
}

// ResetWithSMTPClient sends an SMTP RSET command to the provided smtp.Client, to reset
// the state of the current SMTP session.
//
// This method checks the connection to the SMTP server and, if the connection is valid,
// it sends an RSET command to reset the session state. If the connection is invalid or
// the command fails, an error is returned.
//
// Parameters:
// - client: A pointer to the smtp.Client that handles the connection to the server.
//
// Returns:
// - An error if the connection check fails or if sending the RSET command fails; otherwise, returns nil.
func (c *Client) ResetWithSMTPClient(client *smtp.Client) error {
if err := c.checkConn(client); err != nil {
return err
Expand Down Expand Up @@ -1108,6 +1154,24 @@ func (c *Client) DialAndSendWithContext(ctx context.Context, messages ...*Msg) e
return nil
}

// Send attempts to send one or more Msg using the SMTP client that is assigned to the Client.
// If the Client has no active connection to the server, Send will fail with an error. For
// each of the provided Msg, it will associate a SendError with the Msg in case of a
// transmission or delivery error.
//
// This method first checks for an active connection to the SMTP server. If the connection is
// not valid, it returns a SendError. It then iterates over the provided messages, attempting
// to send each one. If an error occurs during sending, the method records the error and
// associates it with the corresponding Msg. If multiple errors are encountered, it aggregates
// them into a single SendError to be returned.
//
// Parameters:
// - client: A pointer to the smtp.Client that holds the connection to the SMTP server
// - messages: A variadic list of pointers to Msg objects to be sent.
//
// 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) (returnErr error) {
c.sendMutex.Lock()
defer c.sendMutex.Unlock()
Expand Down
10 changes: 5 additions & 5 deletions client_119.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"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
// of the provided Msg, it will associate a SendError with the Msg in case of a transmission
// or delivery error.
// SendWithSMTPClient attempts to send one or more Msg using a provided smtp.Client with an
// established connection to the SMTP server. If the smtp.Client has no active connection to
// the server, SendWithSMTPClient will fail with an error. For each of the provided Msg, it
// will associate a SendError with the Msg in case of a transmission or delivery error.
//
// This method first checks for an active connection to the SMTP server. If the connection is
// not valid, it returns a SendError. It then iterates over the provided messages, attempting
Expand All @@ -25,12 +25,12 @@ import (
// them into a single SendError to be returned.
//
// Parameters:
// - client: A pointer to the smtp.Client that holds the connection to the SMTP server
// - messages: A variadic list of pointers to Msg objects to be sent.
//
// Returns:
// - An error that represents the sending result, which may include multiple SendErrors if
// any occurred; otherwise, returns nil.

func (c *Client) SendWithSMTPClient(client *smtp.Client, messages ...*Msg) error {
escSupport := false
if client != nil {
Expand Down
20 changes: 11 additions & 9 deletions client_120.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ import (
"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
// of the provided Msg, it will associate a SendError with the Msg in case of a transmission
// or delivery error.
// SendWithSMTPClient attempts to send one or more Msg using a provided smtp.Client with an
// established connection to the SMTP server. If the smtp.Client has no active connection to
// the server, SendWithSMTPClient will fail with an error. For each of the provided Msg, it
// will associate a SendError with the Msg in case of a transmission or delivery error.
//
// This method first checks for an active connection to the SMTP server. If the connection is
// not valid, it returns an error wrapped in a SendError. It then iterates over the provided
// messages, attempting to send each one. If an error occurs during sending, the method records
// the error and associates it with the corresponding Msg.
// not valid, it returns a SendError. It then iterates over the provided messages, attempting
// to send each one. If an error occurs during sending, the method records the error and
// associates it with the corresponding Msg. If multiple errors are encountered, it aggregates
// them into a single SendError to be returned.
//
// Parameters:
// - client: A pointer to the smtp.Client that holds the connection to the SMTP server
// - messages: A variadic list of pointers to Msg objects to be sent.
//
// Returns:
// - An error that aggregates any SendErrors encountered during the sending process; otherwise, returns nil.

// - An error that represents the sending result, which may include multiple SendErrors if
// any occurred; otherwise, returns nil.
func (c *Client) SendWithSMTPClient(client *smtp.Client, messages ...*Msg) (returnErr error) {
escSupport := false
if client != nil {
Expand Down

0 comments on commit 4c107f4

Please sign in to comment.