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"
|
2024-09-26 11:51:30 +02:00
|
|
|
"sync"
|
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
|
|
|
const (
|
2024-10-04 20:13:13 +02: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-10-04 20:13:13 +02:00
|
|
|
// DefaultPortSSL is the default connection port for SSL/TLS to the SMTP server.
|
2024-01-25 13:39:02 +01:00
|
|
|
DefaultPortSSL = 465
|
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// DefaultPortTLS is the default connection port for STARTTLS to the SMTP server.
|
2024-01-25 13:39:02 +01:00
|
|
|
DefaultPortTLS = 587
|
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// DefaultTimeout is the default connection timeout.
|
2022-03-10 16:56:41 +01:00
|
|
|
DefaultTimeout = time.Second * 15
|
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// DefaultTLSPolicy specifies the default TLS policy for connections.
|
2022-03-10 16:56:41 +01:00
|
|
|
DefaultTLSPolicy = TLSMandatory
|
2022-03-15 21:10:03 +01:00
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// DefaultTLSMinVersion defines the minimum TLS version to be used for secure connections.
|
|
|
|
// Nowadays TLS 1.2 is assumed be a sane default.
|
2022-03-15 21:10:03 +01:00
|
|
|
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
|
|
|
const (
|
2024-10-04 20:13:13 +02:00
|
|
|
|
|
|
|
// DSNMailReturnHeadersOnly requests that only the message headers of the mail message are returned in
|
|
|
|
// a DSN (Delivery Status Notification).
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc1891#section-5.3
|
2022-09-11 20:24:28 +02:00
|
|
|
DSNMailReturnHeadersOnly DSNMailReturnOption = "HDRS"
|
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// DSNMailReturnFull requests that the entire mail message is returned in any failed DSN
|
|
|
|
// (Delivery Status Notification) issued for this recipient.
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc1891/#section-5.3
|
2022-09-11 20:24:28 +02:00
|
|
|
DSNMailReturnFull DSNMailReturnOption = "FULL"
|
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// DSNRcptNotifyNever indicates that no DSN (Delivery Status Notifications) should be sent for the
|
|
|
|
// recipient under any condition.
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc1891/#section-5.1
|
2022-09-11 20:24:28 +02:00
|
|
|
DSNRcptNotifyNever DSNRcptNotifyOption = "NEVER"
|
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// DSNRcptNotifySuccess indicates that the sender requests a DSN (Delivery Status Notification) if the
|
|
|
|
// message is successfully delivered.
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc1891/#section-5.1
|
2022-09-11 20:24:28 +02:00
|
|
|
DSNRcptNotifySuccess DSNRcptNotifyOption = "SUCCESS"
|
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// DSNRcptNotifyFailure requests that a DSN (Delivery Status Notification) is issued if delivery of
|
|
|
|
// a message fails.
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc1891/#section-5.1
|
2022-09-11 20:24:28 +02:00
|
|
|
DSNRcptNotifyFailure DSNRcptNotifyOption = "FAILURE"
|
|
|
|
|
2024-10-04 20:13:13 +02:00
|
|
|
// 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.
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc1891/#section-5.1
|
2022-09-11 20:24:28 +02:00
|
|
|
DSNRcptNotifyDelay DSNRcptNotifyOption = "DELAY"
|
|
|
|
)
|
2022-03-07 16:24:49 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
type (
|
2023-04-19 16:20:33 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// 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)
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// 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
|
2024-09-27 10:36:09 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// Option is a function type that modifies the configuration or behavior of a Client instance.
|
|
|
|
Option func(*Client) error
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// 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
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// dialContextFunc is the DialContextFunc that is used by the Client to connect to the SMTP server.
|
|
|
|
dialContextFunc DialContextFunc
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2024-10-04 20:30:43 +02:00
|
|
|
// dsnReturnType defines the DSNMailReturnOption in case DSN is enabled
|
|
|
|
dsnReturnType DSNMailReturnOption
|
2022-12-26 13:59:42 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// dsnrntype defines the DSNRcptNotifyOption in case DSN is enabled
|
|
|
|
dsnrntype []string
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// fallbackPort is used as an alternative port number in case the primary port is unavailable or
|
|
|
|
// fails to bind.
|
|
|
|
fallbackPort int
|
2022-03-10 10:53:38 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// HELO/EHLO string for the greeting the target SMTP server
|
|
|
|
helo string
|
2024-09-27 10:36:09 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// Hostname of the target SMTP server to connect to
|
|
|
|
host string
|
2024-09-27 10:36:09 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// isEncrypted indicates if a Client connection is encrypted or not
|
|
|
|
isEncrypted bool
|
2024-09-27 10:36:09 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// logger is a logger that implements the log.Logger interface
|
|
|
|
logger log.Logger
|
2024-09-27 10:36:09 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// mutex is used to synchronize access to shared resources, ensuring that only one goroutine can
|
|
|
|
// modify them at a time.
|
|
|
|
mutex sync.RWMutex
|
2022-03-10 10:53:38 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// noNoop indicates the Noop is to be skipped
|
|
|
|
noNoop bool
|
2022-03-10 12:10:27 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// pass is the corresponding SMTP AUTH password
|
|
|
|
pass string
|
2022-03-10 10:53:38 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// port specifies the network port number on which the server listens for incoming connections.
|
|
|
|
port int
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// requestDSN indicates that we want to use DSN for the Client
|
|
|
|
requestDSN bool
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// smtpAuth is a pointer to smtp.Auth
|
|
|
|
smtpAuth smtp.Auth
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// smtpAuthType represents the authentication type for SMTP AUTH
|
|
|
|
smtpAuthType SMTPAuthType
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// smtpClient is the smtp.Client that is set up when using the Dial*() methods
|
|
|
|
smtpClient *smtp.Client
|
2023-02-03 10:19:26 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// tlspolicy sets the client to use the provided TLSPolicy for the STARTTLS protocol
|
|
|
|
tlspolicy TLSPolicy
|
2023-04-19 16:20:33 +02:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// tlsconfig represents the tls.Config setting for the STARTTLS connection
|
|
|
|
tlsconfig *tls.Config
|
|
|
|
|
|
|
|
// useDebugLog enables the debug logging on the SMTP client
|
|
|
|
useDebugLog bool
|
2022-03-05 12:36:53 +01:00
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
// user is the SMTP AUTH username
|
|
|
|
user string
|
|
|
|
|
|
|
|
// Use SSL for the connection
|
|
|
|
useSSL bool
|
|
|
|
}
|
|
|
|
)
|
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
|
|
|
|
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-04-06 16:49:34 +02:00
|
|
|
// WithSSLPort tells the Client wether or not to use SSL and fallback.
|
|
|
|
// The correct port is automatically set.
|
2024-01-25 13:39:02 +01:00
|
|
|
//
|
2024-04-06 16:49:34 +02:00
|
|
|
// Port 465 is used when SSL set (true).
|
|
|
|
// Port 25 is used when SSL is unset (false).
|
|
|
|
// 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-04-06 16:49:34 +02:00
|
|
|
//
|
|
|
|
// Note: If a different port has already been set otherwise, the port-choosing
|
|
|
|
// and fallback automatism will be skipped.
|
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
|
|
|
//
|
2024-04-06 16:49:34 +02:00
|
|
|
// Note: To follow best-practices for SMTP TLS connections, it is recommended
|
|
|
|
// to 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-04-06 16:49:34 +02:00
|
|
|
//
|
|
|
|
// Note: If a different port has already been set otherwise, the port-choosing
|
|
|
|
// and fallback automatism will be skipped.
|
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 {
|
2024-10-04 20:29:14 +02:00
|
|
|
c.requestDSN = true
|
2024-10-04 20:30:43 +02:00
|
|
|
c.dsnReturnType = DSNMailReturnFull
|
2022-09-11 20:24:28 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
c.requestDSN = true
|
2024-10-04 20:30:43 +02:00
|
|
|
c.dsnReturnType = 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
|
|
|
|
}
|
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
c.requestDSN = 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-04-06 16:49:34 +02:00
|
|
|
//
|
|
|
|
// Note: To follow best-practices for SMTP TLS connections, it is recommended
|
|
|
|
// to use SetTLSPortPolicy instead.
|
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-04-06 16:49:34 +02:00
|
|
|
//
|
|
|
|
// Note: If a different port has already been set otherwise, the port-choosing
|
|
|
|
// and fallback automatism will be skipped.
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetTLSPortPolicy(policy TLSPolicy) {
|
2024-04-06 16:49:34 +02:00
|
|
|
if c.port == DefaultPort {
|
|
|
|
c.port = DefaultPortTLS
|
2024-01-25 13:39:02 +01:00
|
|
|
|
2024-04-06 16:49:34 +02:00
|
|
|
if policy == TLSOpportunistic {
|
|
|
|
c.fallbackPort = DefaultPort
|
|
|
|
}
|
|
|
|
if policy == NoTLS {
|
|
|
|
c.port = DefaultPort
|
|
|
|
}
|
2024-01-25 13:39:02 +01:00
|
|
|
}
|
|
|
|
|
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-04-06 16:49:34 +02:00
|
|
|
//
|
|
|
|
// Note: If a different port has already been set otherwise, the port-choosing
|
|
|
|
// and fallback automatism will be skipped.
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetSSLPort(ssl bool, fallback bool) {
|
2024-04-06 16:49:34 +02:00
|
|
|
if c.port == DefaultPort {
|
|
|
|
if ssl {
|
|
|
|
c.port = DefaultPortSSL
|
|
|
|
}
|
2024-01-25 13:39:02 +01:00
|
|
|
|
2024-04-06 16:49:34 +02:00
|
|
|
c.fallbackPort = 0
|
|
|
|
if fallback {
|
|
|
|
c.fallbackPort = DefaultPort
|
|
|
|
}
|
2024-01-25 13:39:02 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-09-27 11:58:08 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
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
|
2024-10-01 20:44:50 +02:00
|
|
|
c.smtpAuth = nil
|
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 {
|
2024-09-26 11:51:30 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
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
|
|
|
}
|
2024-09-26 11:51:30 +02:00
|
|
|
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-09-26 11:51:30 +02:00
|
|
|
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-09-26 11:51:30 +02:00
|
|
|
client, err := smtp.NewClient(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 {
|
2024-07-24 13:12:06 +02:00
|
|
|
return fmt.Errorf("failed to close connection: %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-09-26 11:51:30 +02:00
|
|
|
if !c.smtpClient.HasConnection() {
|
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-09-26 11:51:30 +02:00
|
|
|
if err := c.smtpClient.UpdateDeadline(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-09-26 11:51:30 +02:00
|
|
|
if !c.smtpClient.HasConnection() {
|
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-10-01 15:28:10 +02:00
|
|
|
tlsConnState, err := c.smtpClient.GetTLSConnectionState()
|
|
|
|
if err != nil {
|
2024-10-01 17:23:29 +02:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, smtp.ErrNonTLSConnection):
|
|
|
|
c.isEncrypted = false
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("failed to get TLS connection state: %w", err)
|
|
|
|
}
|
2024-10-01 15:28:10 +02:00
|
|
|
}
|
|
|
|
c.isEncrypted = tlsConnState.HandshakeComplete
|
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)
|
2024-10-01 11:04:16 +02:00
|
|
|
case SMTPAuthSCRAMSHA1:
|
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthSCRAMSHA1)) {
|
2024-10-01 17:00:57 +02:00
|
|
|
return ErrSCRAMSHA1AuthNotSupported
|
2024-10-01 11:04:16 +02:00
|
|
|
}
|
|
|
|
c.smtpAuth = smtp.ScramSHA1Auth(c.user, c.pass)
|
2024-10-01 15:28:10 +02:00
|
|
|
case SMTPAuthSCRAMSHA256:
|
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthSCRAMSHA256)) {
|
2024-10-01 17:00:57 +02:00
|
|
|
return ErrSCRAMSHA256AuthNotSupported
|
2024-10-01 15:28:10 +02:00
|
|
|
}
|
|
|
|
c.smtpAuth = smtp.ScramSHA256Auth(c.user, c.pass)
|
2024-10-01 11:04:16 +02:00
|
|
|
case SMTPAuthSCRAMSHA1PLUS:
|
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthSCRAMSHA1PLUS)) {
|
2024-10-01 17:00:57 +02:00
|
|
|
return ErrSCRAMSHA1PLUSAuthNotSupported
|
2024-10-01 11:04:16 +02:00
|
|
|
}
|
|
|
|
tlsConnState, err := c.smtpClient.GetTLSConnectionState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.smtpAuth = smtp.ScramSHA1PlusAuth(c.user, c.pass, tlsConnState)
|
|
|
|
case SMTPAuthSCRAMSHA256PLUS:
|
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthSCRAMSHA256PLUS)) {
|
2024-10-01 17:00:57 +02:00
|
|
|
return ErrSCRAMSHA256PLUSAuthNotSupported
|
2024-10-01 11:04:16 +02:00
|
|
|
}
|
|
|
|
tlsConnState, err := c.smtpClient.GetTLSConnectionState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.smtpAuth = smtp.ScramSHA256PlusAuth(c.user, c.pass, tlsConnState)
|
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
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
|
|
|
|
// sendSingleMsg sends out a single message and returns an error if the transmission/delivery fails.
|
|
|
|
// It is invoked by the public Send methods
|
|
|
|
func (c *Client) sendSingleMsg(message *Msg) error {
|
2024-09-27 14:03:50 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2024-09-19 10:56:01 +02:00
|
|
|
if message.encoding == NoEncoding {
|
|
|
|
if ok, _ := c.smtpClient.Extension("8BITMIME"); !ok {
|
2024-09-19 12:09:23 +02:00
|
|
|
return &SendError{Reason: ErrNoUnencoded, isTemp: false, affectedMsg: message}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
from, err := message.GetSender(false)
|
|
|
|
if err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrGetSender, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message,
|
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
rcpts, err := message.GetRecipients()
|
|
|
|
if err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrGetRcpts, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message,
|
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
|
2024-10-04 20:29:14 +02:00
|
|
|
if c.requestDSN {
|
2024-10-04 20:30:43 +02:00
|
|
|
if c.dsnReturnType != "" {
|
|
|
|
c.smtpClient.SetDSNMailReturnOption(string(c.dsnReturnType))
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = c.smtpClient.Mail(from); err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
retError := &SendError{
|
|
|
|
Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message,
|
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
if resetSendErr := c.smtpClient.Reset(); resetSendErr != nil {
|
|
|
|
retError.errlist = append(retError.errlist, resetSendErr)
|
|
|
|
}
|
|
|
|
return retError
|
|
|
|
}
|
|
|
|
hasError := false
|
2024-09-19 12:09:23 +02:00
|
|
|
rcptSendErr := &SendError{affectedMsg: message}
|
2024-09-19 10:56:01 +02:00
|
|
|
rcptSendErr.errlist = make([]error, 0)
|
|
|
|
rcptSendErr.rcpt = make([]string, 0)
|
|
|
|
rcptNotifyOpt := strings.Join(c.dsnrntype, ",")
|
|
|
|
c.smtpClient.SetDSNRcptNotifyOption(rcptNotifyOpt)
|
|
|
|
for _, rcpt := range rcpts {
|
|
|
|
if err = c.smtpClient.Rcpt(rcpt); err != nil {
|
|
|
|
rcptSendErr.Reason = ErrSMTPRcptTo
|
|
|
|
rcptSendErr.errlist = append(rcptSendErr.errlist, err)
|
|
|
|
rcptSendErr.rcpt = append(rcptSendErr.rcpt, rcpt)
|
|
|
|
rcptSendErr.isTemp = isTempError(err)
|
|
|
|
hasError = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasError {
|
|
|
|
if resetSendErr := c.smtpClient.Reset(); resetSendErr != nil {
|
|
|
|
rcptSendErr.errlist = append(rcptSendErr.errlist, resetSendErr)
|
|
|
|
}
|
|
|
|
return rcptSendErr
|
|
|
|
}
|
|
|
|
writer, err := c.smtpClient.Data()
|
|
|
|
if err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message,
|
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
_, err = message.WriteTo(writer)
|
|
|
|
if err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrWriteContent, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message,
|
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
message.isDelivered = true
|
|
|
|
|
|
|
|
if err = writer.Close(); err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message,
|
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.Reset(); err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrSMTPReset, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message,
|
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
if err = c.checkConn(); err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message,
|
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|