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).
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:13:13 +02:00
|
|
|
// 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.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:13:13 +02:00
|
|
|
// 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.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:13:13 +02:00
|
|
|
// 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.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:13:13 +02:00
|
|
|
// 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.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:13:13 +02:00
|
|
|
// 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.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:13:13 +02:00
|
|
|
// 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 (
|
|
|
|
// 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).
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:29:14 +02:00
|
|
|
// 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.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:29:14 +02:00
|
|
|
// 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-06 12:39:02 +02:00
|
|
|
// Client is responsible for connecting and interacting with an SMTP server.
|
|
|
|
//
|
|
|
|
// This struct represents the go-mail client, which manages the connection, authentication, and communication
|
|
|
|
// with an SMTP server. It contains various configuration options, including connection timeouts, encryption
|
|
|
|
// settings, authentication methods, and Delivery Status Notification (DSN) preferences.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://datatracker.ietf.org/doc/html/rfc3207#section-2
|
|
|
|
// - https://datatracker.ietf.org/doc/html/rfc8314
|
2024-10-04 20:29:14 +02:00
|
|
|
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:39:14 +02:00
|
|
|
// dsnRcptNotifyType represents the different types of notifications for DSN (Delivery Status Notifications)
|
|
|
|
// receipts.
|
|
|
|
dsnRcptNotifyType []string
|
2022-12-26 13:59:42 +01:00
|
|
|
|
2024-10-04 20:39:14 +02:00
|
|
|
// dsnReturnType specifies the type of Delivery Status Notification (DSN) that should be requested for an
|
|
|
|
// email.
|
|
|
|
dsnReturnType DSNMailReturnOption
|
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.
|
2024-10-04 20:39:14 +02:00
|
|
|
//
|
|
|
|
// The fallbackPort is only used in combination with SetTLSPortPolicy and SetSSLPort correspondingly.
|
2024-10-04 20:29:14 +02:00
|
|
|
fallbackPort int
|
2022-03-10 10:53:38 +01:00
|
|
|
|
2024-10-04 20:39:14 +02:00
|
|
|
// helo is the hostname used in the HELO/EHLO greeting, that is sent to the target SMTP server.
|
2024-10-04 20:57:51 +02:00
|
|
|
//
|
|
|
|
// helo might be different as host. This can be useful in a shared-hosting scenario.
|
2024-10-04 20:29:14 +02:00
|
|
|
helo string
|
2024-09-27 10:36:09 +02:00
|
|
|
|
2024-10-04 20:39:14 +02:00
|
|
|
// host is the hostname of the SMTP server we are connecting to.
|
2024-10-04 20:29:14 +02:00
|
|
|
host string
|
2024-09-27 10:36:09 +02:00
|
|
|
|
2024-10-15 19:52:59 +02:00
|
|
|
// logAuthData indicates whether authentication-related data should be logged.
|
|
|
|
logAuthData bool
|
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// logger is a logger that satisfies the log.Logger interface.
|
2024-10-04 20:29:14 +02:00
|
|
|
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:57:51 +02:00
|
|
|
// noNoop indicates that the Client should skip the "NOOP" command during the dial.
|
|
|
|
//
|
|
|
|
// This is useful for servers which delay potentially unwanted clients when they perform commands
|
|
|
|
// other than AUTH.
|
2024-10-04 20:29:14 +02:00
|
|
|
noNoop bool
|
2022-03-10 12:10:27 +01:00
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// pass represents a password or a secret token used for the SMTP authentication.
|
2024-10-04 20:29:14 +02:00
|
|
|
pass string
|
2022-03-10 10:53:38 +01:00
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// port specifies the network port that is used to establish the connection with the SMTP server.
|
2024-10-04 20:29:14 +02:00
|
|
|
port int
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// requestDSN indicates wether we want to request DSN (Delivery Status Notifications).
|
2024-10-04 20:29:14 +02:00
|
|
|
requestDSN bool
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-11-21 10:26:48 +01:00
|
|
|
// sendMutex is used to synchronize access to shared resources during the dial and send methods.
|
|
|
|
sendMutex sync.Mutex
|
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// smtpAuth is the authentication type that is used to authenticate the user with SMTP server. It
|
|
|
|
// satisfies the smtp.Auth interface.
|
|
|
|
//
|
|
|
|
// Unless you plan to write you own custom authentication method, it is advised to not set this manually.
|
|
|
|
// You should use one of go-mail's SMTPAuthType, instead.
|
2024-10-04 20:29:14 +02:00
|
|
|
smtpAuth smtp.Auth
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// smtpAuthType specifies the authentication type to be used for SMTP authentication.
|
2024-10-04 20:29:14 +02:00
|
|
|
smtpAuthType SMTPAuthType
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// smtpClient is an instance of smtp.Client used for handling the communication with the SMTP server.
|
2024-10-04 20:29:14 +02:00
|
|
|
smtpClient *smtp.Client
|
2023-02-03 10:19:26 +01:00
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// tlspolicy defines the TLSPolicy configuration the Client uses for the STARTTLS protocol.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:57:51 +02:00
|
|
|
// https://datatracker.ietf.org/doc/html/rfc3207#section-2
|
2024-10-04 20:29:14 +02:00
|
|
|
tlspolicy TLSPolicy
|
2023-04-19 16:20:33 +02:00
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// tlsconfig is a pointer to tls.Config that specifies the TLS configuration for the STARTTLS communication.
|
2024-10-04 20:29:14 +02:00
|
|
|
tlsconfig *tls.Config
|
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// useDebugLog indicates whether debug level logging is enabled for the Client.
|
2024-10-04 20:29:14 +02:00
|
|
|
useDebugLog bool
|
2022-03-05 12:36:53 +01:00
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// user represents a username used for the SMTP authentication.
|
2024-10-04 20:29:14 +02:00
|
|
|
user string
|
|
|
|
|
2024-10-04 20:57:51 +02:00
|
|
|
// useSSL indicates whether to use SSL/TLS encryption for network communication.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-04 20:57:51 +02:00
|
|
|
// https://datatracker.ietf.org/doc/html/rfc8314
|
2024-10-04 20:29:14 +02:00
|
|
|
useSSL bool
|
|
|
|
}
|
|
|
|
)
|
2022-03-05 12:36:53 +01:00
|
|
|
|
2022-03-05 16:27:09 +01:00
|
|
|
var (
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrInvalidPort is returned when the specified port for the SMTP connection is not valid
|
2022-03-15 21:10:03 +01:00
|
|
|
ErrInvalidPort = errors.New("invalid port number")
|
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrInvalidTimeout is returned when the specified timeout is zero or negative.
|
2022-03-15 21:10:03 +01:00
|
|
|
ErrInvalidTimeout = errors.New("timeout cannot be zero or negative")
|
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrInvalidHELO is returned when the HELO/EHLO value is invalid due to being empty.
|
2022-03-15 21:10:03 +01:00
|
|
|
ErrInvalidHELO = errors.New("invalid HELO/EHLO value - must not be empty")
|
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrInvalidTLSConfig is returned when the provided TLS configuration is invalid or nil.
|
2022-03-15 21:10:03 +01:00
|
|
|
ErrInvalidTLSConfig = errors.New("invalid TLS config")
|
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrNoHostname is returned when the hostname for the client is not provided or empty.
|
2022-03-05 16:27:09 +01:00
|
|
|
ErrNoHostname = errors.New("hostname for client cannot be empty")
|
2022-03-10 12:10:27 +01:00
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrDeadlineExtendFailed is returned when an attempt to extend the connection deadline fails.
|
2022-03-10 12:10:27 +01:00
|
|
|
ErrDeadlineExtendFailed = errors.New("connection deadline extension failed")
|
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrNoActiveConnection indicates that there is no active connection to the SMTP server.
|
2022-03-10 12:10:27 +01:00
|
|
|
ErrNoActiveConnection = errors.New("not connected to SMTP server")
|
2022-03-14 10:29:53 +01:00
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrServerNoUnencoded indicates that the server does not support 8BITMIME for unencoded 8-bit messages.
|
2022-03-14 10:29:53 +01:00
|
|
|
ErrServerNoUnencoded = errors.New("message is 8bit unencoded, but server does not support 8BITMIME")
|
2022-09-11 20:24:28 +02:00
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrInvalidDSNMailReturnOption is returned when an invalid DSNMailReturnOption is provided as argument
|
|
|
|
// to the WithDSN Option.
|
2022-09-11 20:24:28 +02:00
|
|
|
ErrInvalidDSNMailReturnOption = errors.New("DSN mail return option can only be HDRS or FULL")
|
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrInvalidDSNRcptNotifyOption is returned when an invalid DSNRcptNotifyOption is provided as argument
|
|
|
|
// to the WithDSN Option.
|
2022-09-11 20:24:28 +02:00
|
|
|
ErrInvalidDSNRcptNotifyOption = errors.New("DSN rcpt notify option can only be: NEVER, " +
|
|
|
|
"SUCCESS, FAILURE or DELAY")
|
|
|
|
|
2024-10-04 21:12:05 +02:00
|
|
|
// ErrInvalidDSNRcptNotifyCombination is returned when an invalid combination of DSNRcptNotifyOption is
|
|
|
|
// provided as argument to the WithDSN Option.
|
2022-09-11 20:24:28 +02:00
|
|
|
ErrInvalidDSNRcptNotifyCombination = errors.New("DSN rcpt notify option NEVER cannot be " +
|
|
|
|
"combined with any of SUCCESS, FAILURE or DELAY")
|
2024-10-23 13:10:13 +02:00
|
|
|
|
|
|
|
// ErrSMTPAuthMethodIsNil indicates that the SMTP authentication method provided is nil
|
|
|
|
ErrSMTPAuthMethodIsNil = errors.New("SMTP auth method is nil")
|
2024-10-23 13:51:31 +02:00
|
|
|
|
|
|
|
// ErrDialContextFuncIsNil indicates that a required dial context function is not provided.
|
|
|
|
ErrDialContextFuncIsNil = errors.New("dial context function is nil")
|
2022-03-05 16:27:09 +01:00
|
|
|
)
|
|
|
|
|
2024-10-04 21:26:29 +02:00
|
|
|
// NewClient creates a new Client instance with the provided host and optional configuration Option functions.
|
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// This function initializes a Client with default values, such as connection timeout, port, TLS settings,
|
|
|
|
// and the HELO/EHLO hostname. Option functions, if provided, can override the default configuration.
|
|
|
|
// It ensures that essential values, like the host, are set. An error is returned if critical defaults are unset.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - host: The hostname of the SMTP server to connect to.
|
|
|
|
// - opts: Optional configuration functions to override default settings.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A pointer to the initialized Client.
|
|
|
|
// - An error if any critical default values are missing or options fail to apply.
|
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-10-11 11:21:56 +02:00
|
|
|
smtpAuthType: SMTPAuthNoAuth,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithPort sets the port number for the Client and overrides the default port.
|
|
|
|
//
|
|
|
|
// This function sets the specified port number for the Client, ensuring that the port number is valid
|
|
|
|
// (between 1 and 65535). If the provided port number is invalid, an error is returned.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - port: The port number to be used by the Client. Must be between 1 and 65535.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that applies the port setting to the Client.
|
|
|
|
// - An error if the port number is outside the valid range.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithTimeout sets the connection timeout for the Client and overrides the default timeout.
|
|
|
|
//
|
|
|
|
// This function configures the Client with a specified connection timeout duration. It validates that the
|
|
|
|
// provided timeout is greater than zero. If the timeout is invalid, an error is returned.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - timeout: The duration to be set as the connection timeout. Must be greater than zero.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that applies the timeout setting to the Client.
|
|
|
|
// - An error if the timeout duration is invalid.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-04 21:26:29 +02:00
|
|
|
// WithSSL enables implicit SSL/TLS for the Client.
|
2024-10-06 12:39:02 +02:00
|
|
|
//
|
|
|
|
// This function configures the Client to use implicit SSL/TLS for secure communication.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that enables SSL/TLS for the Client.
|
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-10-04 21:38:39 +02:00
|
|
|
// WithSSLPort enables implicit SSL/TLS with an optional fallback for the Client. The correct port is
|
|
|
|
// automatically set.
|
2024-01-25 13:39:02 +01:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// When this option is used with NewClient, the default port 25 is overridden with port 465 for SSL/TLS connections.
|
|
|
|
// If fallback is set to true and the SSL/TLS connection fails, the Client attempts to connect on port 25 using an
|
|
|
|
// unencrypted connection. If WithPort has already been used to set a different port, that port takes precedence,
|
|
|
|
// and the automatic fallback mechanism is skipped.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - fallback: A boolean indicating whether to fall back to port 25 without SSL/TLS if the connection fails.
|
2024-04-06 16:49:34 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Returns:
|
|
|
|
// - An Option function that enables SSL/TLS and configures the fallback mechanism for the Client.
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithDebugLog enables debug logging for the Client.
|
2024-10-04 21:38:39 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// This function activates debug logging, which logs incoming and outgoing communication between the
|
2024-10-15 19:52:59 +02:00
|
|
|
// Client and the SMTP server to os.Stderr. By default the debug logging will redact any kind of SMTP
|
|
|
|
// authentication data. If you need access to the actual authentication data in your logs, you can
|
|
|
|
// enable authentication data logging with the WithLogAuthData option or by setting it with the
|
|
|
|
// Client.SetLogAuthData method.
|
2024-10-06 12:39:02 +02:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that enables debug logging for the Client.
|
2023-01-14 12:47:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithLogger defines a custom logger for the Client.
|
|
|
|
//
|
|
|
|
// This function sets a custom logger for the Client, which must satisfy the log.Logger interface. The custom
|
|
|
|
// logger is used only when debug logging is enabled. By default, log.Stdlog is used if no custom logger is provided.
|
2024-10-04 21:38:39 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Parameters:
|
|
|
|
// - logger: A logger that satisfies the log.Logger interface.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the custom logger for the Client.
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithHELO sets the HELO/EHLO string used by the Client.
|
|
|
|
//
|
|
|
|
// This function configures the HELO/EHLO string sent by the Client when initiating communication
|
|
|
|
// with the SMTP server. By default, os.Hostname is used to identify the HELO/EHLO string.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - helo: The string to be used for the HELO/EHLO greeting. Must not be empty.
|
2024-10-04 21:38:39 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the HELO/EHLO string for the Client.
|
|
|
|
// - An error if the provided HELO string is empty.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithTLSPolicy sets the TLSPolicy of the Client and overrides the DefaultTLSPolicy.
|
|
|
|
//
|
|
|
|
// This function configures the Client's TLSPolicy, specifying how the Client handles TLS for SMTP connections.
|
|
|
|
// It overrides the default policy. For best practices regarding SMTP TLS connections, it is recommended to use
|
|
|
|
// WithTLSPortPolicy instead.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - policy: The TLSPolicy to be applied to the Client.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the TLSPolicy for the Client.
|
2024-01-25 13:39:02 +01:00
|
|
|
//
|
2024-10-04 21:45:12 +02:00
|
|
|
// 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-10-04 21:45:12 +02:00
|
|
|
// WithTLSPortPolicy enables explicit TLS via STARTTLS for the Client using the provided TLSPolicy. The
|
|
|
|
// correct port is automatically set.
|
2024-01-25 13:39:02 +01:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// When TLSMandatory or TLSOpportunistic is provided as the TLSPolicy, port 587 is used for the connection.
|
|
|
|
// If the connection fails with TLSOpportunistic, the Client attempts to connect on port 25 using an unencrypted
|
|
|
|
// connection as a fallback. If NoTLS is specified, the Client will always use port 25.
|
|
|
|
// If WithPort has already been used to set a different port, that port takes precedence, and the automatic fallback
|
|
|
|
// mechanism is skipped.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - policy: The TLSPolicy to be used for STARTTLS communication.
|
2024-04-06 16:49:34 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the TLSPortPolicy for the Client.
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithTLSConfig sets the tls.Config for the Client and overrides the default configuration.
|
|
|
|
//
|
|
|
|
// This function configures the Client with a custom tls.Config. It overrides the default TLS settings.
|
|
|
|
// An error is returned if the provided tls.Config is nil or invalid.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - tlsconfig: A pointer to a tls.Config struct to be used for the Client. Must not be nil.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the tls.Config for the Client.
|
|
|
|
// - An error if the provided tls.Config is invalid.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithSMTPAuth configures the Client to use the specified SMTPAuthType for SMTP authentication.
|
|
|
|
//
|
|
|
|
// This function sets the Client to use the specified SMTPAuthType for authenticating with the SMTP server.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - authtype: The SMTPAuthType to be used for SMTP authentication.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that configures the Client to use the specified SMTPAuthType.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithSMTPAuthCustom sets a custom SMTP authentication mechanism for the Client.
|
|
|
|
//
|
|
|
|
// This function configures the Client to use a custom SMTP authentication mechanism. The provided
|
|
|
|
// mechanism must satisfy the smtp.Auth interface.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - smtpAuth: The custom SMTP authentication mechanism, which must implement the smtp.Auth interface.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the custom SMTP authentication for the Client.
|
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-10-23 13:10:13 +02:00
|
|
|
if smtpAuth == nil {
|
|
|
|
return ErrSMTPAuthMethodIsNil
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpAuth = smtpAuth
|
2024-10-04 22:33:42 +02:00
|
|
|
c.smtpAuthType = SMTPAuthCustom
|
2022-03-15 21:10:03 +01:00
|
|
|
return nil
|
2022-03-10 10:53:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithUsername sets the username that the Client will use for SMTP authentication.
|
|
|
|
//
|
|
|
|
// This function configures the Client with the specified username for SMTP authentication.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - username: The username to be used for SMTP authentication.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the username for the Client.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithPassword sets the password that the Client will use for SMTP authentication.
|
|
|
|
//
|
|
|
|
// This function configures the Client with the specified password for SMTP authentication.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - password: The password to be used for SMTP authentication.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the password for the Client.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithDSN enables DSN (Delivery Status Notifications) for the Client as described in RFC 1891.
|
2024-10-04 21:57:38 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// This function configures the Client to request DSN, which provides status notifications for email delivery.
|
|
|
|
// DSN is only effective if the SMTP server supports it. By default, DSNMailReturnOption is set to DSNMailReturnFull,
|
|
|
|
// and DSNRcptNotifyOption is set to DSNRcptNotifySuccess and DSNRcptNotifyFailure.
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Returns:
|
|
|
|
// - An Option function that enables DSN for the Client.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://datatracker.ietf.org/doc/html/rfc1891
|
2022-09-11 20:24:28 +02:00
|
|
|
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
|
2024-10-04 20:39:14 +02:00
|
|
|
c.dsnRcptNotifyType = []string{string(DSNRcptNotifyFailure), string(DSNRcptNotifySuccess)}
|
2022-09-11 20:24:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithDSNMailReturnType enables DSN (Delivery Status Notifications) for the Client as described in RFC 1891.
|
|
|
|
//
|
|
|
|
// This function configures the Client to request DSN and sets the DSNMailReturnOption to the provided value.
|
|
|
|
// DSN is only effective if the SMTP server supports it. The provided option must be either DSNMailReturnHeadersOnly
|
|
|
|
// or DSNMailReturnFull; otherwise, an error is returned.
|
2024-10-04 21:57:38 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Parameters:
|
|
|
|
// - option: The DSNMailReturnOption to be used (DSNMailReturnHeadersOnly or DSNMailReturnFull).
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the DSNMailReturnOption for the Client.
|
|
|
|
// - An error if an invalid DSNMailReturnOption is provided.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://datatracker.ietf.org/doc/html/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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithDSNRcptNotifyType enables DSN (Delivery Status Notifications) for the Client as described in RFC 1891.
|
|
|
|
//
|
|
|
|
// This function configures the Client to request DSN and sets the DSNRcptNotifyOption to the provided values.
|
|
|
|
// The provided options must be valid DSNRcptNotifyOption types. If DSNRcptNotifyNever is combined with
|
|
|
|
// any other notification type (such as DSNRcptNotifySuccess, DSNRcptNotifyFailure, or DSNRcptNotifyDelay),
|
|
|
|
// an error is returned.
|
2024-10-04 21:57:38 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Parameters:
|
|
|
|
// - opts: A variadic list of DSNRcptNotifyOption values (e.g., DSNRcptNotifySuccess, DSNRcptNotifyFailure).
|
2024-10-05 13:39:21 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the DSNRcptNotifyOption for the Client.
|
|
|
|
// - An error if invalid DSNRcptNotifyOption values are provided or incompatible combinations are used.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://datatracker.ietf.org/doc/html/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-10-04 20:39:14 +02:00
|
|
|
c.dsnRcptNotifyType = rcptOpts
|
2022-09-11 20:24:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-04 22:23:18 +02:00
|
|
|
// WithoutNoop indicates that the Client should skip the "NOOP" command during the dial.
|
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// This option is useful for servers that delay potentially unwanted clients when they perform
|
|
|
|
// commands other than AUTH, such as Microsoft's Exchange Tarpit.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that configures the Client to skip the "NOOP" command.
|
2022-12-26 13:59:42 +01:00
|
|
|
func WithoutNoop() Option {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.noNoop = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// WithDialContextFunc sets the provided DialContextFunc as the DialContext for connecting to the SMTP server.
|
|
|
|
//
|
|
|
|
// This function overrides the default DialContext function used by the Client when establishing a connection
|
|
|
|
// to the SMTP server with the provided DialContextFunc.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - dialCtxFunc: The custom DialContextFunc to be used for connecting to the SMTP server.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that sets the custom DialContextFunc for the Client.
|
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-10-23 13:51:31 +02:00
|
|
|
if dialCtxFunc == nil {
|
|
|
|
return ErrDialContextFuncIsNil
|
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
c.dialContextFunc = dialCtxFunc
|
2023-04-19 16:20:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 19:52:59 +02:00
|
|
|
// WithLogAuthData enables logging of authentication data.
|
|
|
|
//
|
|
|
|
// This function sets the logAuthData field of the Client to true, enabling the logging of authentication data.
|
|
|
|
//
|
|
|
|
// Be cautious when using this option, as the logs may include unencrypted authentication data, depending on
|
|
|
|
// the SMTP authentication method in use, which could pose a data protection risk.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An Option function that configures the Client to enable authentication data logging.
|
|
|
|
func WithLogAuthData() Option {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.logAuthData = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// TLSPolicy returns the TLSPolicy that is currently set on the Client as a string.
|
|
|
|
//
|
|
|
|
// This method retrieves the current TLSPolicy configured for the Client and returns it as a string representation.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A string representing the currently set TLSPolicy for the Client.
|
2022-03-07 16:24:49 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-04 22:23:18 +02:00
|
|
|
// ServerAddr returns the server address that is currently set on the Client in the format "host:port".
|
2024-10-06 12:39:02 +02:00
|
|
|
//
|
|
|
|
// This method constructs and returns the server address using the host and port currently configured
|
|
|
|
// for the Client.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A string representing the server address in the format "host:port".
|
2022-03-07 18:14:38 +01:00
|
|
|
func (c *Client) ServerAddr() string {
|
|
|
|
return fmt.Sprintf("%s:%d", c.host, c.port)
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// SetTLSPolicy sets or overrides the TLSPolicy currently configured on the Client with the given TLSPolicy.
|
2024-04-06 16:49:34 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// This method allows the user to set a new TLSPolicy for the Client. For best practices regarding
|
|
|
|
// SMTP TLS connections, it is recommended to use SetTLSPortPolicy instead.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - policy: The TLSPolicy to be set for the Client.
|
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-10-06 12:39:02 +02:00
|
|
|
// SetTLSPortPolicy sets or overrides the TLSPolicy currently configured on the Client with the given TLSPolicy.
|
|
|
|
// The correct port is automatically set based on the specified policy.
|
2024-01-25 13:39:02 +01:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// If TLSMandatory or TLSOpportunistic is provided as the TLSPolicy, port 587 will be used for the connection.
|
2024-10-04 22:23:18 +02:00
|
|
|
// If the connection fails with TLSOpportunistic, the Client will attempt to connect on port 25 using
|
2024-10-06 12:39:02 +02:00
|
|
|
// an unencrypted connection as a fallback. If NoTLS is provided, the Client will always use port 25.
|
|
|
|
//
|
|
|
|
// Note: If a different port has already been set using WithPort, that port takes precedence and is used
|
|
|
|
// to establish the SSL/TLS connection, skipping the automatic fallback mechanism.
|
2024-04-06 16:49:34 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Parameters:
|
|
|
|
// - policy: The TLSPolicy to be set for the Client.
|
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-10-23 14:39:28 +02:00
|
|
|
c.fallbackPort = 0
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// SetSSL sets or overrides whether the Client should use implicit SSL/TLS.
|
|
|
|
//
|
|
|
|
// This method configures the Client to either enable or disable implicit SSL/TLS for secure communication.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - ssl: A boolean value indicating whether to enable (true) or disable (false) implicit SSL/TLS.
|
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-10-06 12:39:02 +02:00
|
|
|
// SetSSLPort sets or overrides whether the Client should use implicit SSL/TLS with optional fallback.
|
|
|
|
// The correct port is automatically set.
|
|
|
|
//
|
|
|
|
// If ssl is set to true, the default port 25 will be overridden with port 465. If fallback is set to true
|
|
|
|
// and the SSL/TLS connection fails, the Client will attempt to connect on port 25 using an unencrypted
|
|
|
|
// connection.
|
2024-01-25 13:39:02 +01:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Note: If a different port has already been set using WithPort, that port takes precedence and is used
|
|
|
|
// to establish the SSL/TLS connection, skipping the automatic fallback mechanism.
|
2024-04-06 16:49:34 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Parameters:
|
|
|
|
// - ssl: A boolean value indicating whether to enable implicit SSL/TLS.
|
|
|
|
// - fallback: A boolean value indicating whether to enable fallback to an unencrypted connection.
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// SetDebugLog sets or overrides whether the Client is using debug logging. The debug logger will log incoming
|
2024-11-22 15:55:45 +01:00
|
|
|
// and outgoing communication between the client and the server to log.Logger that is defined on the Client.
|
2024-10-04 22:23:18 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Note: The SMTP communication might include unencrypted authentication data, depending on whether you are using
|
|
|
|
// SMTP authentication and the type of authentication mechanism. This could pose a data protection risk. Use
|
|
|
|
// debug logging with caution.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - val: A boolean value indicating whether to enable (true) or disable (false) debug logging.
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetDebugLog(val bool) {
|
2024-11-22 15:29:22 +01:00
|
|
|
c.SetDebugLogWithSMTPClient(c.smtpClient, val)
|
|
|
|
}
|
|
|
|
|
2024-11-22 15:55:45 +01:00
|
|
|
// SetDebugLogWithSMTPClient sets or overrides whether the provided smtp.Client is using debug logging.
|
|
|
|
// The debug logger will log incoming and outgoing communication between the client and the server to
|
|
|
|
// log.Logger that is defined on the Client.
|
|
|
|
//
|
|
|
|
// Note: The SMTP communication might include unencrypted authentication data, depending on whether you are using
|
|
|
|
// SMTP authentication and the type of authentication mechanism. This could pose a data protection risk. Use
|
|
|
|
// debug logging with caution.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - client: A pointer to the smtp.Client that handles the connection to the server.
|
|
|
|
// - val: A boolean value indicating whether to enable (true) or disable (false) debug logging.
|
2024-11-22 15:29:22 +01:00
|
|
|
func (c *Client) SetDebugLogWithSMTPClient(client *smtp.Client, val bool) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2024-02-24 22:06:18 +01:00
|
|
|
c.useDebugLog = val
|
2024-11-22 15:29:22 +01:00
|
|
|
if client != nil {
|
|
|
|
client.SetDebugLog(val)
|
2023-01-14 13:05:04 +01:00
|
|
|
}
|
2023-01-14 12:47:51 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// SetLogger sets or overrides the custom logger currently used by the Client. The logger must
|
|
|
|
// satisfy the log.Logger interface and is only utilized when debug logging is enabled on the
|
|
|
|
// Client.
|
|
|
|
//
|
|
|
|
// By default, log.Stdlog is used if no custom logger is provided.
|
2024-10-04 22:23:18 +02:00
|
|
|
//
|
2024-10-06 12:39:02 +02:00
|
|
|
// Parameters:
|
|
|
|
// - logger: A logger that satisfies the log.Logger interface to be set for the Client.
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetLogger(logger log.Logger) {
|
2024-11-22 15:29:22 +01:00
|
|
|
c.SetLoggerWithSMTPClient(c.smtpClient, logger)
|
|
|
|
}
|
|
|
|
|
2024-11-22 15:55:45 +01:00
|
|
|
// SetLoggerWithSMTPClient sets or overrides the custom logger currently used by the provided smtp.Client.
|
|
|
|
// The logger must satisfy the log.Logger interface and is only utilized when debug logging is enabled on
|
|
|
|
// the provided smtp.Client.
|
|
|
|
//
|
|
|
|
// By default, log.Stdlog is used if no custom logger is provided.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - client: A pointer to the smtp.Client that handles the connection to the server.
|
|
|
|
// - logger: A logger that satisfies the log.Logger interface to be set for the Client.
|
2024-11-22 15:29:22 +01:00
|
|
|
func (c *Client) SetLoggerWithSMTPClient(client *smtp.Client, logger log.Logger) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2024-02-24 22:06:18 +01:00
|
|
|
c.logger = logger
|
2024-11-22 15:29:22 +01:00
|
|
|
if client != nil {
|
|
|
|
client.SetLogger(logger)
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// SetTLSConfig sets or overrides the tls.Config currently configured for the Client with the
|
|
|
|
// given value. An error is returned if the provided tls.Config is invalid.
|
|
|
|
//
|
|
|
|
// This method ensures that the provided tls.Config is not nil before updating the Client's
|
|
|
|
// TLS configuration.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - tlsconfig: A pointer to the tls.Config struct to be set for the Client. Must not be nil.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if the provided tls.Config is invalid or nil.
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 12:39:02 +02:00
|
|
|
// SetUsername sets or overrides the username that the Client will use for SMTP authentication.
|
|
|
|
//
|
|
|
|
// This method updates the username used by the Client for authenticating with the SMTP server.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - username: The username to be set for SMTP authentication.
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// SetPassword sets or overrides the password that the Client will use for SMTP authentication.
|
|
|
|
//
|
|
|
|
// This method updates the password used by the Client for authenticating with the SMTP server.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - password: The password to be set for SMTP authentication.
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// SetSMTPAuth sets or overrides the SMTPAuthType currently configured on the Client for SMTP
|
2024-10-04 22:33:42 +02:00
|
|
|
// authentication.
|
2024-10-06 13:39:40 +02:00
|
|
|
//
|
|
|
|
// This method updates the authentication type used by the Client for authenticating with the
|
|
|
|
// SMTP server and resets any custom SMTP authentication mechanism.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - authtype: The SMTPAuthType to be set for the Client.
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// SetSMTPAuthCustom sets or overrides the custom SMTP authentication mechanism currently
|
|
|
|
// configured for the Client. The provided authentication mechanism must satisfy the
|
|
|
|
// smtp.Auth interface.
|
|
|
|
//
|
|
|
|
// This method updates the authentication mechanism used by the Client for authenticating
|
|
|
|
// with the SMTP server and sets the authentication type to SMTPAuthCustom.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - smtpAuth: The custom SMTP authentication mechanism to be set for the Client.
|
2024-02-24 22:06:18 +01:00
|
|
|
func (c *Client) SetSMTPAuthCustom(smtpAuth smtp.Auth) {
|
|
|
|
c.smtpAuth = smtpAuth
|
2024-10-04 22:33:42 +02:00
|
|
|
c.smtpAuthType = SMTPAuthCustom
|
2022-03-07 16:24:49 +01:00
|
|
|
}
|
|
|
|
|
2024-10-15 19:52:59 +02:00
|
|
|
// SetLogAuthData sets or overrides the logging of SMTP authentication data for the Client.
|
|
|
|
//
|
|
|
|
// This function sets the logAuthData field of the Client to true, enabling the logging of authentication data.
|
|
|
|
//
|
|
|
|
// Be cautious when using this option, as the logs may include unencrypted authentication data, depending on
|
|
|
|
// the SMTP authentication method in use, which could pose a data protection risk.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - logAuth: Set wether or not to log SMTP authentication data for the Client.
|
|
|
|
func (c *Client) SetLogAuthData(logAuth bool) {
|
|
|
|
c.logAuthData = logAuth
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:15:01 +02:00
|
|
|
// DialWithContext establishes a connection to the server using the provided context.Context.
|
|
|
|
//
|
2024-10-06 13:39:40 +02:00
|
|
|
// This function adds a deadline based on the Client's timeout to the provided context.Context
|
|
|
|
// before connecting to the server. After dialing the defined DialContextFunc and successfully
|
|
|
|
// establishing the connection, it sends the HELO/EHLO SMTP command, followed by optional
|
|
|
|
// STARTTLS and SMTP AUTH commands. If debug logging is enabled, it attaches the log.Logger.
|
|
|
|
//
|
|
|
|
// After this method is called, the Client will have an active (cancelable) connection to the
|
|
|
|
// SMTP server.
|
2024-10-04 23:15:01 +02:00
|
|
|
//
|
2024-10-06 13:39:40 +02:00
|
|
|
// Parameters:
|
2024-11-22 16:06:28 +01:00
|
|
|
// - ctxDial: The context.Context used to control the connection timeout and cancellation.
|
2024-10-04 23:15:01 +02:00
|
|
|
//
|
2024-10-06 13:39:40 +02:00
|
|
|
// Returns:
|
|
|
|
// - An error if the connection to the SMTP server fails or any subsequent command fails.
|
2024-11-22 16:06:28 +01:00
|
|
|
func (c *Client) DialWithContext(ctxDial context.Context) error {
|
|
|
|
client, err := c.DialToSMTPClientWithContext(ctxDial)
|
2024-11-22 14:59:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-26 11:51:30 +02:00
|
|
|
c.mutex.Lock()
|
2024-11-22 14:59:19 +01:00
|
|
|
c.smtpClient = client
|
|
|
|
c.mutex.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-22 16:06:28 +01:00
|
|
|
// DialToSMTPClientWithContext establishes and configures a smtp.Client connection using
|
|
|
|
// the provided context.
|
|
|
|
//
|
|
|
|
// This function uses the provided context to manage the connection deadline and cancellation.
|
|
|
|
// It dials the SMTP server using the Client's configured DialContextFunc or a default dialer.
|
|
|
|
// If SSL is enabled, it uses a TLS connection. After successfully connecting, it initializes
|
|
|
|
// an smtp.Client, sends the HELO/EHLO command, and optionally performs STARTTLS and SMTP AUTH
|
|
|
|
// based on the Client's configuration. Debug and authentication logging are enabled if
|
|
|
|
// configured.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - ctxDial: The context used to control the connection timeout and cancellation.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A pointer to the initialized smtp.Client.
|
|
|
|
// - An error if the connection fails, the smtp.Client cannot be created, or any subsequent commands fail.
|
|
|
|
func (c *Client) DialToSMTPClientWithContext(ctxDial context.Context) (*smtp.Client, error) {
|
2024-11-22 14:59:19 +01:00
|
|
|
c.mutex.RLock()
|
|
|
|
defer c.mutex.RUnlock()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithDeadline(ctxDial, time.Now().Add(c.connTimeout))
|
2024-02-24 22:06:18 +01:00
|
|
|
defer cancel()
|
2022-03-05 16:27:09 +01:00
|
|
|
|
2024-11-22 14:59:19 +01:00
|
|
|
isEncrypted := false
|
|
|
|
dialContextFunc := c.dialContextFunc
|
2023-04-19 16:20:33 +02:00
|
|
|
if c.dialContextFunc == nil {
|
2024-02-24 22:06:18 +01:00
|
|
|
netDialer := net.Dialer{}
|
2024-11-22 14:59:19 +01:00
|
|
|
dialContextFunc = netDialer.DialContext
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.useSSL {
|
|
|
|
tlsDialer := tls.Dialer{NetDialer: &netDialer, Config: c.tlsconfig}
|
2024-11-22 14:59:19 +01:00
|
|
|
isEncrypted = true
|
|
|
|
dialContextFunc = tlsDialer.DialContext
|
2023-04-19 16:20:33 +02:00
|
|
|
}
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
connection, err := 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-11-22 14:59:19 +01:00
|
|
|
connection, err = dialContextFunc(ctx, "tcp", c.serverFallbackAddr())
|
2024-01-25 13:39:02 +01:00
|
|
|
}
|
2022-03-05 16:27:09 +01:00
|
|
|
if err != nil {
|
2024-11-22 14:59:19 +01:00
|
|
|
return nil, 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 {
|
2024-11-22 14:59:19 +01:00
|
|
|
return nil, err
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
2023-11-29 16:25:35 +01:00
|
|
|
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.logger != nil {
|
2024-11-22 14:59:19 +01:00
|
|
|
client.SetLogger(c.logger)
|
2023-02-03 10:19:26 +01:00
|
|
|
}
|
2024-02-24 22:06:18 +01:00
|
|
|
if c.useDebugLog {
|
2024-11-22 14:59:19 +01:00
|
|
|
client.SetDebugLog(true)
|
2023-01-14 12:47:51 +01:00
|
|
|
}
|
2024-10-15 19:52:59 +02:00
|
|
|
if c.logAuthData {
|
2024-11-22 14:59:19 +01:00
|
|
|
client.SetLogAuthData()
|
2024-10-15 19:52:59 +02:00
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
if err = client.Hello(c.helo); err != nil {
|
|
|
|
return nil, err
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
|
|
|
|
2024-11-22 14:59:19 +01:00
|
|
|
if err = c.tls(client, &isEncrypted); err != nil {
|
|
|
|
return nil, err
|
2022-03-05 16:27:09 +01:00
|
|
|
}
|
2022-03-07 16:24:49 +01:00
|
|
|
|
2024-11-22 14:59:19 +01:00
|
|
|
if err = c.auth(client, isEncrypted); err != nil {
|
|
|
|
return nil, err
|
2022-03-10 10:53:38 +01:00
|
|
|
}
|
|
|
|
|
2024-11-22 14:59:19 +01:00
|
|
|
return client, nil
|
2022-03-10 10:53:38 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// Close terminates the connection to the SMTP server, returning an error if the disconnection
|
|
|
|
// fails. If the connection is already closed, this method is a no-op and disregards any error.
|
|
|
|
//
|
|
|
|
// This function checks if the Client's SMTP connection is active. If not, it simply returns
|
|
|
|
// without any action. If the connection is active, it attempts to gracefully close the
|
|
|
|
// connection using the Quit method.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if the disconnection fails; otherwise, returns nil.
|
2022-03-10 16:19:51 +01:00
|
|
|
func (c *Client) Close() error {
|
2024-11-22 14:59:19 +01:00
|
|
|
return c.CloseWithSMTPClient(c.smtpClient)
|
|
|
|
}
|
|
|
|
|
2024-11-22 15:55:45 +01:00
|
|
|
// CloseWithSMTPClient terminates the connection of the provided smtp.Client to the SMTP server,
|
|
|
|
// returning an error if the disconnection fails. If the connection is already closed, this
|
|
|
|
// method is a no-op and disregards any error.
|
|
|
|
//
|
|
|
|
// This function checks if the smtp.Client connection is active. If not, it simply returns
|
|
|
|
// without any action. If the connection is active, it attempts to gracefully close the
|
|
|
|
// connection using the Quit method.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - client: A pointer to the smtp.Client that handles the connection to the server.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if the disconnection fails; otherwise, returns nil.
|
2024-11-22 14:59:19 +01:00
|
|
|
func (c *Client) CloseWithSMTPClient(client *smtp.Client) error {
|
|
|
|
if client == nil || !client.HasConnection() {
|
2024-10-04 23:15:01 +02:00
|
|
|
return nil
|
2022-03-10 16:19:51 +01:00
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
if err := client.Quit(); err != nil {
|
2022-03-10 16:19:51 +01:00
|
|
|
return fmt.Errorf("failed to close SMTP client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:23:16 +02:00
|
|
|
// Reset sends an SMTP RSET command to reset the state of the current SMTP session.
|
2024-10-06 13:39:40 +02:00
|
|
|
//
|
|
|
|
// This method checks the connection to the SMTP server and, if the connection is valid,
|
|
|
|
// it sends an RSET command to reset the session state. If the connection is invalid or
|
|
|
|
// the command fails, an error is returned.
|
|
|
|
//
|
|
|
|
// Returns:
|
2024-11-22 15:55:45 +01:00
|
|
|
// - An error if the connection check fails or if sending the RSET command fails;
|
|
|
|
// otherwise, returns nil.
|
2022-03-11 19:17:43 +01:00
|
|
|
func (c *Client) Reset() error {
|
2024-11-22 14:59:19 +01:00
|
|
|
return c.ResetWithSMTPClient(c.smtpClient)
|
|
|
|
}
|
|
|
|
|
2024-11-22 15:55:45 +01:00
|
|
|
// ResetWithSMTPClient sends an SMTP RSET command to the provided smtp.Client, to reset
|
|
|
|
// the state of the current SMTP session.
|
|
|
|
//
|
|
|
|
// This method checks the connection to the SMTP server and, if the connection is valid,
|
|
|
|
// it sends an RSET command to reset the session state. If the connection is invalid or
|
|
|
|
// the command fails, an error is returned.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - client: A pointer to the smtp.Client that handles the connection to the server.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if the connection check fails or if sending the RSET command fails; otherwise, returns nil.
|
2024-11-22 14:59:19 +01:00
|
|
|
func (c *Client) ResetWithSMTPClient(client *smtp.Client) error {
|
|
|
|
if err := c.checkConn(client); err != nil {
|
2022-03-11 19:17:43 +01:00
|
|
|
return err
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
if err := client.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
|
|
|
|
}
|
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// DialAndSend establishes a connection to the server and sends out the provided Msg.
|
|
|
|
// It calls DialAndSendWithContext with an empty Context.Background.
|
|
|
|
//
|
|
|
|
// This method simplifies the process of connecting to the SMTP server and sending messages
|
|
|
|
// by using a default context. It prepares the messages for sending and ensures the connection
|
|
|
|
// is established before attempting to send them.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - messages: A variadic list of pointers to Msg objects to be sent.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if the connection fails or if sending the messages fails; otherwise, returns nil.
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// DialAndSendWithContext establishes a connection to the SMTP server using DialWithContext
|
|
|
|
// with the provided context.Context, then sends out the given Msg. After successful delivery,
|
|
|
|
// the Client will close the connection to the server.
|
|
|
|
//
|
|
|
|
// This method first attempts to connect to the SMTP server using the provided context.
|
|
|
|
// Upon successful connection, it sends the specified messages and ensures that the connection
|
|
|
|
// is closed after the operation, regardless of success or failure in sending the messages.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - ctx: The context.Context to control the connection timeout and cancellation.
|
|
|
|
// - messages: A variadic list of pointers to Msg objects to be sent.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if the connection fails, if sending the messages fails, or if closing the
|
|
|
|
// connection fails; otherwise, returns nil.
|
2024-02-25 17:48:53 +01:00
|
|
|
func (c *Client) DialAndSendWithContext(ctx context.Context, messages ...*Msg) error {
|
2024-11-22 16:06:28 +01:00
|
|
|
client, err := c.DialToSMTPClientWithContext(ctx)
|
2024-11-22 14:59:19 +01:00
|
|
|
if err != nil {
|
2022-03-10 12:10:27 +01:00
|
|
|
return fmt.Errorf("dial failed: %w", err)
|
|
|
|
}
|
2024-10-04 23:23:16 +02:00
|
|
|
defer func() {
|
2024-11-22 14:59:19 +01:00
|
|
|
_ = c.CloseWithSMTPClient(client)
|
2024-10-04 23:23:16 +02:00
|
|
|
}()
|
|
|
|
|
2024-11-22 15:29:22 +01:00
|
|
|
if err = c.SendWithSMTPClient(client, messages...); err != nil {
|
2022-03-10 12:10:27 +01:00
|
|
|
return fmt.Errorf("send failed: %w", err)
|
|
|
|
}
|
2024-11-22 15:29:22 +01:00
|
|
|
if err = c.CloseWithSMTPClient(client); 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
|
|
|
|
}
|
|
|
|
|
2024-11-22 15:55:45 +01:00
|
|
|
// Send attempts to send one or more Msg using the SMTP client that is assigned to the Client.
|
|
|
|
// If the Client has no active connection to the server, Send will fail with an error. For
|
|
|
|
// each of the provided Msg, it will associate a SendError with the Msg in case of a
|
|
|
|
// transmission or delivery error.
|
|
|
|
//
|
|
|
|
// This method first checks for an active connection to the SMTP server. If the connection is
|
|
|
|
// not valid, it returns a SendError. It then iterates over the provided messages, attempting
|
|
|
|
// to send each one. If an error occurs during sending, the method records the error and
|
|
|
|
// associates it with the corresponding Msg. If multiple errors are encountered, it aggregates
|
|
|
|
// them into a single SendError to be returned.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - client: A pointer to the smtp.Client that holds the connection to the SMTP server
|
|
|
|
// - messages: A variadic list of pointers to Msg objects to be sent.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error that represents the sending result, which may include multiple SendErrors if
|
|
|
|
// any occurred; otherwise, returns nil.
|
2024-11-22 15:38:04 +01:00
|
|
|
func (c *Client) Send(messages ...*Msg) (returnErr error) {
|
|
|
|
c.sendMutex.Lock()
|
|
|
|
defer c.sendMutex.Unlock()
|
|
|
|
return c.SendWithSMTPClient(c.smtpClient, messages...)
|
|
|
|
}
|
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// auth attempts to authenticate the client using SMTP AUTH mechanisms. It checks the connection,
|
|
|
|
// determines the supported authentication methods, and applies the appropriate authentication
|
|
|
|
// type. An error is returned if authentication fails.
|
|
|
|
//
|
2024-10-11 11:21:56 +02:00
|
|
|
// By default NewClient sets the SMTP authentication type to SMTPAuthNoAuth, meaning, that no
|
|
|
|
// SMTP authentication will be performed. If the user makes use of SetSMTPAuth or initialzes the
|
|
|
|
// client with WithSMTPAuth, the SMTP authentication type will be set in the Client, forcing
|
|
|
|
// this method to determine if the server supports the selected authentication method and
|
|
|
|
// assigning the corresponding smtp.Auth function to it.
|
|
|
|
//
|
|
|
|
// If the user set a custom SMTP authentication function using SetSMTPAuthCustom or
|
|
|
|
// WithSMTPAuthCustom, we will not perform any detection and assignment logic and will trust
|
|
|
|
// the user with their provided smtp.Auth function.
|
|
|
|
//
|
2024-10-06 13:39:40 +02:00
|
|
|
// Finally, it attempts to authenticate the client using the selected method.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if the connection check fails, if no supported authentication method is found,
|
|
|
|
// or if the authentication process fails.
|
2024-11-22 14:59:19 +01:00
|
|
|
func (c *Client) auth(client *smtp.Client, isEnc bool) error {
|
|
|
|
var smtpAuth smtp.Auth
|
2024-10-11 11:21:56 +02:00
|
|
|
if c.smtpAuth == nil && c.smtpAuthType != SMTPAuthNoAuth {
|
2024-11-22 14:59:19 +01:00
|
|
|
hasSMTPAuth, smtpAuthType := client.Extension("AUTH")
|
2024-02-25 17:48:53 +01:00
|
|
|
if !hasSMTPAuth {
|
2022-03-10 10:53:38 +01:00
|
|
|
return fmt.Errorf("server does not support SMTP AUTH")
|
|
|
|
}
|
|
|
|
|
2024-11-16 21:46:46 +01:00
|
|
|
authType := c.smtpAuthType
|
2024-11-16 14:29:34 +01:00
|
|
|
if c.smtpAuthType == SMTPAuthAutoDiscover {
|
2024-11-22 14:59:19 +01:00
|
|
|
discoveredType, err := c.authTypeAutoDiscover(smtpAuthType, isEnc)
|
2024-11-16 14:29:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
authType = discoveredType
|
|
|
|
}
|
|
|
|
|
|
|
|
switch authType {
|
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-11-22 14:59:19 +01:00
|
|
|
smtpAuth = smtp.PlainAuth("", c.user, c.pass, c.host, false)
|
2024-10-22 15:30:15 +02:00
|
|
|
case SMTPAuthPlainNoEnc:
|
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthPlain)) {
|
|
|
|
return ErrPlainAuthNotSupported
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
smtpAuth = smtp.PlainAuth("", c.user, c.pass, c.host, true)
|
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-11-22 14:59:19 +01:00
|
|
|
smtpAuth = smtp.LoginAuth(c.user, c.pass, c.host, false)
|
2024-10-22 15:38:51 +02:00
|
|
|
case SMTPAuthLoginNoEnc:
|
|
|
|
if !strings.Contains(smtpAuthType, string(SMTPAuthLogin)) {
|
|
|
|
return ErrLoginAuthNotSupported
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
smtpAuth = smtp.LoginAuth(c.user, c.pass, c.host, true)
|
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-11-22 14:59:19 +01:00
|
|
|
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-11-22 14:59:19 +01:00
|
|
|
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
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
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
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
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
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
tlsConnState, err := client.GetTLSConnectionState()
|
2024-10-01 11:04:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
smtpAuth = smtp.ScramSHA1PlusAuth(c.user, c.pass, tlsConnState)
|
2024-10-01 11:04:16 +02:00
|
|
|
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
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
tlsConnState, err := client.GetTLSConnectionState()
|
2024-10-01 11:04:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
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-11-22 14:59:19 +01:00
|
|
|
if smtpAuth != nil {
|
|
|
|
if err := client.Auth(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
|
|
|
|
2024-11-22 14:59:19 +01:00
|
|
|
func (c *Client) authTypeAutoDiscover(supported string, isEnc bool) (SMTPAuthType, error) {
|
2024-11-16 21:38:29 +01:00
|
|
|
if supported == "" {
|
|
|
|
return "", ErrNoSupportedAuthDiscovered
|
|
|
|
}
|
2024-11-16 21:41:32 +01:00
|
|
|
preferList := []SMTPAuthType{
|
|
|
|
SMTPAuthSCRAMSHA256PLUS, SMTPAuthSCRAMSHA256, SMTPAuthSCRAMSHA1PLUS, SMTPAuthSCRAMSHA1,
|
|
|
|
SMTPAuthXOAUTH2, SMTPAuthCramMD5, SMTPAuthPlain, SMTPAuthLogin,
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
if !isEnc {
|
2024-11-16 14:29:34 +01:00
|
|
|
preferList = []SMTPAuthType{SMTPAuthSCRAMSHA256, SMTPAuthSCRAMSHA1, SMTPAuthXOAUTH2, SMTPAuthCramMD5}
|
|
|
|
}
|
|
|
|
mechs := strings.Split(supported, " ")
|
|
|
|
|
|
|
|
for _, item := range preferList {
|
|
|
|
if sliceContains(mechs, string(item)) {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ErrNoSupportedAuthDiscovered
|
|
|
|
}
|
|
|
|
|
|
|
|
func sliceContains(slice []string, item string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if s == item {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// sendSingleMsg sends out a single message and returns an error if the transmission or
|
|
|
|
// delivery fails. It is invoked by the public Send methods.
|
|
|
|
//
|
|
|
|
// This method handles the process of sending a single email message through the SMTP
|
|
|
|
// client. It performs several checks and operations, including verifying the encoding,
|
|
|
|
// retrieving the sender and recipient addresses, and managing delivery status notifications
|
|
|
|
// (DSN). It attempts to send the message and handles any errors that occur during the
|
|
|
|
// transmission process, ensuring that any necessary cleanup is performed (such as resetting
|
|
|
|
// the SMTP client if an error occurs).
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - message: A pointer to the Msg object representing the email message to be sent.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if any part of the sending process fails; otherwise, returns nil.
|
2024-11-22 14:59:19 +01:00
|
|
|
func (c *Client) sendSingleMsg(client *smtp.Client, message *Msg) error {
|
|
|
|
c.mutex.RLock()
|
|
|
|
defer c.mutex.RUnlock()
|
|
|
|
escSupport, _ := client.Extension("ENHANCEDSTATUSCODES")
|
2024-09-27 14:03:50 +02:00
|
|
|
|
2024-09-19 10:56:01 +02:00
|
|
|
if message.encoding == NoEncoding {
|
2024-11-22 14:59:19 +01:00
|
|
|
if ok, _ := client.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),
|
2024-11-13 22:47:53 +01:00
|
|
|
affectedMsg: message, errcode: errorCode(err),
|
|
|
|
enhancedStatusCode: enhancedStatusCode(err, escSupport),
|
2024-09-20 10:31:19 +02:00
|
|
|
}
|
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),
|
2024-11-13 22:47:53 +01:00
|
|
|
affectedMsg: message, errcode: errorCode(err),
|
|
|
|
enhancedStatusCode: enhancedStatusCode(err, escSupport),
|
2024-09-20 10:31:19 +02:00
|
|
|
}
|
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 != "" {
|
2024-11-22 14:59:19 +01:00
|
|
|
client.SetDSNMailReturnOption(string(c.dsnReturnType))
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
if err = client.Mail(from); err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
retError := &SendError{
|
|
|
|
Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err),
|
2024-11-13 22:47:53 +01:00
|
|
|
affectedMsg: message, errcode: errorCode(err),
|
|
|
|
enhancedStatusCode: enhancedStatusCode(err, escSupport),
|
2024-09-20 10:31:19 +02:00
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
if resetSendErr := client.Reset(); resetSendErr != nil {
|
2024-09-19 10:56:01 +02:00
|
|
|
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)
|
2024-10-04 20:39:14 +02:00
|
|
|
rcptNotifyOpt := strings.Join(c.dsnRcptNotifyType, ",")
|
2024-11-22 14:59:19 +01:00
|
|
|
client.SetDSNRcptNotifyOption(rcptNotifyOpt)
|
2024-09-19 10:56:01 +02:00
|
|
|
for _, rcpt := range rcpts {
|
2024-11-22 14:59:19 +01:00
|
|
|
if err = client.Rcpt(rcpt); err != nil {
|
2024-09-19 10:56:01 +02:00
|
|
|
rcptSendErr.Reason = ErrSMTPRcptTo
|
|
|
|
rcptSendErr.errlist = append(rcptSendErr.errlist, err)
|
|
|
|
rcptSendErr.rcpt = append(rcptSendErr.rcpt, rcpt)
|
|
|
|
rcptSendErr.isTemp = isTempError(err)
|
2024-11-13 22:47:53 +01:00
|
|
|
rcptSendErr.errcode = errorCode(err)
|
|
|
|
rcptSendErr.enhancedStatusCode = enhancedStatusCode(err, escSupport)
|
2024-09-19 10:56:01 +02:00
|
|
|
hasError = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasError {
|
2024-11-22 14:59:19 +01:00
|
|
|
if resetSendErr := client.Reset(); resetSendErr != nil {
|
2024-09-19 10:56:01 +02:00
|
|
|
rcptSendErr.errlist = append(rcptSendErr.errlist, resetSendErr)
|
|
|
|
}
|
|
|
|
return rcptSendErr
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
writer, err := client.Data()
|
2024-09-19 10:56:01 +02:00
|
|
|
if err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err),
|
2024-11-13 22:47:53 +01:00
|
|
|
affectedMsg: message, errcode: errorCode(err),
|
|
|
|
enhancedStatusCode: enhancedStatusCode(err, escSupport),
|
2024-09-20 10:31:19 +02:00
|
|
|
}
|
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),
|
2024-11-13 22:47:53 +01:00
|
|
|
affectedMsg: message, errcode: errorCode(err),
|
|
|
|
enhancedStatusCode: enhancedStatusCode(err, escSupport),
|
2024-09-20 10:31:19 +02:00
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
if err = writer.Close(); err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err),
|
2024-11-13 22:47:53 +01:00
|
|
|
affectedMsg: message, errcode: errorCode(err),
|
|
|
|
enhancedStatusCode: enhancedStatusCode(err, escSupport),
|
2024-09-20 10:31:19 +02:00
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
2024-10-26 21:06:21 +02:00
|
|
|
message.isDelivered = true
|
2024-09-19 10:56:01 +02:00
|
|
|
|
2024-11-22 14:59:19 +01:00
|
|
|
if err = c.ResetWithSMTPClient(client); err != nil {
|
2024-09-20 10:31:19 +02:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrSMTPReset, errlist: []error{err}, isTemp: isTempError(err),
|
2024-11-13 22:47:53 +01:00
|
|
|
affectedMsg: message, errcode: errorCode(err),
|
|
|
|
enhancedStatusCode: enhancedStatusCode(err, escSupport),
|
2024-09-20 10:31:19 +02:00
|
|
|
}
|
2024-09-19 10:56:01 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-04 22:38:18 +02:00
|
|
|
|
2024-10-06 13:39:40 +02:00
|
|
|
// checkConn ensures that a required server connection is available and extends the connection
|
|
|
|
// deadline.
|
|
|
|
//
|
|
|
|
// This method verifies whether there is an active connection to the SMTP server. If there is no
|
|
|
|
// connection, it returns an error. If the "noNoop" flag is not set, it sends a NOOP command to
|
|
|
|
// the server to confirm the connection is still valid. Finally, it updates the connection
|
|
|
|
// deadline based on the specified timeout value. If any operation fails, the appropriate error
|
|
|
|
// is returned.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if there is no active connection, if the NOOP command fails, or if extending
|
|
|
|
// the deadline fails; otherwise, returns nil.
|
2024-11-22 14:59:19 +01:00
|
|
|
func (c *Client) checkConn(client *smtp.Client) error {
|
|
|
|
if client == nil {
|
2024-10-24 10:44:40 +02:00
|
|
|
return ErrNoActiveConnection
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
if !client.HasConnection() {
|
2024-10-04 22:38:18 +02:00
|
|
|
return ErrNoActiveConnection
|
|
|
|
}
|
|
|
|
|
2024-11-22 14:59:19 +01:00
|
|
|
c.mutex.RLock()
|
|
|
|
noNoop := c.noNoop
|
|
|
|
c.mutex.RUnlock()
|
|
|
|
if !noNoop {
|
|
|
|
if err := client.Noop(); err != nil {
|
2024-10-04 22:38:18 +02:00
|
|
|
return ErrNoActiveConnection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-22 14:59:19 +01:00
|
|
|
if err := client.UpdateDeadline(c.connTimeout); err != nil {
|
2024-10-04 22:38:18 +02:00
|
|
|
return ErrDeadlineExtendFailed
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:25:43 +02:00
|
|
|
// serverFallbackAddr returns the currently set combination of hostname and fallback port.
|
2024-10-06 13:39:40 +02:00
|
|
|
//
|
|
|
|
// This method constructs and returns the server address using the host and fallback port
|
|
|
|
// currently configured for the Client. It is useful for establishing a connection when
|
|
|
|
// the primary port is unavailable.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A string representing the server address in the format "host:fallbackPort".
|
2024-10-04 22:38:18 +02:00
|
|
|
func (c *Client) serverFallbackAddr() string {
|
|
|
|
return fmt.Sprintf("%s:%d", c.host, c.fallbackPort)
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:25:43 +02:00
|
|
|
// setDefaultHelo sets the HELO/EHLO hostname to the local machine's hostname.
|
2024-10-06 13:39:40 +02:00
|
|
|
//
|
|
|
|
// This method retrieves the local hostname using the operating system's hostname function
|
|
|
|
// and sets it as the HELO/EHLO string for the Client. If retrieving the hostname fails,
|
|
|
|
// an error is returned.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if there is a failure in reading the local hostname; otherwise, returns nil.
|
2024-10-04 22:38:18 +02:00
|
|
|
func (c *Client) setDefaultHelo() error {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read local hostname: %w", err)
|
|
|
|
}
|
|
|
|
c.helo = hostname
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:25:43 +02:00
|
|
|
// tls establishes a TLS connection based on the client's TLS policy and configuration.
|
|
|
|
// Returns an error if no active connection exists or if a TLS error occurs.
|
2024-10-06 13:39:40 +02:00
|
|
|
//
|
|
|
|
// This method first checks if there is an active connection to the SMTP server. If SSL is not
|
|
|
|
// being used and the TLS policy is not set to NoTLS, it checks for STARTTLS support. Depending
|
|
|
|
// on the TLS policy (mandatory or opportunistic), it may initiate a TLS connection using the
|
|
|
|
// StartTLS method. The method also retrieves the TLS connection state to determine if the
|
|
|
|
// connection is encrypted and returns any errors encountered during these processes.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error if there is no active connection, if STARTTLS is required but not supported,
|
|
|
|
// or if there are issues during the TLS handshake; otherwise, returns nil.
|
2024-11-22 14:59:19 +01:00
|
|
|
func (c *Client) tls(client *smtp.Client, isEnc *bool) error {
|
2024-10-04 22:38:18 +02:00
|
|
|
if !c.useSSL && c.tlspolicy != NoTLS {
|
|
|
|
hasStartTLS := false
|
2024-11-22 14:59:19 +01:00
|
|
|
extension, _ := client.Extension("STARTTLS")
|
2024-10-04 22:38:18 +02:00
|
|
|
if c.tlspolicy == TLSMandatory {
|
|
|
|
hasStartTLS = true
|
|
|
|
if !extension {
|
|
|
|
return fmt.Errorf("STARTTLS mode set to: %q, but target host does not support STARTTLS",
|
|
|
|
c.tlspolicy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.tlspolicy == TLSOpportunistic {
|
|
|
|
if extension {
|
|
|
|
hasStartTLS = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasStartTLS {
|
2024-11-22 14:59:19 +01:00
|
|
|
if err := client.StartTLS(c.tlsconfig); err != nil {
|
2024-10-04 22:38:18 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
tlsConnState, err := client.GetTLSConnectionState()
|
2024-10-04 22:38:18 +02:00
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, smtp.ErrNonTLSConnection):
|
2024-11-22 14:59:19 +01:00
|
|
|
*isEnc = false
|
2024-10-04 22:38:18 +02:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("failed to get TLS connection state: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2024-11-22 14:59:19 +01:00
|
|
|
*isEnc = tlsConnState.HandshakeComplete
|
2024-10-04 22:38:18 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|