Refactor DSN field in Client structure

Renamed `dsn` field to `requestDSN` in Client structure for clarity and consistency. Adjusted associated methods and tests to reflect this change, improving code readability and maintainability.
This commit is contained in:
Winni Neessen 2024-10-04 20:29:14 +02:00
parent 92c411454b
commit ef3da39840
Signed by: wneessen
GPG key ID: 385AC9889632126E
2 changed files with 75 additions and 72 deletions

View file

@ -40,19 +40,6 @@ const (
DefaultTLSMinVersion = tls.VersionTLS12
)
type (
// DSNMailReturnOption is a type wrapper for a string and specifies the type of return content requested
// in a Delivery Status Notification (DSN).
// https://datatracker.ietf.org/doc/html/rfc1891/
DSNMailReturnOption string
// DSNRcptNotifyOption is a type wrapper for a string and specifies the notification options for a
// recipient in DSNs.
// https://datatracker.ietf.org/doc/html/rfc1891/
DSNRcptNotifyOption string
)
const (
// DSNMailReturnHeadersOnly requests that only the message headers of the mail message are returned in
@ -90,20 +77,35 @@ const (
DSNRcptNotifyDelay DSNRcptNotifyOption = "DELAY"
)
// DialContextFunc is a type to define custom DialContext function.
type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error)
type (
// Client is the SMTP client struct
type Client struct {
// Timeout for the SMTP server connection
// DialContextFunc defines a function type for establishing a network connection using context, network
// type, and address. It is used to specify custom DialContext function.
//
// By default we use net.Dial or tls.Dial respectively.
DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error)
// DSNMailReturnOption is a type wrapper for a string and specifies the type of return content requested
// in a Delivery Status Notification (DSN).
// https://datatracker.ietf.org/doc/html/rfc1891/
DSNMailReturnOption string
// DSNRcptNotifyOption is a type wrapper for a string and specifies the notification options for a
// recipient in DSNs.
// https://datatracker.ietf.org/doc/html/rfc1891/
DSNRcptNotifyOption string
// Option is a function type that modifies the configuration or behavior of a Client instance.
Option func(*Client) error
// Client is the go-mail client that is responsible for connecting and interacting with an SMTP server.
Client struct {
// connTimeout specifies timeout for the connection to the SMTP server.
connTimeout time.Duration
// dialContextFunc is a custom DialContext function to dial target SMTP server
// dialContextFunc is the DialContextFunc that is used by the Client to connect to the SMTP server.
dialContextFunc DialContextFunc
// dsn indicates that we want to use DSN for the Client
dsn bool
// dsnmrtype defines the DSNMailReturnOption in case DSN is enabled
dsnmrtype DSNMailReturnOption
@ -139,6 +141,9 @@ type Client struct {
// port specifies the network port number on which the server listens for incoming connections.
port int
// requestDSN indicates that we want to use DSN for the Client
requestDSN bool
// smtpAuth is a pointer to smtp.Auth
smtpAuth smtp.Auth
@ -162,10 +167,8 @@ type Client struct {
// Use SSL for the connection
useSSL bool
}
// Option returns a function that can be used for grouping Client options
type Option func(*Client) error
}
)
var (
// ErrInvalidPort should be used if a port is specified that is not valid
@ -394,7 +397,7 @@ func WithPassword(password string) Option {
// and DSNRcptNotifyFailure
func WithDSN() Option {
return func(c *Client) error {
c.dsn = true
c.requestDSN = true
c.dsnmrtype = DSNMailReturnFull
c.dsnrntype = []string{string(DSNRcptNotifyFailure), string(DSNRcptNotifySuccess)}
return nil
@ -414,7 +417,7 @@ func WithDSNMailReturnType(option DSNMailReturnOption) Option {
return ErrInvalidDSNMailReturnOption
}
c.dsn = true
c.requestDSN = true
c.dsnmrtype = option
return nil
}
@ -448,7 +451,7 @@ func WithDSNRcptNotifyType(opts ...DSNRcptNotifyOption) Option {
return ErrInvalidDSNRcptNotifyCombination
}
c.dsn = true
c.requestDSN = true
c.dsnrntype = rcptOpts
return nil
}
@ -870,7 +873,7 @@ func (c *Client) sendSingleMsg(message *Msg) error {
}
}
if c.dsn {
if c.requestDSN {
if c.dsnmrtype != "" {
c.smtpClient.SetDSNMailReturnOption(string(c.dsnmrtype))
}

View file

@ -483,8 +483,8 @@ func TestWithDSN(t *testing.T) {
t.Errorf("failed to create new client: %s", err)
return
}
if !c.dsn {
t.Errorf("WithDSN failed. c.dsn expected to be: %t, got: %t", true, c.dsn)
if !c.requestDSN {
t.Errorf("WithDSN failed. c.requestDSN expected to be: %t, got: %t", true, c.requestDSN)
}
if c.dsnmrtype != DSNMailReturnFull {
t.Errorf("WithDSN failed. c.dsnmrtype expected to be: %s, got: %s", DSNMailReturnFull,