Refactor variable names in SMTP client code

The commit introduces improved variable names in the SMTP client code that are more expressive and better describe the function they perform. This is combined with corrections to some code comments. It's aimed to increase code readability and maintainability. Updates are made across the client_119.go and client.go files to ensure consistent naming conventions.
This commit is contained in:
Winni Neessen 2024-02-24 22:06:18 +01:00
parent 12a145f612
commit e1098091c3
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
4 changed files with 191 additions and 191 deletions

284
client.go
View file

@ -87,11 +87,11 @@ type DialContextFunc func(ctx context.Context, network, address string) (net.Con
// Client is the SMTP client struct // Client is the SMTP client struct
type Client struct { type Client struct {
// co is the net.Conn that the smtp.Client is based on // connection is the net.Conn that the smtp.Client is based on
co net.Conn connection net.Conn
// Timeout for the SMTP server connection // Timeout for the SMTP server connection
cto time.Duration connTimeout time.Duration
// dsn indicates that we want to use DSN for the Client // dsn indicates that we want to use DSN for the Client
dsn bool dsn bool
@ -102,8 +102,8 @@ type Client struct {
// dsnrntype defines the DSNRcptNotifyOption in case DSN is enabled // dsnrntype defines the DSNRcptNotifyOption in case DSN is enabled
dsnrntype []string dsnrntype []string
// enc indicates if a Client connection is encrypted or not // isEncrypted indicates if a Client connection is encrypted or not
enc bool isEncrypted bool
// noNoop indicates the Noop is to be skipped // noNoop indicates the Noop is to be skipped
noNoop bool noNoop bool
@ -121,17 +121,17 @@ type Client struct {
port int port int
fallbackPort int fallbackPort int
// sa is a pointer to smtp.Auth // smtpAuth is a pointer to smtp.Auth
sa smtp.Auth smtpAuth smtp.Auth
// satype represents the authentication type for SMTP AUTH // smtpAuthType represents the authentication type for SMTP AUTH
satype SMTPAuthType smtpAuthType SMTPAuthType
// sc is the smtp.Client that is set up when using the Dial*() methods // smtpClient is the smtp.Client that is set up when using the Dial*() methods
sc *smtp.Client smtpClient *smtp.Client
// Use SSL for the connection // Use SSL for the connection
ssl bool useSSL bool
// tlspolicy sets the client to use the provided TLSPolicy for the STARTTLS protocol // tlspolicy sets the client to use the provided TLSPolicy for the STARTTLS protocol
tlspolicy TLSPolicy tlspolicy TLSPolicy
@ -142,11 +142,11 @@ type Client struct {
// user is the SMTP AUTH username // user is the SMTP AUTH username
user string user string
// dl enables the debug logging on the SMTP client // useDebugLog enables the debug logging on the SMTP client
dl bool useDebugLog bool
// l is a logger that implements the log.Logger interface // logger is a logger that implements the log.Logger interface
l log.Logger logger log.Logger
// dialContextFunc is a custom DialContext function to dial target SMTP server // dialContextFunc is a custom DialContext function to dial target SMTP server
dialContextFunc DialContextFunc dialContextFunc DialContextFunc
@ -198,13 +198,13 @@ var (
) )
// NewClient returns a new Session client object // NewClient returns a new Session client object
func NewClient(h string, o ...Option) (*Client, error) { func NewClient(host string, opts ...Option) (*Client, error) {
c := &Client{ c := &Client{
cto: DefaultTimeout, connTimeout: DefaultTimeout,
host: h, host: host,
port: DefaultPort, port: DefaultPort,
tlsconfig: &tls.Config{ServerName: h, MinVersion: DefaultTLSMinVersion}, tlsconfig: &tls.Config{ServerName: host, MinVersion: DefaultTLSMinVersion},
tlspolicy: DefaultTLSPolicy, tlspolicy: DefaultTLSPolicy,
} }
// Set default HELO/EHLO hostname // Set default HELO/EHLO hostname
@ -213,11 +213,11 @@ func NewClient(h string, o ...Option) (*Client, error) {
} }
// Override defaults with optionally provided Option functions // Override defaults with optionally provided Option functions
for _, co := range o { for _, opt := range opts {
if co == nil { if opt == nil {
continue continue
} }
if err := co(c); err != nil { if err := opt(c); err != nil {
return c, fmt.Errorf("failed to apply option: %w", err) return c, fmt.Errorf("failed to apply option: %w", err)
} }
} }
@ -231,23 +231,23 @@ func NewClient(h string, o ...Option) (*Client, error) {
} }
// WithPort overrides the default connection port // WithPort overrides the default connection port
func WithPort(p int) Option { func WithPort(port int) Option {
return func(c *Client) error { return func(c *Client) error {
if p < 1 || p > 65535 { if port < 1 || port > 65535 {
return ErrInvalidPort return ErrInvalidPort
} }
c.port = p c.port = port
return nil return nil
} }
} }
// WithTimeout overrides the default connection timeout // WithTimeout overrides the default connection timeout
func WithTimeout(t time.Duration) Option { func WithTimeout(timeout time.Duration) Option {
return func(c *Client) error { return func(c *Client) error {
if t <= 0 { if timeout <= 0 {
return ErrInvalidTimeout return ErrInvalidTimeout
} }
c.cto = t c.connTimeout = timeout
return nil return nil
} }
} }
@ -257,7 +257,7 @@ func WithTimeout(t time.Duration) Option {
// Deprecated: use WithSSLPort instead. // Deprecated: use WithSSLPort instead.
func WithSSL() Option { func WithSSL() Option {
return func(c *Client) error { return func(c *Client) error {
c.ssl = true c.useSSL = true
return nil return nil
} }
} }
@ -267,9 +267,9 @@ func WithSSL() Option {
// //
// When the SSL connection fails and fallback is set to true, // When the SSL connection fails and fallback is set to true,
// the client will attempt to connect on port 25 using plaintext. // the client will attempt to connect on port 25 using plaintext.
func WithSSLPort(fb bool) Option { func WithSSLPort(fallback bool) Option {
return func(c *Client) error { return func(c *Client) error {
c.SetSSLPort(true, fb) c.SetSSLPort(true, fallback)
return nil return nil
} }
} }
@ -278,26 +278,26 @@ func WithSSLPort(fb bool) Option {
// to StdErr // to StdErr
func WithDebugLog() Option { func WithDebugLog() Option {
return func(c *Client) error { return func(c *Client) error {
c.dl = true c.useDebugLog = true
return nil return nil
} }
} }
// WithLogger overrides the default log.Logger that is used for debug logging // WithLogger overrides the default log.Logger that is used for debug logging
func WithLogger(l log.Logger) Option { func WithLogger(logger log.Logger) Option {
return func(c *Client) error { return func(c *Client) error {
c.l = l c.logger = logger
return nil return nil
} }
} }
// WithHELO tells the client to use the provided string as HELO/EHLO greeting host // WithHELO tells the client to use the provided string as HELO/EHLO greeting host
func WithHELO(h string) Option { func WithHELO(helo string) Option {
return func(c *Client) error { return func(c *Client) error {
if h == "" { if helo == "" {
return ErrInvalidHELO return ErrInvalidHELO
} }
c.helo = h c.helo = helo
return nil return nil
} }
} }
@ -305,9 +305,9 @@ func WithHELO(h string) Option {
// WithTLSPolicy tells the client to use the provided TLSPolicy // WithTLSPolicy tells the client to use the provided TLSPolicy
// //
// Deprecated: use WithTLSPortPolicy instead. // Deprecated: use WithTLSPortPolicy instead.
func WithTLSPolicy(p TLSPolicy) Option { func WithTLSPolicy(policy TLSPolicy) Option {
return func(c *Client) error { return func(c *Client) error {
c.tlspolicy = p c.tlspolicy = policy
return nil return nil
} }
} }
@ -319,52 +319,52 @@ func WithTLSPolicy(p TLSPolicy) Option {
// If the connection fails with TLSOpportunistic, // If the connection fails with TLSOpportunistic,
// a plaintext connection is attempted on port 25 as a fallback. // a plaintext connection is attempted on port 25 as a fallback.
// NoTLS will allways use port 25. // NoTLS will allways use port 25.
func WithTLSPortPolicy(p TLSPolicy) Option { func WithTLSPortPolicy(policy TLSPolicy) Option {
return func(c *Client) error { return func(c *Client) error {
c.SetTLSPortPolicy(p) c.SetTLSPortPolicy(policy)
return nil return nil
} }
} }
// WithTLSConfig tells the client to use the provided *tls.Config // WithTLSConfig tells the client to use the provided *tls.Config
func WithTLSConfig(co *tls.Config) Option { func WithTLSConfig(tlsconfig *tls.Config) Option {
return func(c *Client) error { return func(c *Client) error {
if co == nil { if tlsconfig == nil {
return ErrInvalidTLSConfig return ErrInvalidTLSConfig
} }
c.tlsconfig = co c.tlsconfig = tlsconfig
return nil return nil
} }
} }
// WithSMTPAuth tells the client to use the provided SMTPAuthType for authentication // WithSMTPAuth tells the client to use the provided SMTPAuthType for authentication
func WithSMTPAuth(t SMTPAuthType) Option { func WithSMTPAuth(authtype SMTPAuthType) Option {
return func(c *Client) error { return func(c *Client) error {
c.satype = t c.smtpAuthType = authtype
return nil return nil
} }
} }
// WithSMTPAuthCustom tells the client to use the provided smtp.Auth for SMTP authentication // WithSMTPAuthCustom tells the client to use the provided smtp.Auth for SMTP authentication
func WithSMTPAuthCustom(a smtp.Auth) Option { func WithSMTPAuthCustom(smtpAuth smtp.Auth) Option {
return func(c *Client) error { return func(c *Client) error {
c.sa = a c.smtpAuth = smtpAuth
return nil return nil
} }
} }
// WithUsername tells the client to use the provided string as username for authentication // WithUsername tells the client to use the provided string as username for authentication
func WithUsername(u string) Option { func WithUsername(username string) Option {
return func(c *Client) error { return func(c *Client) error {
c.user = u c.user = username
return nil return nil
} }
} }
// WithPassword tells the client to use the provided string as password/secret for authentication // WithPassword tells the client to use the provided string as password/secret for authentication
func WithPassword(p string) Option { func WithPassword(password string) Option {
return func(c *Client) error { return func(c *Client) error {
c.pass = p c.pass = password
return nil return nil
} }
} }
@ -386,9 +386,9 @@ func WithDSN() Option {
// as described in the RFC 1891 and set the MAIL FROM Return option type to the // as described in the RFC 1891 and set the MAIL FROM Return option type to the
// given DSNMailReturnOption // given DSNMailReturnOption
// See: https://www.rfc-editor.org/rfc/rfc1891 // See: https://www.rfc-editor.org/rfc/rfc1891
func WithDSNMailReturnType(mro DSNMailReturnOption) Option { func WithDSNMailReturnType(option DSNMailReturnOption) Option {
return func(c *Client) error { return func(c *Client) error {
switch mro { switch option {
case DSNMailReturnHeadersOnly: case DSNMailReturnHeadersOnly:
case DSNMailReturnFull: case DSNMailReturnFull:
default: default:
@ -396,7 +396,7 @@ func WithDSNMailReturnType(mro DSNMailReturnOption) Option {
} }
c.dsn = true c.dsn = true
c.dsnmrtype = mro c.dsnmrtype = option
return nil return nil
} }
} }
@ -404,13 +404,13 @@ func WithDSNMailReturnType(mro DSNMailReturnOption) Option {
// WithDSNRcptNotifyType enables the Client to request DSNs as described in the RFC 1891 // WithDSNRcptNotifyType enables the Client to request DSNs as described in the RFC 1891
// and sets the RCPT TO notify options to the given list of DSNRcptNotifyOption // and sets the RCPT TO notify options to the given list of DSNRcptNotifyOption
// See: https://www.rfc-editor.org/rfc/rfc1891 // See: https://www.rfc-editor.org/rfc/rfc1891
func WithDSNRcptNotifyType(rno ...DSNRcptNotifyOption) Option { func WithDSNRcptNotifyType(opts ...DSNRcptNotifyOption) Option {
return func(c *Client) error { return func(c *Client) error {
var rnol []string var rcptOpts []string
var ns, nns bool var ns, nns bool
if len(rno) > 0 { if len(opts) > 0 {
for _, crno := range rno { for _, opt := range opts {
switch crno { switch opt {
case DSNRcptNotifyNever: case DSNRcptNotifyNever:
ns = true ns = true
case DSNRcptNotifySuccess: case DSNRcptNotifySuccess:
@ -422,7 +422,7 @@ func WithDSNRcptNotifyType(rno ...DSNRcptNotifyOption) Option {
default: default:
return ErrInvalidDSNRcptNotifyOption return ErrInvalidDSNRcptNotifyOption
} }
rnol = append(rnol, string(crno)) rcptOpts = append(rcptOpts, string(opt))
} }
} }
if ns && nns { if ns && nns {
@ -430,7 +430,7 @@ func WithDSNRcptNotifyType(rno ...DSNRcptNotifyOption) Option {
} }
c.dsn = true c.dsn = true
c.dsnrntype = rnol c.dsnrntype = rcptOpts
return nil return nil
} }
} }
@ -445,9 +445,9 @@ func WithoutNoop() Option {
} }
// WithDialContextFunc overrides the default DialContext for connecting SMTP server // WithDialContextFunc overrides the default DialContext for connecting SMTP server
func WithDialContextFunc(f DialContextFunc) Option { func WithDialContextFunc(dialCtxFunc DialContextFunc) Option {
return func(c *Client) error { return func(c *Client) error {
c.dialContextFunc = f c.dialContextFunc = dialCtxFunc
return nil return nil
} }
} }
@ -463,8 +463,8 @@ func (c *Client) ServerAddr() string {
} }
// SetTLSPolicy overrides the current TLSPolicy with the given TLSPolicy value // SetTLSPolicy overrides the current TLSPolicy with the given TLSPolicy value
func (c *Client) SetTLSPolicy(p TLSPolicy) { func (c *Client) SetTLSPolicy(policy TLSPolicy) {
c.tlspolicy = p c.tlspolicy = policy
} }
// SetTLSPortPolicy overrides the current TLSPolicy with the given TLSPolicy // SetTLSPortPolicy overrides the current TLSPolicy with the given TLSPolicy
@ -474,22 +474,22 @@ func (c *Client) SetTLSPolicy(p TLSPolicy) {
// If the connection fails with TLSOpportunistic, a plaintext connection is // If the connection fails with TLSOpportunistic, a plaintext connection is
// attempted on port 25 as a fallback. // attempted on port 25 as a fallback.
// NoTLS will allways use port 25. // NoTLS will allways use port 25.
func (c *Client) SetTLSPortPolicy(p TLSPolicy) { func (c *Client) SetTLSPortPolicy(policy TLSPolicy) {
c.port = DefaultPortTLS c.port = DefaultPortTLS
if p == TLSOpportunistic { if policy == TLSOpportunistic {
c.fallbackPort = DefaultPort c.fallbackPort = DefaultPort
} }
if p == NoTLS { if policy == NoTLS {
c.port = DefaultPort c.port = DefaultPort
} }
c.tlspolicy = p c.tlspolicy = policy
} }
// SetSSL tells the Client wether to use SSL or not // SetSSL tells the Client wether to use SSL or not
func (c *Client) SetSSL(s bool) { func (c *Client) SetSSL(ssl bool) {
c.ssl = s c.useSSL = ssl
} }
// SetSSLPort tells the Client wether or not to use SSL and fallback. // SetSSLPort tells the Client wether or not to use SSL and fallback.
@ -499,124 +499,124 @@ func (c *Client) SetSSL(s bool) {
// Port 25 is used when SSL is unset (false). // Port 25 is used when SSL is unset (false).
// When the SSL connection fails and fb is set to true, // When the SSL connection fails and fb is set to true,
// the client will attempt to connect on port 25 using plaintext. // the client will attempt to connect on port 25 using plaintext.
func (c *Client) SetSSLPort(ssl bool, fb bool) { func (c *Client) SetSSLPort(ssl bool, fallback bool) {
c.port = DefaultPort c.port = DefaultPort
if ssl { if ssl {
c.port = DefaultPortSSL c.port = DefaultPortSSL
} }
c.fallbackPort = 0 c.fallbackPort = 0
if fb { if fallback {
c.fallbackPort = DefaultPort c.fallbackPort = DefaultPort
} }
c.ssl = ssl c.useSSL = ssl
} }
// SetDebugLog tells the Client whether debug logging is enabled or not // SetDebugLog tells the Client whether debug logging is enabled or not
func (c *Client) SetDebugLog(v bool) { func (c *Client) SetDebugLog(val bool) {
c.dl = v c.useDebugLog = val
if c.sc != nil { if c.smtpClient != nil {
c.sc.SetDebugLog(v) c.smtpClient.SetDebugLog(val)
} }
} }
// SetLogger tells the Client which log.Logger to use // SetLogger tells the Client which log.Logger to use
func (c *Client) SetLogger(l log.Logger) { func (c *Client) SetLogger(logger log.Logger) {
c.l = l c.logger = logger
if c.sc != nil { if c.smtpClient != nil {
c.sc.SetLogger(l) c.smtpClient.SetLogger(logger)
} }
} }
// SetTLSConfig overrides the current *tls.Config with the given *tls.Config value // SetTLSConfig overrides the current *tls.Config with the given *tls.Config value
func (c *Client) SetTLSConfig(co *tls.Config) error { func (c *Client) SetTLSConfig(tlsconfig *tls.Config) error {
if co == nil { if tlsconfig == nil {
return ErrInvalidTLSConfig return ErrInvalidTLSConfig
} }
c.tlsconfig = co c.tlsconfig = tlsconfig
return nil return nil
} }
// SetUsername overrides the current username string with the given value // SetUsername overrides the current username string with the given value
func (c *Client) SetUsername(u string) { func (c *Client) SetUsername(username string) {
c.user = u c.user = username
} }
// SetPassword overrides the current password string with the given value // SetPassword overrides the current password string with the given value
func (c *Client) SetPassword(p string) { func (c *Client) SetPassword(password string) {
c.pass = p c.pass = password
} }
// SetSMTPAuth overrides the current SMTP AUTH type setting with the given value // SetSMTPAuth overrides the current SMTP AUTH type setting with the given value
func (c *Client) SetSMTPAuth(a SMTPAuthType) { func (c *Client) SetSMTPAuth(authtype SMTPAuthType) {
c.satype = a c.smtpAuthType = authtype
} }
// SetSMTPAuthCustom overrides the current SMTP AUTH setting with the given custom smtp.Auth // SetSMTPAuthCustom overrides the current SMTP AUTH setting with the given custom smtp.Auth
func (c *Client) SetSMTPAuthCustom(sa smtp.Auth) { func (c *Client) SetSMTPAuthCustom(smtpAuth smtp.Auth) {
c.sa = sa c.smtpAuth = smtpAuth
} }
// setDefaultHelo retrieves the current hostname and sets it as HELO/EHLO hostname // setDefaultHelo retrieves the current hostname and sets it as HELO/EHLO hostname
func (c *Client) setDefaultHelo() error { func (c *Client) setDefaultHelo() error {
hn, err := os.Hostname() hostname, err := os.Hostname()
if err != nil { if err != nil {
return fmt.Errorf("failed cto read local hostname: %w", err) return fmt.Errorf("failed to read local hostname: %w", err)
} }
c.helo = hn c.helo = hostname
return nil return nil
} }
// DialWithContext establishes a connection cto the SMTP server with a given context.Context // DialWithContext establishes a connection to the SMTP server with a given context.Context
func (c *Client) DialWithContext(pc context.Context) error { func (c *Client) DialWithContext(dialCtx context.Context) error {
ctx, cfn := context.WithDeadline(pc, time.Now().Add(c.cto)) ctx, cancel := context.WithDeadline(dialCtx, time.Now().Add(c.connTimeout))
defer cfn() defer cancel()
if c.dialContextFunc == nil { if c.dialContextFunc == nil {
nd := net.Dialer{} netDialer := net.Dialer{}
c.dialContextFunc = nd.DialContext c.dialContextFunc = netDialer.DialContext
if c.ssl { if c.useSSL {
td := tls.Dialer{NetDialer: &nd, Config: c.tlsconfig} tlsDialer := tls.Dialer{NetDialer: &netDialer, Config: c.tlsconfig}
c.enc = true c.isEncrypted = true
c.dialContextFunc = td.DialContext c.dialContextFunc = tlsDialer.DialContext
} }
} }
var err error var err error
c.co, err = c.dialContextFunc(ctx, "tcp", c.ServerAddr()) c.connection, err = c.dialContextFunc(ctx, "tcp", c.ServerAddr())
if err != nil && c.fallbackPort != 0 { if err != nil && c.fallbackPort != 0 {
// TODO: should we somehow log or append the previous error? // TODO: should we somehow log or append the previous error?
c.co, err = c.dialContextFunc(ctx, "tcp", c.serverFallbackAddr()) c.connection, err = c.dialContextFunc(ctx, "tcp", c.serverFallbackAddr())
} }
if err != nil { if err != nil {
return err return err
} }
sc, err := smtp.NewClient(c.co, c.host) client, err := smtp.NewClient(c.connection, c.host)
if err != nil { if err != nil {
return err return err
} }
if sc == nil { if client == nil {
return fmt.Errorf("SMTP client is nil") return fmt.Errorf("SMTP client is nil")
} }
c.sc = sc c.smtpClient = client
if c.l != nil { if c.logger != nil {
c.sc.SetLogger(c.l) c.smtpClient.SetLogger(c.logger)
} }
if c.dl { if c.useDebugLog {
c.sc.SetDebugLog(true) c.smtpClient.SetDebugLog(true)
} }
if err := c.sc.Hello(c.helo); err != nil { if err = c.smtpClient.Hello(c.helo); err != nil {
return err return err
} }
if err := c.tls(); err != nil { if err = c.tls(); err != nil {
return err return err
} }
if err := c.auth(); err != nil { if err = c.auth(); err != nil {
return err return err
} }
@ -628,7 +628,7 @@ func (c *Client) Close() error {
if err := c.checkConn(); err != nil { if err := c.checkConn(); err != nil {
return err return err
} }
if err := c.sc.Quit(); err != nil { if err := c.smtpClient.Quit(); err != nil {
return fmt.Errorf("failed to close SMTP client: %w", err) return fmt.Errorf("failed to close SMTP client: %w", err)
} }
@ -640,7 +640,7 @@ func (c *Client) Reset() error {
if err := c.checkConn(); err != nil { if err := c.checkConn(); err != nil {
return err return err
} }
if err := c.sc.Reset(); err != nil { if err := c.smtpClient.Reset(); err != nil {
return fmt.Errorf("failed to send RSET to SMTP client: %w", err) return fmt.Errorf("failed to send RSET to SMTP client: %w", err)
} }
@ -672,17 +672,17 @@ func (c *Client) DialAndSendWithContext(ctx context.Context, ml ...*Msg) error {
// checkConn makes sure that a required server connection is available and extends the // checkConn makes sure that a required server connection is available and extends the
// connection deadline // connection deadline
func (c *Client) checkConn() error { func (c *Client) checkConn() error {
if c.co == nil { if c.connection == nil {
return ErrNoActiveConnection return ErrNoActiveConnection
} }
if !c.noNoop { if !c.noNoop {
if err := c.sc.Noop(); err != nil { if err := c.smtpClient.Noop(); err != nil {
return ErrNoActiveConnection return ErrNoActiveConnection
} }
} }
if err := c.co.SetDeadline(time.Now().Add(c.cto)); err != nil { if err := c.connection.SetDeadline(time.Now().Add(c.connTimeout)); err != nil {
return ErrDeadlineExtendFailed return ErrDeadlineExtendFailed
} }
return nil return nil
@ -696,12 +696,12 @@ func (c *Client) serverFallbackAddr() string {
// tls tries to make sure that the STARTTLS requirements are satisfied // tls tries to make sure that the STARTTLS requirements are satisfied
func (c *Client) tls() error { func (c *Client) tls() error {
if c.co == nil { if c.connection == nil {
return ErrNoActiveConnection return ErrNoActiveConnection
} }
if !c.ssl && c.tlspolicy != NoTLS { if !c.useSSL && c.tlspolicy != NoTLS {
est := false est := false
st, _ := c.sc.Extension("STARTTLS") st, _ := c.smtpClient.Extension("STARTTLS")
if c.tlspolicy == TLSMandatory { if c.tlspolicy == TLSMandatory {
est = true est = true
if !st { if !st {
@ -715,11 +715,11 @@ func (c *Client) tls() error {
} }
} }
if est { if est {
if err := c.sc.StartTLS(c.tlsconfig); err != nil { if err := c.smtpClient.StartTLS(c.tlsconfig); err != nil {
return err return err
} }
} }
_, c.enc = c.sc.TLSConnectionState() _, c.isEncrypted = c.smtpClient.TLSConnectionState()
} }
return nil return nil
} }
@ -729,40 +729,40 @@ func (c *Client) auth() error {
if err := c.checkConn(); err != nil { if err := c.checkConn(); err != nil {
return fmt.Errorf("failed to authenticate: %w", err) return fmt.Errorf("failed to authenticate: %w", err)
} }
if c.sa == nil && c.satype != "" { if c.smtpAuth == nil && c.smtpAuthType != "" {
sa, sat := c.sc.Extension("AUTH") sa, sat := c.smtpClient.Extension("AUTH")
if !sa { if !sa {
return fmt.Errorf("server does not support SMTP AUTH") return fmt.Errorf("server does not support SMTP AUTH")
} }
switch c.satype { switch c.smtpAuthType {
case SMTPAuthPlain: case SMTPAuthPlain:
if !strings.Contains(sat, string(SMTPAuthPlain)) { if !strings.Contains(sat, string(SMTPAuthPlain)) {
return ErrPlainAuthNotSupported return ErrPlainAuthNotSupported
} }
c.sa = smtp.PlainAuth("", c.user, c.pass, c.host) c.smtpAuth = smtp.PlainAuth("", c.user, c.pass, c.host)
case SMTPAuthLogin: case SMTPAuthLogin:
if !strings.Contains(sat, string(SMTPAuthLogin)) { if !strings.Contains(sat, string(SMTPAuthLogin)) {
return ErrLoginAuthNotSupported return ErrLoginAuthNotSupported
} }
c.sa = smtp.LoginAuth(c.user, c.pass, c.host) c.smtpAuth = smtp.LoginAuth(c.user, c.pass, c.host)
case SMTPAuthCramMD5: case SMTPAuthCramMD5:
if !strings.Contains(sat, string(SMTPAuthCramMD5)) { if !strings.Contains(sat, string(SMTPAuthCramMD5)) {
return ErrCramMD5AuthNotSupported return ErrCramMD5AuthNotSupported
} }
c.sa = smtp.CRAMMD5Auth(c.user, c.pass) c.smtpAuth = smtp.CRAMMD5Auth(c.user, c.pass)
case SMTPAuthXOAUTH2: case SMTPAuthXOAUTH2:
if !strings.Contains(sat, string(SMTPAuthXOAUTH2)) { if !strings.Contains(sat, string(SMTPAuthXOAUTH2)) {
return ErrXOauth2AuthNotSupported return ErrXOauth2AuthNotSupported
} }
c.sa = smtp.XOAuth2Auth(c.user, c.pass) c.smtpAuth = smtp.XOAuth2Auth(c.user, c.pass)
default: default:
return fmt.Errorf("unsupported SMTP AUTH type %q", c.satype) return fmt.Errorf("unsupported SMTP AUTH type %q", c.smtpAuthType)
} }
} }
if c.sa != nil { if c.smtpAuth != nil {
if err := c.sc.Auth(c.sa); err != nil { if err := c.smtpClient.Auth(c.smtpAuth); err != nil {
return fmt.Errorf("SMTP AUTH failed: %w", err) return fmt.Errorf("SMTP AUTH failed: %w", err)
} }
} }

View file

@ -18,7 +18,7 @@ func (c *Client) Send(ml ...*Msg) error {
for _, m := range ml { for _, m := range ml {
m.sendError = nil m.sendError = nil
if m.encoding == NoEncoding { if m.encoding == NoEncoding {
if ok, _ := c.sc.Extension("8BITMIME"); !ok { if ok, _ := c.smtpClient.Extension("8BITMIME"); !ok {
se := &SendError{Reason: ErrNoUnencoded, isTemp: false} se := &SendError{Reason: ErrNoUnencoded, isTemp: false}
m.sendError = se m.sendError = se
errs = append(errs, se) errs = append(errs, se)
@ -42,12 +42,12 @@ func (c *Client) Send(ml ...*Msg) error {
if c.dsn { if c.dsn {
if c.dsnmrtype != "" { if c.dsnmrtype != "" {
c.sc.SetDSNMailReturnOption(string(c.dsnmrtype)) c.smtpClient.SetDSNMailReturnOption(string(c.dsnmrtype))
} }
} }
if err := c.sc.Mail(f); err != nil { if err := c.smtpClient.Mail(f); err != nil {
se := &SendError{Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err)} se := &SendError{Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err)}
if reserr := c.sc.Reset(); reserr != nil { if reserr := c.smtpClient.Reset(); reserr != nil {
se.errlist = append(se.errlist, reserr) se.errlist = append(se.errlist, reserr)
} }
m.sendError = se m.sendError = se
@ -59,9 +59,9 @@ func (c *Client) Send(ml ...*Msg) error {
rse.errlist = make([]error, 0) rse.errlist = make([]error, 0)
rse.rcpt = make([]string, 0) rse.rcpt = make([]string, 0)
rno := strings.Join(c.dsnrntype, ",") rno := strings.Join(c.dsnrntype, ",")
c.sc.SetDSNRcptNotifyOption(rno) c.smtpClient.SetDSNRcptNotifyOption(rno)
for _, r := range rl { for _, r := range rl {
if err := c.sc.Rcpt(r); err != nil { if err := c.smtpClient.Rcpt(r); err != nil {
rse.Reason = ErrSMTPRcptTo rse.Reason = ErrSMTPRcptTo
rse.errlist = append(rse.errlist, err) rse.errlist = append(rse.errlist, err)
rse.rcpt = append(rse.rcpt, r) rse.rcpt = append(rse.rcpt, r)
@ -70,14 +70,14 @@ func (c *Client) Send(ml ...*Msg) error {
} }
} }
if failed { if failed {
if reserr := c.sc.Reset(); reserr != nil { if reserr := c.smtpClient.Reset(); reserr != nil {
rse.errlist = append(rse.errlist, err) rse.errlist = append(rse.errlist, err)
} }
m.sendError = rse m.sendError = rse
errs = append(errs, rse) errs = append(errs, rse)
continue continue
} }
w, err := c.sc.Data() w, err := c.smtpClient.Data()
if err != nil { if err != nil {
se := &SendError{Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err)} se := &SendError{Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err)}
m.sendError = se m.sendError = se

View file

@ -21,7 +21,7 @@ func (c *Client) Send(ml ...*Msg) (rerr error) {
for _, m := range ml { for _, m := range ml {
m.sendError = nil m.sendError = nil
if m.encoding == NoEncoding { if m.encoding == NoEncoding {
if ok, _ := c.sc.Extension("8BITMIME"); !ok { if ok, _ := c.smtpClient.Extension("8BITMIME"); !ok {
m.sendError = &SendError{Reason: ErrNoUnencoded, isTemp: false} m.sendError = &SendError{Reason: ErrNoUnencoded, isTemp: false}
rerr = errors.Join(rerr, m.sendError) rerr = errors.Join(rerr, m.sendError)
continue continue
@ -42,13 +42,13 @@ func (c *Client) Send(ml ...*Msg) (rerr error) {
if c.dsn { if c.dsn {
if c.dsnmrtype != "" { if c.dsnmrtype != "" {
c.sc.SetDSNMailReturnOption(string(c.dsnmrtype)) c.smtpClient.SetDSNMailReturnOption(string(c.dsnmrtype))
} }
} }
if err := c.sc.Mail(f); err != nil { if err := c.smtpClient.Mail(f); err != nil {
m.sendError = &SendError{Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err)} m.sendError = &SendError{Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err)}
rerr = errors.Join(rerr, m.sendError) rerr = errors.Join(rerr, m.sendError)
if reserr := c.sc.Reset(); reserr != nil { if reserr := c.smtpClient.Reset(); reserr != nil {
rerr = errors.Join(rerr, reserr) rerr = errors.Join(rerr, reserr)
} }
continue continue
@ -58,9 +58,9 @@ func (c *Client) Send(ml ...*Msg) (rerr error) {
rse.errlist = make([]error, 0) rse.errlist = make([]error, 0)
rse.rcpt = make([]string, 0) rse.rcpt = make([]string, 0)
rno := strings.Join(c.dsnrntype, ",") rno := strings.Join(c.dsnrntype, ",")
c.sc.SetDSNRcptNotifyOption(rno) c.smtpClient.SetDSNRcptNotifyOption(rno)
for _, r := range rl { for _, r := range rl {
if err := c.sc.Rcpt(r); err != nil { if err := c.smtpClient.Rcpt(r); err != nil {
rse.Reason = ErrSMTPRcptTo rse.Reason = ErrSMTPRcptTo
rse.errlist = append(rse.errlist, err) rse.errlist = append(rse.errlist, err)
rse.rcpt = append(rse.rcpt, r) rse.rcpt = append(rse.rcpt, r)
@ -69,14 +69,14 @@ func (c *Client) Send(ml ...*Msg) (rerr error) {
} }
} }
if failed { if failed {
if reserr := c.sc.Reset(); reserr != nil { if reserr := c.smtpClient.Reset(); reserr != nil {
rerr = errors.Join(rerr, reserr) rerr = errors.Join(rerr, reserr)
} }
m.sendError = rse m.sendError = rse
rerr = errors.Join(rerr, m.sendError) rerr = errors.Join(rerr, m.sendError)
continue continue
} }
w, err := c.sc.Data() w, err := c.smtpClient.Data()
if err != nil { if err != nil {
m.sendError = &SendError{Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err)} m.sendError = &SendError{Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err)}
rerr = errors.Join(rerr, m.sendError) rerr = errors.Join(rerr, m.sendError)

View file

@ -49,9 +49,9 @@ func TestNewClient(t *testing.T) {
if c.host != tt.host { if c.host != tt.host {
t.Errorf("failed to create new client. Host expected: %s, got: %s", host, c.host) t.Errorf("failed to create new client. Host expected: %s, got: %s", host, c.host)
} }
if c.cto != DefaultTimeout { if c.connTimeout != DefaultTimeout {
t.Errorf("failed to create new client. Timeout expected: %s, got: %s", DefaultTimeout.String(), t.Errorf("failed to create new client. Timeout expected: %s, got: %s", DefaultTimeout.String(),
c.cto.String()) c.connTimeout.String())
} }
if c.port != DefaultPort { if c.port != DefaultPort {
t.Errorf("failed to create new client. Port expected: %d, got: %d", DefaultPort, c.port) t.Errorf("failed to create new client. Port expected: %d, got: %d", DefaultPort, c.port)
@ -205,8 +205,8 @@ func TestWithTimeout(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.cto != tt.want { if c.connTimeout != tt.want {
t.Errorf("failed to set custom timeout. Want: %d, got: %d", tt.want, c.cto) t.Errorf("failed to set custom timeout. Want: %d, got: %d", tt.want, c.connTimeout)
} }
}) })
} }
@ -345,8 +345,8 @@ func TestSetSSL(t *testing.T) {
return return
} }
c.SetSSL(tt.value) c.SetSSL(tt.value)
if c.ssl != tt.value { if c.useSSL != tt.value {
t.Errorf("failed to set SSL setting. Got: %t, want: %t", c.ssl, tt.value) t.Errorf("failed to set SSL setting. Got: %t, want: %t", c.useSSL, tt.value)
} }
}) })
} }
@ -374,8 +374,8 @@ func TestClient_SetSSLPort(t *testing.T) {
return return
} }
c.SetSSLPort(tt.value, tt.fb) c.SetSSLPort(tt.value, tt.fb)
if c.ssl != tt.value { if c.useSSL != tt.value {
t.Errorf("failed to set SSL setting. Got: %t, want: %t", c.ssl, tt.value) t.Errorf("failed to set SSL setting. Got: %t, want: %t", c.useSSL, tt.value)
} }
if c.port != tt.port { if c.port != tt.port {
t.Errorf("failed to set SSLPort, wanted port: %d, got: %d", c.port, tt.port) t.Errorf("failed to set SSLPort, wanted port: %d, got: %d", c.port, tt.port)
@ -460,8 +460,8 @@ func TestSetSMTPAuth(t *testing.T) {
return return
} }
c.SetSMTPAuth(tt.value) c.SetSMTPAuth(tt.value)
if string(c.satype) != tt.want { if string(c.smtpAuthType) != tt.want {
t.Errorf("failed to set SMTP auth type. Expected %s, got: %s", tt.want, string(c.satype)) t.Errorf("failed to set SMTP auth type. Expected %s, got: %s", tt.want, string(c.smtpAuthType))
} }
}) })
} }
@ -590,10 +590,10 @@ func TestSetSMTPAuthCustom(t *testing.T) {
return return
} }
c.SetSMTPAuthCustom(tt.value) c.SetSMTPAuthCustom(tt.value)
if c.sa == nil { if c.smtpAuth == nil {
t.Errorf("failed to set custom SMTP auth method. SMTP Auth method is empty") t.Errorf("failed to set custom SMTP auth method. SMTP Auth method is empty")
} }
p, _, err := c.sa.Start(&si) p, _, err := c.smtpAuth.Start(&si)
if err != nil { if err != nil {
t.Errorf("SMTP Auth Start() method returned error: %s", err) t.Errorf("SMTP Auth Start() method returned error: %s", err)
} }
@ -615,10 +615,10 @@ func TestClient_DialWithContext(t *testing.T) {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if c.co == nil { if c.connection == nil {
t.Errorf("DialWithContext didn't fail but no connection found.") t.Errorf("DialWithContext didn't fail but no connection found.")
} }
if c.sc == nil { if c.smtpClient == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
} }
if err := c.Close(); err != nil { if err := c.Close(); err != nil {
@ -640,10 +640,10 @@ func TestClient_DialWithContext_Fallback(t *testing.T) {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if c.co == nil { if c.connection == nil {
t.Errorf("DialWithContext didn't fail but no connection found.") t.Errorf("DialWithContext didn't fail but no connection found.")
} }
if c.sc == nil { if c.smtpClient == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
} }
if err := c.Close(); err != nil { if err := c.Close(); err != nil {
@ -670,10 +670,10 @@ func TestClient_DialWithContext_Debug(t *testing.T) {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if c.co == nil { if c.connection == nil {
t.Errorf("DialWithContext didn't fail but no connection found.") t.Errorf("DialWithContext didn't fail but no connection found.")
} }
if c.sc == nil { if c.smtpClient == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
} }
c.SetDebugLog(true) c.SetDebugLog(true)
@ -694,10 +694,10 @@ func TestClient_DialWithContext_Debug_custom(t *testing.T) {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if c.co == nil { if c.connection == nil {
t.Errorf("DialWithContext didn't fail but no connection found.") t.Errorf("DialWithContext didn't fail but no connection found.")
} }
if c.sc == nil { if c.smtpClient == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
} }
c.SetDebugLog(true) c.SetDebugLog(true)
@ -714,7 +714,7 @@ func TestClient_DialWithContextInvalidHost(t *testing.T) {
if err != nil { if err != nil {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
c.co = nil c.connection = nil
c.host = "invalid.addr" c.host = "invalid.addr"
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err == nil { if err := c.DialWithContext(ctx); err == nil {
@ -730,7 +730,7 @@ func TestClient_DialWithContextInvalidHELO(t *testing.T) {
if err != nil { if err != nil {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
c.co = nil c.connection = nil
c.helo = "" c.helo = ""
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err == nil { if err := c.DialWithContext(ctx); err == nil {
@ -762,7 +762,7 @@ func TestClient_checkConn(t *testing.T) {
if err != nil { if err != nil {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
c.co = nil c.connection = nil
if err := c.checkConn(); err == nil { if err := c.checkConn(); err == nil {
t.Errorf("connCheck() should fail but succeeded") t.Errorf("connCheck() should fail but succeeded")
} }
@ -799,10 +799,10 @@ func TestClient_DialWithContextOptions(t *testing.T) {
return return
} }
if !tt.sf { if !tt.sf {
if c.co == nil && !tt.sf { if c.connection == nil && !tt.sf {
t.Errorf("DialWithContext didn't fail but no connection found.") t.Errorf("DialWithContext didn't fail but no connection found.")
} }
if c.sc == nil && !tt.sf { if c.smtpClient == nil && !tt.sf {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
} }
if err := c.Reset(); err != nil { if err := c.Reset(); err != nil {
@ -1002,16 +1002,16 @@ func TestClient_DialSendCloseBroken(t *testing.T) {
return return
} }
if tt.closestart { if tt.closestart {
_ = c.sc.Close() _ = c.smtpClient.Close()
_ = c.co.Close() _ = c.connection.Close()
} }
if err := c.Send(m); err != nil && !tt.sf { if err := c.Send(m); err != nil && !tt.sf {
t.Errorf("Send() failed: %s", err) t.Errorf("Send() failed: %s", err)
return return
} }
if tt.closeearly { if tt.closeearly {
_ = c.sc.Close() _ = c.smtpClient.Close()
_ = c.co.Close() _ = c.connection.Close()
} }
if err := c.Close(); err != nil && !tt.sf { if err := c.Close(); err != nil && !tt.sf {
t.Errorf("Close() failed: %s", err) t.Errorf("Close() failed: %s", err)
@ -1062,16 +1062,16 @@ func TestClient_DialSendCloseBrokenWithDSN(t *testing.T) {
return return
} }
if tt.closestart { if tt.closestart {
_ = c.sc.Close() _ = c.smtpClient.Close()
_ = c.co.Close() _ = c.connection.Close()
} }
if err := c.Send(m); err != nil && !tt.sf { if err := c.Send(m); err != nil && !tt.sf {
t.Errorf("Send() failed: %s", err) t.Errorf("Send() failed: %s", err)
return return
} }
if tt.closeearly { if tt.closeearly {
_ = c.sc.Close() _ = c.smtpClient.Close()
_ = c.co.Close() _ = c.connection.Close()
} }
if err := c.Close(); err != nil && !tt.sf { if err := c.Close(); err != nil && !tt.sf {
t.Errorf("Close() failed: %s", err) t.Errorf("Close() failed: %s", err)