mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-15 02:12:55 +01:00
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:
parent
92c411454b
commit
ef3da39840
2 changed files with 75 additions and 72 deletions
61
client.go
61
client.go
|
@ -40,19 +40,6 @@ const (
|
||||||
DefaultTLSMinVersion = tls.VersionTLS12
|
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 (
|
const (
|
||||||
|
|
||||||
// DSNMailReturnHeadersOnly requests that only the message headers of the mail message are returned in
|
// DSNMailReturnHeadersOnly requests that only the message headers of the mail message are returned in
|
||||||
|
@ -90,20 +77,35 @@ const (
|
||||||
DSNRcptNotifyDelay DSNRcptNotifyOption = "DELAY"
|
DSNRcptNotifyDelay DSNRcptNotifyOption = "DELAY"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DialContextFunc is a type to define custom DialContext function.
|
type (
|
||||||
type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error)
|
|
||||||
|
|
||||||
// Client is the SMTP client struct
|
// DialContextFunc defines a function type for establishing a network connection using context, network
|
||||||
type Client struct {
|
// type, and address. It is used to specify custom DialContext function.
|
||||||
// Timeout for the SMTP server connection
|
//
|
||||||
|
// 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
|
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
|
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 defines the DSNMailReturnOption in case DSN is enabled
|
||||||
dsnmrtype DSNMailReturnOption
|
dsnmrtype DSNMailReturnOption
|
||||||
|
|
||||||
|
@ -139,6 +141,9 @@ type Client struct {
|
||||||
// port specifies the network port number on which the server listens for incoming connections.
|
// port specifies the network port number on which the server listens for incoming connections.
|
||||||
port int
|
port int
|
||||||
|
|
||||||
|
// requestDSN indicates that we want to use DSN for the Client
|
||||||
|
requestDSN bool
|
||||||
|
|
||||||
// smtpAuth is a pointer to smtp.Auth
|
// smtpAuth is a pointer to smtp.Auth
|
||||||
smtpAuth smtp.Auth
|
smtpAuth smtp.Auth
|
||||||
|
|
||||||
|
@ -163,9 +168,7 @@ type Client struct {
|
||||||
// Use SSL for the connection
|
// Use SSL for the connection
|
||||||
useSSL bool
|
useSSL bool
|
||||||
}
|
}
|
||||||
|
)
|
||||||
// Option returns a function that can be used for grouping Client options
|
|
||||||
type Option func(*Client) error
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrInvalidPort should be used if a port is specified that is not valid
|
// ErrInvalidPort should be used if a port is specified that is not valid
|
||||||
|
@ -394,7 +397,7 @@ func WithPassword(password string) Option {
|
||||||
// and DSNRcptNotifyFailure
|
// and DSNRcptNotifyFailure
|
||||||
func WithDSN() Option {
|
func WithDSN() Option {
|
||||||
return func(c *Client) error {
|
return func(c *Client) error {
|
||||||
c.dsn = true
|
c.requestDSN = true
|
||||||
c.dsnmrtype = DSNMailReturnFull
|
c.dsnmrtype = DSNMailReturnFull
|
||||||
c.dsnrntype = []string{string(DSNRcptNotifyFailure), string(DSNRcptNotifySuccess)}
|
c.dsnrntype = []string{string(DSNRcptNotifyFailure), string(DSNRcptNotifySuccess)}
|
||||||
return nil
|
return nil
|
||||||
|
@ -414,7 +417,7 @@ func WithDSNMailReturnType(option DSNMailReturnOption) Option {
|
||||||
return ErrInvalidDSNMailReturnOption
|
return ErrInvalidDSNMailReturnOption
|
||||||
}
|
}
|
||||||
|
|
||||||
c.dsn = true
|
c.requestDSN = true
|
||||||
c.dsnmrtype = option
|
c.dsnmrtype = option
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -448,7 +451,7 @@ func WithDSNRcptNotifyType(opts ...DSNRcptNotifyOption) Option {
|
||||||
return ErrInvalidDSNRcptNotifyCombination
|
return ErrInvalidDSNRcptNotifyCombination
|
||||||
}
|
}
|
||||||
|
|
||||||
c.dsn = true
|
c.requestDSN = true
|
||||||
c.dsnrntype = rcptOpts
|
c.dsnrntype = rcptOpts
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -870,7 +873,7 @@ func (c *Client) sendSingleMsg(message *Msg) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.dsn {
|
if c.requestDSN {
|
||||||
if c.dsnmrtype != "" {
|
if c.dsnmrtype != "" {
|
||||||
c.smtpClient.SetDSNMailReturnOption(string(c.dsnmrtype))
|
c.smtpClient.SetDSNMailReturnOption(string(c.dsnmrtype))
|
||||||
}
|
}
|
||||||
|
|
|
@ -483,8 +483,8 @@ func TestWithDSN(t *testing.T) {
|
||||||
t.Errorf("failed to create new client: %s", err)
|
t.Errorf("failed to create new client: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !c.dsn {
|
if !c.requestDSN {
|
||||||
t.Errorf("WithDSN failed. c.dsn expected to be: %t, got: %t", true, c.dsn)
|
t.Errorf("WithDSN failed. c.requestDSN expected to be: %t, got: %t", true, c.requestDSN)
|
||||||
}
|
}
|
||||||
if c.dsnmrtype != DSNMailReturnFull {
|
if c.dsnmrtype != DSNMailReturnFull {
|
||||||
t.Errorf("WithDSN failed. c.dsnmrtype expected to be: %s, got: %s", DSNMailReturnFull,
|
t.Errorf("WithDSN failed. c.dsnmrtype expected to be: %s, got: %s", DSNMailReturnFull,
|
||||||
|
|
Loading…
Reference in a new issue