2023-01-15 16:14:19 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
|
2022-06-17 15:05:54 +02:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-05 12:36:53 +01:00
|
|
|
package mail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-05 16:27:09 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
2022-03-10 10:53:38 +01:00
|
|
|
"strings"
|
2022-03-05 12:36:53 +01:00
|
|
|
"time"
|
2022-10-17 18:12:18 +02:00
|
|
|
|
2023-02-03 10:19:26 +01:00
|
|
|
"github.com/wneessen/go-mail/log"
|
2023-01-10 00:38:42 +01:00
|
|
|
"github.com/wneessen/go-mail/smtp"
|
2022-03-05 12:36:53 +01:00
|
|
|
)
|
|
|
|
|
2022-03-10 16:56:41 +01:00
|
|
|
// Defaults
|
|
|
|
const (
|
2024-01-25 13:39:02 +01:00
|
|
|
// DefaultPort is the default connection port to the SMTP server
|
2022-03-10 16:56:41 +01:00
|
|
|
DefaultPort = 25
|
2022-03-05 12:36:53 +01:00
|
|
|
|
2024-01-25 13:39:02 +01:00
|
|
|
// DefaultPortSSL is the default connection port for SSL/TLS to the SMTP server
|
|
|
|
DefaultPortSSL = 465
|
|
|
|
|
|
|
|
// DefaultPortTLS is the default connection port for STARTTLS to the SMTP server
|
|
|
|
DefaultPortTLS = 587
|
|
|
|
|
2022-03-10 16:56:41 +01:00
|
|
|
// DefaultTimeout is the default connection timeout
|
|
|
|
DefaultTimeout = time.Second * 15
|
|
|
|
|
|
|
|
// DefaultTLSPolicy is the default STARTTLS policy
|
|
|
|
DefaultTLSPolicy = TLSMandatory
|
2022-03-15 21:10:03 +01:00
|
|
|
|
|
|
|
// DefaultTLSMinVersion is the minimum TLS version required for the connection
|
|
|
|
// Nowadays TLS1.2 should be the sane default
|
|
|
|
DefaultTLSMinVersion = tls.VersionTLS12
|
2022-03-10 16:56:41 +01:00
|
|
|
)
|
2022-03-05 12:36:53 +01:00
|
|
|
|
2022-09-11 20:24:28 +02:00
|
|
|
// DSNMailReturnOption is a type to define which MAIL RET option is used when a DSN
|
|
|
|
// is requested
|
|
|
|
type DSNMailReturnOption string
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2022-09-11 20:24:28 +02:00
|
|
|
// DSNRcptNotifyOption is a type to define which RCPT NOTIFY option is used when a DSN
|
|
|
|
// is requested
|
|
|
|
type DSNRcptNotifyOption string
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2022-09-11 20:24:28 +02:00
|
|
|
const (
|
|
|
|
// DSNMailReturnHeadersOnly requests that only the headers of the message be returned.
|
|
|
|
// See: https://www.rfc-editor.org/rfc/rfc1891#section-5.3
|
|
|
|
DSNMailReturnHeadersOnly DSNMailReturnOption = "HDRS"
|
|
|
|
|
|
|
|
// DSNMailReturnFull requests that the entire message be returned in any "failed"
|
|
|
|
// delivery status notification issued for this recipient
|
|
|
|
// See: https://www.rfc-editor.org/rfc/rfc1891#section-5.3
|
|
|
|
DSNMailReturnFull DSNMailReturnOption = "FULL"
|
|
|
|
|
|
|
|
// DSNRcptNotifyNever requests that a DSN not be returned to the sender under
|
|
|
|
// any conditions.
|
|
|
|
// See: https://www.rfc-editor.org/rfc/rfc1891#section-5.1
|
|
|
|
DSNRcptNotifyNever DSNRcptNotifyOption = "NEVER"
|
|
|
|
|
|
|
|
// DSNRcptNotifySuccess requests that a DSN be issued on successful delivery
|
|
|
|
// See: https://www.rfc-editor.org/rfc/rfc1891#section-5.1
|
|
|
|
DSNRcptNotifySuccess DSNRcptNotifyOption = "SUCCESS"
|
|
|
|
|
|
|
|
// DSNRcptNotifyFailure requests that a DSN be issued on delivery failure
|
|
|
|
// See: https://www.rfc-editor.org/rfc/rfc1891#section-5.1
|
|
|
|
DSNRcptNotifyFailure DSNRcptNotifyOption = "FAILURE"
|
|
|
|
|
|
|
|
// DSNRcptNotifyDelay indicates the sender's willingness to receive
|
|
|
|
// "delayed" DSNs. Delayed DSNs may be issued if delivery of a message has
|
|
|
|
// been delayed for an unusual amount of time (as determined by the MTA at
|
|
|
|
// which the message is delayed), but the final delivery status (whether
|
|
|
|
// successful or failure) cannot be determined. The absence of the DELAY
|
|
|
|
// keyword in a NOTIFY parameter requests that a "delayed" DSN NOT be
|
|
|
|
// issued under any conditions.
|
|
|
|
// See: https://www.rfc-editor.org/rfc/rfc1891#section-5.1
|
|
|
|
DSNRcptNotifyDelay DSNRcptNotifyOption = "DELAY"
|
|
|
|
)
|
2022-03-07 16:24:49 +01:00
|
|
|
|
2023-04-19 16:20:33 +02:00
|
|
|
// DialContextFunc is a type to define custom DialContext function.
|
|
|
|
type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error)
|
|
|
|
|
2022-09-11 20:24:28 +02:00
|
|
|
// Client is the SMTP client struct
|
|
|
|
type Client struct {
|
2024-02-24 22:06:18 +01:00
|
|
|
// connection is the net.Conn that the smtp.Client is based on
|
|
|
|
connection net.Conn
|
2022-03-05 16:27:09 +01:00
|
|
|
|
|
|
|
// Timeout for the SMTP server connection
|
2024-02-24 22:06:18 +01:00
|
|
|
connTimeout time.Duration
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2022-09-11 20:24:28 +02:00
|
|
|
// dsn indicates that we want to use DSN for the Client
|
|
|
|
dsn bool
|
|
|
|
|
|
|
|
// dsnmrtype defines the DSNMailReturnOption in case DSN is enabled
|
|
|
|
dsnmrtype DSNMailReturnOption
|
|
|
|
|
|
|
|
// dsnrntype defines the DSNRcptNotifyOption in case DSN is enabled
|
|
|
|
dsnrntype []string
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
// isEncrypted indicates if a Client connection is encrypted or not
|
|
|
|
isEncrypted bool
|
2022-03-09 13:20:01 +01:00
|
|
|
|
2022-12-26 13:59:42 +01:00
|
|
|
// noNoop indicates the Noop is to be skipped
|
|
|
|
noNoop bool
|
|
|
|
|
2022-09-11 20:24:28 +02:00
|
|
|
// HELO/EHLO string for the greeting the target SMTP server
|
|
|
|
helo string
|
|
|
|
|
2024-01-25 13:39:02 +01:00
|
|
|
// Hostname of the target SMTP server to connect to
|
2022-09-11 20:24:28 +02:00
|
|
|
host string
|
2022-03-10 10:53:38 +01:00
|
|
|
|
|
|
|
// pass is the corresponding SMTP AUTH password
|
|
|
|
pass string
|
|
|
|
|
2024-01-25 13:39:02 +01:00
|
|
|
// Port of the SMTP server to connect to
|
|
|
|
port int
|
|
|
|
fallbackPort int
|
2022-03-10 12:10:27 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
// smtpAuth is a pointer to smtp.Auth
|
|
|
|
smtpAuth smtp.Auth
|
2022-03-10 10:53:38 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
// smtpAuthType represents the authentication type for SMTP AUTH
|
|
|
|
smtpAuthType SMTPAuthType
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
// smtpClient is the smtp.Client that is set up when using the Dial*() methods
|
|
|
|
smtpClient *smtp.Client
|
2022-09-11 20:24:28 +02:00
|
|
|
|
|
|
|
// Use SSL for the connection
|
2024-02-24 22:06:18 +01:00
|
|
|
useSSL bool
|
2022-09-11 20:24:28 +02:00
|
|
|
|
|
|
|
// tlspolicy sets the client to use the provided TLSPolicy for the STARTTLS protocol
|
|
|
|
tlspolicy TLSPolicy
|
|
|
|
|
|
|
|
// tlsconfig represents the tls.Config setting for the STARTTLS connection
|
|
|
|
tlsconfig *tls.Config
|
|
|
|
|
|
|
|
// user is the SMTP AUTH username
|
|
|
|
user string
|
2023-01-14 12:47:51 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
// useDebugLog enables the debug logging on the SMTP client
|
|
|
|
useDebugLog bool
|
2023-02-03 10:19:26 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
// logger is a logger that implements the log.Logger interface
|
|
|
|
logger log.Logger
|
2023-04-19 16:20:33 +02:00
|
|
|
|
|
|
|
// dialContextFunc is a custom DialContext function to dial target SMTP server
|
|
|
|
dialContextFunc DialContextFunc
|
2022-03-05 12:36:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option returns a function that can be used for grouping Client options
|
2022-03-15 21:10:03 +01:00
|
|
|
type Option func(*Client) error
|
2022-03-05 12:36:53 +01:00
|
|
|
|
2022-03-05 16:27:09 +01:00
|
|
|
var (
|
2022-03-15 21:10:03 +01:00
|
|
|
// ErrInvalidPort should be used if a port is specified that is not valid
|
|
|
|
ErrInvalidPort = errors.New("invalid port number")
|
|
|
|
|
|
|
|
// ErrInvalidTimeout should be used if a timeout is set that is zero or negative
|
|
|
|
ErrInvalidTimeout = errors.New("timeout cannot be zero or negative")
|
|
|
|
|
|
|
|
// ErrInvalidHELO should be used if an empty HELO sting is provided
|
|
|
|
ErrInvalidHELO = errors.New("invalid HELO/EHLO value - must not be empty")
|
|
|
|
|
|
|
|
// ErrInvalidTLSConfig should be used if an empty tls.Config is provided
|
|
|
|
ErrInvalidTLSConfig = errors.New("invalid TLS config")
|
|
|
|
|
2022-03-05 16:27:09 +01:00
|
|
|
// ErrNoHostname should be used if a Client has no hostname set
|
|
|
|
ErrNoHostname = errors.New("hostname for client cannot be empty")
|
2022-03-10 12:10:27 +01:00
|
|
|
|
|
|
|
// ErrDeadlineExtendFailed should be used if the extension of the connection deadline fails
|
|
|
|
ErrDeadlineExtendFailed = errors.New("connection deadline extension failed")
|
|
|
|
|
|
|
|
// ErrNoActiveConnection should be used when a method is used that requies a server connection
|
|
|
|
// but is not yet connected
|
|
|
|
ErrNoActiveConnection = errors.New("not connected to SMTP server")
|
2022-03-14 10:29:53 +01:00
|
|
|
|
|
|
|
// ErrServerNoUnencoded should be used when 8BIT encoding is selected for a message, but
|
|
|
|
// the server does not offer 8BITMIME mode
|
|
|
|
ErrServerNoUnencoded = errors.New("message is 8bit unencoded, but server does not support 8BITMIME")
|
2022-09-11 20:24:28 +02:00
|
|
|
|
|
|
|
// ErrInvalidDSNMailReturnOption should be used when an invalid option is provided for the
|
|
|
|
// DSNMailReturnOption in WithDSN
|
|
|
|
ErrInvalidDSNMailReturnOption = errors.New("DSN mail return option can only be HDRS or FULL")
|
|
|
|
|
|
|
|
// ErrInvalidDSNRcptNotifyOption should be used when an invalid option is provided for the
|
|
|
|
// DSNRcptNotifyOption in WithDSN
|
|
|
|
ErrInvalidDSNRcptNotifyOption = errors.New("DSN rcpt notify option can only be: NEVER, " +
|
|
|
|
"SUCCESS, FAILURE or DELAY")
|
|
|
|
|
|
|
|
// ErrInvalidDSNRcptNotifyCombination should be used when an invalid option is provided for the
|
|
|
|
// DSNRcptNotifyOption in WithDSN
|
|
|
|
ErrInvalidDSNRcptNotifyCombination = errors.New("DSN rcpt notify option NEVER cannot be " +
|
|
|
|
"combined with any of SUCCESS, FAILURE or DELAY")
|
2022-03-05 16:27:09 +01:00
|
|
|
)
|
|
|
|
|
2022-03-05 12:36:53 +01:00
|
|
|
// NewClient returns a new Session client object
|
2024-02-24 22:06:18 +01:00
|
|
|
func NewClient(host string, opts ...Option) (*Client, error) {
|
2022-03-05 16:27:09 +01:00
|
|
|
c := &Client{
|
2024-02-24 22:06:18 +01:00
|
|
|
connTimeout: DefaultTimeout,
|
|
|
|
host: host,
|
|
|
|
port: DefaultPort,
|
|
|
|
tlsconfig: &tls.Config{ServerName: host, MinVersion: DefaultTLSMinVersion},
|
|
|
|
tlspolicy: DefaultTLSPolicy,
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set default HELO/EHLO hostname
|
|
|
|
if err := c.setDefaultHelo(); err != nil {
|
|
|
|
return c, err
|
2022-03-05 12:36:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override defaults with optionally provided Option functions
|
2024-02-24 22:06:18 +01:00
|
|
|
for _, opt := range opts {
|
|
|
|
if opt == nil {
|
2022-03-05 12:36:53 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if err := opt(c); err != nil {
|
2022-03-15 21:10:03 +01:00
|
|
|
return c, fmt.Errorf("failed to apply option: %w", err)
|
|
|
|
}
|
2022-03-05 12:36:53 +01:00
|
|
|
}
|
|
|
|
|
2022-03-05 16:27:09 +01:00
|
|
|
// Some settings in a Client cannot be empty/unset
|
|
|
|
if c.host == "" {
|
|
|
|
return c, ErrNoHostname
|
2022-03-05 12:36:53 +01:00
|
|
|
}
|
2022-03-05 16:27:09 +01:00
|
|
|
|
|
|
|
return c, nil
|
2022-03-05 12:36:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithPort overrides the default connection port
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithPort(port int) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
if port < 1 || port > 65535 {
|
2022-03-15 21:10:03 +01:00
|
|
|
return ErrInvalidPort
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.port = port
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTimeout overrides the default connection timeout
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithTimeout(timeout time.Duration) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
if timeout <= 0 {
|
2022-03-15 21:10:03 +01:00
|
|
|
return ErrInvalidTimeout
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.connTimeout = timeout
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSSL tells the client to use a SSL/TLS connection
|
2024-01-25 13:39:02 +01:00
|
|
|
//
|
|
|
|
// Deprecated: use WithSSLPort instead.
|
2022-03-05 16:27:09 +01:00
|
|
|
func WithSSL() Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.useSSL = true
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:39:02 +01:00
|
|
|
// WithSSLPort tells the client to use a SSL/TLS connection.
|
|
|
|
// It automatically sets the port to 465.
|
|
|
|
//
|
|
|
|
// When the SSL connection fails and fallback is set to true,
|
|
|
|
// the client will attempt to connect on port 25 using plaintext.
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithSSLPort(fallback bool) Option {
|
2024-01-25 13:39:02 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.SetSSLPort(true, fallback)
|
2024-01-25 13:39:02 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 12:47:51 +01:00
|
|
|
// WithDebugLog tells the client to log incoming and outgoing messages of the SMTP client
|
|
|
|
// to StdErr
|
|
|
|
func WithDebugLog() Option {
|
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.useDebugLog = true
|
2023-01-14 12:47:51 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:19:26 +01:00
|
|
|
// WithLogger overrides the default log.Logger that is used for debug logging
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithLogger(logger log.Logger) Option {
|
2023-02-03 10:19:26 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.logger = logger
|
2023-02-03 10:19:26 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 15:15:42 +01:00
|
|
|
// WithHELO tells the client to use the provided string as HELO/EHLO greeting host
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithHELO(helo string) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
if helo == "" {
|
2022-03-15 21:10:03 +01:00
|
|
|
return ErrInvalidHELO
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.helo = helo
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-06 15:15:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 13:20:01 +01:00
|
|
|
// WithTLSPolicy tells the client to use the provided TLSPolicy
|
2024-01-25 13:39:02 +01:00
|
|
|
//
|
|
|
|
// Deprecated: use WithTLSPortPolicy instead.
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithTLSPolicy(policy TLSPolicy) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.tlspolicy = policy
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-09 13:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:39:02 +01:00
|
|
|
// WithTLSPortPolicy tells the client to use the provided TLSPolicy,
|
|
|
|
// The correct port is automatically set.
|
|
|
|
//
|
|
|
|
// Port 587 is used for TLSMandatory and TLSOpportunistic.
|
|
|
|
// If the connection fails with TLSOpportunistic,
|
|
|
|
// a plaintext connection is attempted on port 25 as a fallback.
|
|
|
|
// NoTLS will allways use port 25.
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithTLSPortPolicy(policy TLSPolicy) Option {
|
2024-01-25 13:39:02 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.SetTLSPortPolicy(policy)
|
2024-01-25 13:39:02 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 13:20:01 +01:00
|
|
|
// WithTLSConfig tells the client to use the provided *tls.Config
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithTLSConfig(tlsconfig *tls.Config) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
if tlsconfig == nil {
|
2022-03-15 21:10:03 +01:00
|
|
|
return ErrInvalidTLSConfig
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.tlsconfig = tlsconfig
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-09 13:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:53:38 +01:00
|
|
|
// WithSMTPAuth tells the client to use the provided SMTPAuthType for authentication
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithSMTPAuth(authtype SMTPAuthType) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpAuthType = authtype
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-10 10:53:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSMTPAuthCustom tells the client to use the provided smtp.Auth for SMTP authentication
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithSMTPAuthCustom(smtpAuth smtp.Auth) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpAuth = smtpAuth
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-10 10:53:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithUsername tells the client to use the provided string as username for authentication
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithUsername(username string) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.user = username
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-10 10:53:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPassword tells the client to use the provided string as password/secret for authentication
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithPassword(password string) Option {
|
2022-03-15 21:10:03 +01:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.pass = password
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-10 10:53:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 20:24:28 +02:00
|
|
|
// WithDSN enables the Client to request DSNs (if the server supports it)
|
|
|
|
// as described in the RFC 1891 and set defaults for DSNMailReturnOption
|
|
|
|
// to DSNMailReturnFull and DSNRcptNotifyOption to DSNRcptNotifySuccess
|
|
|
|
// and DSNRcptNotifyFailure
|
|
|
|
func WithDSN() Option {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.dsn = true
|
|
|
|
c.dsnmrtype = DSNMailReturnFull
|
|
|
|
c.dsnrntype = []string{string(DSNRcptNotifyFailure), string(DSNRcptNotifySuccess)}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDSNMailReturnType enables the Client to request DSNs (if the server supports it)
|
|
|
|
// as described in the RFC 1891 and set the MAIL FROM Return option type to the
|
|
|
|
// given DSNMailReturnOption
|
|
|
|
// See: https://www.rfc-editor.org/rfc/rfc1891
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithDSNMailReturnType(option DSNMailReturnOption) Option {
|
2022-09-11 20:24:28 +02:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
switch option {
|
2022-09-11 20:24:28 +02:00
|
|
|
case DSNMailReturnHeadersOnly:
|
|
|
|
case DSNMailReturnFull:
|
|
|
|
default:
|
|
|
|
return ErrInvalidDSNMailReturnOption
|
|
|
|
}
|
|
|
|
|
|
|
|
c.dsn = true
|
2024-02-24 22:06:18 +01:00
|
|
|
c.dsnmrtype = option
|
2022-09-11 20:24:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// See: https://www.rfc-editor.org/rfc/rfc1891
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithDSNRcptNotifyType(opts ...DSNRcptNotifyOption) Option {
|
2022-09-11 20:24:28 +02:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
var rcptOpts []string
|
2022-09-11 20:24:28 +02:00
|
|
|
var ns, nns bool
|
2024-02-24 22:06:18 +01:00
|
|
|
if len(opts) > 0 {
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt {
|
2022-09-11 20:24:28 +02:00
|
|
|
case DSNRcptNotifyNever:
|
|
|
|
ns = true
|
|
|
|
case DSNRcptNotifySuccess:
|
|
|
|
nns = true
|
|
|
|
case DSNRcptNotifyFailure:
|
|
|
|
nns = true
|
|
|
|
case DSNRcptNotifyDelay:
|
|
|
|
nns = true
|
|
|
|
default:
|
|
|
|
return ErrInvalidDSNRcptNotifyOption
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
rcptOpts = append(rcptOpts, string(opt))
|
2022-09-11 20:24:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ns && nns {
|
|
|
|
return ErrInvalidDSNRcptNotifyCombination
|
|
|
|
}
|
|
|
|
|
|
|
|
c.dsn = true
|
2024-02-24 22:06:18 +01:00
|
|
|
c.dsnrntype = rcptOpts
|
2022-09-11 20:24:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:59:42 +01:00
|
|
|
// WithoutNoop disables the Client Noop check during connections. This is primarily for servers which delay responses
|
|
|
|
// to SMTP commands that are not the AUTH command. For example Microsoft Exchange's Tarpit.
|
|
|
|
func WithoutNoop() Option {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.noNoop = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:20:33 +02:00
|
|
|
// WithDialContextFunc overrides the default DialContext for connecting SMTP server
|
2024-02-24 22:06:18 +01:00
|
|
|
func WithDialContextFunc(dialCtxFunc DialContextFunc) Option {
|
2023-04-19 16:20:33 +02:00
|
|
|
return func(c *Client) error {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.dialContextFunc = dialCtxFunc
|
2023-04-19 16:20:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:24:49 +01:00
|
|
|
// TLSPolicy returns the currently set TLSPolicy as string
|
|
|
|
func (c *Client) TLSPolicy() string {
|
2022-03-09 17:05:38 +01:00
|
|
|
return c.tlspolicy.String()
|
2022-03-07 16:24:49 +01:00
|
|
|
}
|
|
|
|
|
2022-03-07 18:14:38 +01:00
|
|
|
// ServerAddr returns the currently set combination of hostname and port
|
|
|
|
func (c *Client) ServerAddr() string {
|
|
|
|
return fmt.Sprintf("%s:%d", c.host, c.port)
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:24:49 +01:00
|
|
|
// SetTLSPolicy overrides the current TLSPolicy with the given TLSPolicy value
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetTLSPolicy(policy TLSPolicy) {
|
|
|
|
c.tlspolicy = policy
|
2022-03-07 16:24:49 +01:00
|
|
|
}
|
|
|
|
|
2024-01-25 13:39:02 +01:00
|
|
|
// SetTLSPortPolicy overrides the current TLSPolicy with the given TLSPolicy
|
|
|
|
// value. The correct port is automatically set.
|
|
|
|
//
|
|
|
|
// Port 587 is used for TLSMandatory and TLSOpportunistic.
|
|
|
|
// If the connection fails with TLSOpportunistic, a plaintext connection is
|
|
|
|
// attempted on port 25 as a fallback.
|
|
|
|
// NoTLS will allways use port 25.
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetTLSPortPolicy(policy TLSPolicy) {
|
2024-01-25 13:39:02 +01:00
|
|
|
c.port = DefaultPortTLS
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
if policy == TLSOpportunistic {
|
2024-01-25 13:39:02 +01:00
|
|
|
c.fallbackPort = DefaultPort
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if policy == NoTLS {
|
2024-01-25 13:39:02 +01:00
|
|
|
c.port = DefaultPort
|
|
|
|
}
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
c.tlspolicy = policy
|
2024-01-25 13:39:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-16 21:02:31 +01:00
|
|
|
// SetSSL tells the Client wether to use SSL or not
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetSSL(ssl bool) {
|
|
|
|
c.useSSL = ssl
|
2022-03-16 21:02:31 +01:00
|
|
|
}
|
|
|
|
|
2024-01-25 13:39:02 +01:00
|
|
|
// SetSSLPort tells the Client wether or not to use SSL and fallback.
|
|
|
|
// The correct port is automatically set.
|
|
|
|
//
|
|
|
|
// Port 465 is used when SSL set (true).
|
|
|
|
// Port 25 is used when SSL is unset (false).
|
2024-01-31 11:22:01 +01:00
|
|
|
// When the SSL connection fails and fb is set to true,
|
2024-01-25 13:39:02 +01:00
|
|
|
// the client will attempt to connect on port 25 using plaintext.
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetSSLPort(ssl bool, fallback bool) {
|
2024-01-31 11:22:01 +01:00
|
|
|
c.port = DefaultPort
|
2024-01-25 13:39:02 +01:00
|
|
|
if ssl {
|
|
|
|
c.port = DefaultPortSSL
|
|
|
|
}
|
|
|
|
|
2024-01-31 11:22:01 +01:00
|
|
|
c.fallbackPort = 0
|
2024-02-24 22:06:18 +01:00
|
|
|
if fallback {
|
2024-01-25 13:39:02 +01:00
|
|
|
c.fallbackPort = DefaultPort
|
|
|
|
}
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
c.useSSL = ssl
|
2024-01-25 13:39:02 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 12:47:51 +01:00
|
|
|
// SetDebugLog tells the Client whether debug logging is enabled or not
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetDebugLog(val bool) {
|
|
|
|
c.useDebugLog = val
|
|
|
|
if c.smtpClient != nil {
|
|
|
|
c.smtpClient.SetDebugLog(val)
|
2023-01-14 13:05:04 +01:00
|
|
|
}
|
2023-01-14 12:47:51 +01:00
|
|
|
}
|
|
|
|
|
2023-02-03 10:19:26 +01:00
|
|
|
// SetLogger tells the Client which log.Logger to use
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetLogger(logger log.Logger) {
|
|
|
|
c.logger = logger
|
|
|
|
if c.smtpClient != nil {
|
|
|
|
c.smtpClient.SetLogger(logger)
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 13:20:01 +01:00
|
|
|
// SetTLSConfig overrides the current *tls.Config with the given *tls.Config value
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetTLSConfig(tlsconfig *tls.Config) error {
|
|
|
|
if tlsconfig == nil {
|
2022-03-15 22:37:55 +01:00
|
|
|
return ErrInvalidTLSConfig
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.tlsconfig = tlsconfig
|
2022-03-15 22:37:55 +01:00
|
|
|
return nil
|
2022-03-09 13:20:01 +01:00
|
|
|
}
|
|
|
|
|
2022-03-10 12:10:27 +01:00
|
|
|
// SetUsername overrides the current username string with the given value
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetUsername(username string) {
|
|
|
|
c.user = username
|
2022-03-10 12:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPassword overrides the current password string with the given value
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetPassword(password string) {
|
|
|
|
c.pass = password
|
2022-03-10 12:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSMTPAuth overrides the current SMTP AUTH type setting with the given value
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetSMTPAuth(authtype SMTPAuthType) {
|
|
|
|
c.smtpAuthType = authtype
|
2022-03-10 12:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSMTPAuthCustom overrides the current SMTP AUTH setting with the given custom smtp.Auth
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetSMTPAuthCustom(smtpAuth smtp.Auth) {
|
|
|
|
c.smtpAuth = smtpAuth
|
2022-03-07 16:24:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// setDefaultHelo retrieves the current hostname and sets it as HELO/EHLO hostname
|
|
|
|
func (c *Client) setDefaultHelo() error {
|
2024-02-24 22:06:18 +01:00
|
|
|
hostname, err := os.Hostname()
|
2022-03-07 16:24:49 +01:00
|
|
|
if err != nil {
|
2024-02-24 22:06:18 +01:00
|
|
|
return fmt.Errorf("failed to read local hostname: %w", err)
|
2022-03-07 16:24:49 +01:00
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.helo = hostname
|
2022-03-07 16:24:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
// DialWithContext establishes a connection to the SMTP server with a given context.Context
|
|
|
|
func (c *Client) DialWithContext(dialCtx context.Context) error {
|
|
|
|
ctx, cancel := context.WithDeadline(dialCtx, time.Now().Add(c.connTimeout))
|
|
|
|
defer cancel()
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2023-04-19 16:20:33 +02:00
|
|
|
if c.dialContextFunc == nil {
|
2024-02-24 22:06:18 +01:00
|
|
|
netDialer := net.Dialer{}
|
|
|
|
c.dialContextFunc = netDialer.DialContext
|
2022-12-26 12:07:47 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.useSSL {
|
|
|
|
tlsDialer := tls.Dialer{NetDialer: &netDialer, Config: c.tlsconfig}
|
|
|
|
c.isEncrypted = true
|
|
|
|
c.dialContextFunc = tlsDialer.DialContext
|
2023-04-19 16:20:33 +02:00
|
|
|
}
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
2023-04-19 16:20:33 +02:00
|
|
|
var err error
|
2024-02-24 22:06:18 +01:00
|
|
|
c.connection, err = c.dialContextFunc(ctx, "tcp", c.ServerAddr())
|
2024-01-25 13:39:02 +01:00
|
|
|
if err != nil && c.fallbackPort != 0 {
|
|
|
|
// TODO: should we somehow log or append the previous error?
|
2024-02-24 22:06:18 +01:00
|
|
|
c.connection, err = c.dialContextFunc(ctx, "tcp", c.serverFallbackAddr())
|
2024-01-25 13:39:02 +01:00
|
|
|
}
|
2022-03-05 16:27:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-05 12:36:53 +01:00
|
|
|
}
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
client, err := smtp.NewClient(c.connection, c.host)
|
2022-03-05 16:27:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if client == nil {
|
2023-11-29 16:25:35 +01:00
|
|
|
return fmt.Errorf("SMTP client is nil")
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpClient = client
|
2023-11-29 16:25:35 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.logger != nil {
|
|
|
|
c.smtpClient.SetLogger(c.logger)
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.useDebugLog {
|
|
|
|
c.smtpClient.SetDebugLog(true)
|
2023-01-14 12:47:51 +01:00
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if err = c.smtpClient.Hello(c.helo); err != nil {
|
2022-03-05 16:27:09 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
if err = c.tls(); err != nil {
|
2022-03-10 16:56:41 +01:00
|
|
|
return err
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
2022-03-07 16:24:49 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
if err = c.auth(); err != nil {
|
2022-03-10 10:53:38 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:19:51 +01:00
|
|
|
// Close closes the Client connection
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
if err := c.checkConn(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if err := c.smtpClient.Quit(); err != nil {
|
2022-03-10 16:19:51 +01:00
|
|
|
return fmt.Errorf("failed to close SMTP client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 19:17:43 +01:00
|
|
|
// Reset sends the RSET command to the SMTP client
|
|
|
|
func (c *Client) Reset() error {
|
|
|
|
if err := c.checkConn(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if err := c.smtpClient.Reset(); err != nil {
|
2022-03-11 19:17:43 +01:00
|
|
|
return fmt.Errorf("failed to send RSET to SMTP client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 12:10:27 +01:00
|
|
|
// DialAndSend establishes a connection to the SMTP server with a
|
|
|
|
// default context.Background and sends the mail
|
2024-02-25 17:48:53 +01:00
|
|
|
func (c *Client) DialAndSend(messages ...*Msg) error {
|
2022-03-10 12:10:27 +01:00
|
|
|
ctx := context.Background()
|
2024-02-25 17:48:53 +01:00
|
|
|
return c.DialAndSendWithContext(ctx, messages...)
|
2022-09-26 10:40:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialAndSendWithContext establishes a connection to the SMTP server with a
|
|
|
|
// custom context and sends the mail
|
2024-02-25 17:48:53 +01:00
|
|
|
func (c *Client) DialAndSendWithContext(ctx context.Context, messages ...*Msg) error {
|
2022-03-10 12:10:27 +01:00
|
|
|
if err := c.DialWithContext(ctx); err != nil {
|
|
|
|
return fmt.Errorf("dial failed: %w", err)
|
|
|
|
}
|
2024-02-25 17:48:53 +01:00
|
|
|
if err := c.Send(messages...); err != nil {
|
2022-03-10 12:10:27 +01:00
|
|
|
return fmt.Errorf("send failed: %w", err)
|
|
|
|
}
|
2022-03-12 15:10:01 +01:00
|
|
|
if err := c.Close(); err != nil {
|
2022-10-17 18:16:00 +02:00
|
|
|
return fmt.Errorf("failed to close connction: %w", err)
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
2022-03-10 12:10:27 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkConn makes sure that a required server connection is available and extends the
|
|
|
|
// connection deadline
|
|
|
|
func (c *Client) checkConn() error {
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.connection == nil {
|
2022-03-10 12:10:27 +01:00
|
|
|
return ErrNoActiveConnection
|
|
|
|
}
|
2022-12-26 13:59:42 +01:00
|
|
|
|
|
|
|
if !c.noNoop {
|
2024-02-24 22:06:18 +01:00
|
|
|
if err := c.smtpClient.Noop(); err != nil {
|
2022-12-26 13:59:42 +01:00
|
|
|
return ErrNoActiveConnection
|
|
|
|
}
|
2022-03-11 19:17:43 +01:00
|
|
|
}
|
2022-12-26 13:59:42 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
if err := c.connection.SetDeadline(time.Now().Add(c.connTimeout)); err != nil {
|
2022-03-10 12:10:27 +01:00
|
|
|
return ErrDeadlineExtendFailed
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:39:02 +01:00
|
|
|
// serverFallbackAddr returns the currently set combination of hostname
|
|
|
|
// and fallback port.
|
|
|
|
func (c *Client) serverFallbackAddr() string {
|
|
|
|
return fmt.Sprintf("%s:%d", c.host, c.fallbackPort)
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:56:41 +01:00
|
|
|
// tls tries to make sure that the STARTTLS requirements are satisfied
|
|
|
|
func (c *Client) tls() error {
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.connection == nil {
|
2022-03-16 21:02:31 +01:00
|
|
|
return ErrNoActiveConnection
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if !c.useSSL && c.tlspolicy != NoTLS {
|
2024-02-25 17:48:53 +01:00
|
|
|
hasStartTLS := false
|
|
|
|
extension, _ := c.smtpClient.Extension("STARTTLS")
|
2022-03-10 16:56:41 +01:00
|
|
|
if c.tlspolicy == TLSMandatory {
|
2024-02-25 17:48:53 +01:00
|
|
|
hasStartTLS = true
|
|
|
|
if !extension {
|
2022-03-10 16:56:41 +01:00
|
|
|
return fmt.Errorf("STARTTLS mode set to: %q, but target host does not support STARTTLS",
|
|
|
|
c.tlspolicy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.tlspolicy == TLSOpportunistic {
|
2024-02-25 17:48:53 +01:00
|
|
|
if extension {
|
|
|
|
hasStartTLS = true
|
2022-03-10 16:56:41 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-25 17:48:53 +01:00
|
|
|
if hasStartTLS {
|
2024-02-24 22:06:18 +01:00
|
|
|
if err := c.smtpClient.StartTLS(c.tlsconfig); err != nil {
|
2022-03-10 16:56:41 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
_, c.isEncrypted = c.smtpClient.TLSConnectionState()
|
2022-03-10 16:56:41 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:53:38 +01:00
|
|
|
// auth will try to perform SMTP AUTH if requested
|
|
|
|
func (c *Client) auth() error {
|
2022-03-10 12:10:27 +01:00
|
|
|
if err := c.checkConn(); err != nil {
|
|
|
|
return fmt.Errorf("failed to authenticate: %w", err)
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.smtpAuth == nil && c.smtpAuthType != "" {
|
2024-02-25 17:48:53 +01:00
|
|
|
hasSMTPAuth, smtpAuthType := c.smtpClient.Extension("AUTH")
|
|
|
|
if !hasSMTPAuth {
|
2022-03-10 10:53:38 +01:00
|
|
|
return fmt.Errorf("server does not support SMTP AUTH")
|
|
|
|
}
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
switch c.smtpAuthType {
|
2022-03-10 10:53:38 +01:00
|
|
|
case SMTPAuthPlain:
|
2024-02-25 17:48:53 +01:00
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthPlain)) {
|
2022-03-10 10:53:38 +01:00
|
|
|
return ErrPlainAuthNotSupported
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpAuth = smtp.PlainAuth("", c.user, c.pass, c.host)
|
2022-03-10 10:53:38 +01:00
|
|
|
case SMTPAuthLogin:
|
2024-02-25 17:48:53 +01:00
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthLogin)) {
|
2022-03-10 10:53:38 +01:00
|
|
|
return ErrLoginAuthNotSupported
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpAuth = smtp.LoginAuth(c.user, c.pass, c.host)
|
2022-03-10 10:53:38 +01:00
|
|
|
case SMTPAuthCramMD5:
|
2024-02-25 17:48:53 +01:00
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthCramMD5)) {
|
2022-03-10 10:53:38 +01:00
|
|
|
return ErrCramMD5AuthNotSupported
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpAuth = smtp.CRAMMD5Auth(c.user, c.pass)
|
2023-05-28 14:31:04 +02:00
|
|
|
case SMTPAuthXOAUTH2:
|
2024-02-25 17:48:53 +01:00
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthXOAUTH2)) {
|
2023-05-28 14:31:04 +02:00
|
|
|
return ErrXOauth2AuthNotSupported
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpAuth = smtp.XOAuth2Auth(c.user, c.pass)
|
2022-03-10 10:53:38 +01:00
|
|
|
default:
|
2024-02-24 22:06:18 +01:00
|
|
|
return fmt.Errorf("unsupported SMTP AUTH type %q", c.smtpAuthType)
|
2022-03-10 10:53:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.smtpAuth != nil {
|
|
|
|
if err := c.smtpClient.Auth(c.smtpAuth); err != nil {
|
2022-03-10 10:53:38 +01:00
|
|
|
return fmt.Errorf("SMTP AUTH failed: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 16:27:09 +01:00
|
|
|
return nil
|
2022-03-05 12:36:53 +01:00
|
|
|
}
|