| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/url" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 |
) |
| 12 |
|
| 13 |
// Service sends notifications to given email addresses via SMTP. |
| 14 |
type Service struct { |
| 15 |
// Standard provides logging and other base service behavior. |
| 16 |
standard.Standard |
| 17 |
// Templater provides named message templates for plain and HTML parts. |
| 18 |
standard.Templater |
| 19 |
|
| 20 |
// Config holds SMTP server, authentication, and message options. |
| 21 |
Config *Config |
| 22 |
// propKeyResolver applies URL and send-parameter overrides to [Config]. |
| 23 |
propKeyResolver format.PropKeyResolver |
| 24 |
// dialContext, if non-nil, is used for the SMTP TCP dial. |
| 25 |
dialContext types.DialContextFunc |
| 26 |
} |
| 27 |
|
| 28 |
const ( |
| 29 |
// DefaultSMTPPort is the standard port for SMTP communication. |
| 30 |
DefaultSMTPPort = 25 |
| 31 |
// defaultTimeout is the default SMTP timeout. |
| 32 |
defaultTimeout = 10 * time.Second |
| 33 |
) |
| 34 |
|
| 35 |
var ( |
| 36 |
_ types.ContextSender = (*Service)(nil) |
| 37 |
_ types.DialContextSetter = (*Service)(nil) |
| 38 |
_ types.ServiceTimeout = (*Service)(nil) |
| 39 |
) |
| 40 |
|
| 41 |
// GetID returns the service identifier. |
| 42 |
// |
| 43 |
// Returns: |
| 44 |
// - The scheme name [Scheme]. |
| 45 |
func (s *Service) GetID() string { |
| 46 |
return Scheme |
| 47 |
} |
| 48 |
|
| 49 |
// Initialize loads [Config] from serviceURL and sets logger for this [Service]. |
| 50 |
// |
| 51 |
// It applies SMTP defaults, parses the configuration URL, and infers Auth as |
| 52 |
// [AuthPlain] when a username is present or [AuthNone] otherwise if |
| 53 |
// Auth is [AuthUnknown]. |
| 54 |
// |
| 55 |
// Parameters: |
| 56 |
// - serviceURL: The SMTP configuration URL. |
| 57 |
// - logger: Logger used for service output ([types.StdLogger]). |
| 58 |
// |
| 59 |
// Returns: |
| 60 |
// - An error if the configuration URL is invalid. |
| 61 |
func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 62 |
s.SetLogger(logger) |
| 63 |
s.Config = &Config{ |
| 64 |
Port: DefaultSMTPPort, |
| 65 |
ToAddresses: nil, |
| 66 |
Subject: "", |
| 67 |
Auth: AuthTypes.Unknown, |
| 68 |
UseStartTLS: true, |
| 69 |
UseHTML: false, |
| 70 |
Encryption: EncMethods.Auto, |
| 71 |
ClientHost: "localhost", |
| 72 |
Timeout: defaultTimeout, |
| 73 |
} |
| 74 |
|
| 75 |
pkr := format.NewPropKeyResolver(s.Config) |
| 76 |
|
| 77 |
if err := s.Config.setURL(&pkr, serviceURL); err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
|
| 81 |
if s.Config.Auth == AuthTypes.Unknown { |
| 82 |
if s.Config.Username != "" { |
| 83 |
s.Config.Auth = AuthTypes.Plain |
| 84 |
} else { |
| 85 |
s.Config.Auth = AuthTypes.None |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
s.propKeyResolver = pkr |
| 90 |
|
| 91 |
return nil |
| 92 |
} |
| 93 |
|
| 94 |
// Send sends a notification message to email recipients. |
| 95 |
// |
| 96 |
// It delegates to [Service.SendContext] with [context.Background]. |
| 97 |
// |
| 98 |
// Parameters: |
| 99 |
// - message: The notification body sent as the email message. |
| 100 |
// - params: Optional runtime overrides for configuration fields ([types.Params]). |
| 101 |
// |
| 102 |
// Returns: |
| 103 |
// - An error if configuration updates, connection, or delivery fail. |
| 104 |
func (s *Service) Send(message string, params *types.Params) error { |
| 105 |
return s.SendContext(context.Background(), message, params) |
| 106 |
} |
| 107 |
|
| 108 |
// SendContext sends a notification message to email recipients. |
| 109 |
// |
| 110 |
// It clones the service configuration, applies optional runtime params, opens |
| 111 |
// an SMTP client, and delivers the message. A timeout from [Config.Timeout] |
| 112 |
// bounds the connection and session, derived from ctx so caller cancellation |
| 113 |
// and deadlines propagate. Non-positive timeouts use [defaultTimeout]. |
| 114 |
// |
| 115 |
// Parameters: |
| 116 |
// - ctx: Parent context for cancellation and deadlines. |
| 117 |
// - message: The notification body sent as the email message. |
| 118 |
// - params: Optional runtime overrides for configuration fields ([types.Params]). |
| 119 |
// |
| 120 |
// Returns: |
| 121 |
// - An error if configuration updates, connection, or delivery fail. |
| 122 |
func (s *Service) SendContext(ctx context.Context, message string, params *types.Params) error { |
| 123 |
config, err := s.sendConfig(params) |
| 124 |
if err != nil { |
| 125 |
return fail(FailApplySendParams, err) |
| 126 |
} |
| 127 |
|
| 128 |
if config.SkipTLSVerify { |
| 129 |
s.Log("Warning: TLS verification is disabled, making connections insecure") |
| 130 |
} |
| 131 |
|
| 132 |
ctx, cancel := context.WithTimeout( |
| 133 |
ctx, |
| 134 |
effectiveTimeout(config.Timeout), |
| 135 |
) |
| 136 |
defer cancel() |
| 137 |
|
| 138 |
client, err := dialClient(ctx, &config, s.dialContext) |
| 139 |
if err != nil { |
| 140 |
return fail(FailGetSMTPClient, err) |
| 141 |
} |
| 142 |
|
| 143 |
return (&session{ |
| 144 |
client: client, |
| 145 |
config: &config, |
| 146 |
svc: s, |
| 147 |
closed: false, |
| 148 |
}).run(message) |
| 149 |
} |
| 150 |
|
| 151 |
// ServiceTimeout reports the SMTP session timeout. |
| 152 |
// |
| 153 |
// Params are applied to a clone of the stored configuration. A param that |
| 154 |
// does not apply leaves the configured timeout in place. |
| 155 |
// |
| 156 |
// Parameters: |
| 157 |
// - params: Optional runtime overrides for configuration fields ([types.Params]). |
| 158 |
// |
| 159 |
// Returns: |
| 160 |
// - The positive session timeout. Non-positive configured values use [defaultTimeout]. |
| 161 |
func (s *Service) ServiceTimeout(params *types.Params) time.Duration { |
| 162 |
config, err := s.sendConfig(params) |
| 163 |
if err != nil { |
| 164 |
return effectiveTimeout(s.Config.Timeout) |
| 165 |
} |
| 166 |
|
| 167 |
return effectiveTimeout(config.Timeout) |
| 168 |
} |
| 169 |
|
| 170 |
// SetDialContext sets a custom dial function for SMTP TCP connections. |
| 171 |
// |
| 172 |
// TLS wrapping for implicit TLS still happens after the TCP dial. |
| 173 |
// A nil dial restores the default [net.Dialer]. |
| 174 |
// |
| 175 |
// Parameters: |
| 176 |
// - dial: The dial function. Must be safe for concurrent use when non-nil. |
| 177 |
func (s *Service) SetDialContext(dial types.DialContextFunc) { |
| 178 |
s.dialContext = dial |
| 179 |
} |
| 180 |
|
| 181 |
// sendConfig returns the configuration for one send. |
| 182 |
// |
| 183 |
// The stored configuration is cloned before params are applied. |
| 184 |
// |
| 185 |
// Parameters: |
| 186 |
// - params: Optional runtime overrides for configuration fields ([types.Params]). |
| 187 |
// |
| 188 |
// Returns: |
| 189 |
// - The cloned configuration. |
| 190 |
// - An error if a param does not apply. |
| 191 |
func (s *Service) sendConfig(params *types.Params) (Config, error) { |
| 192 |
config := s.Config.Clone() |
| 193 |
if err := s.propKeyResolver.UpdateConfigFromParams(&config, params); err != nil { |
| 194 |
return Config{}, err |
| 195 |
} |
| 196 |
|
| 197 |
return config, nil |
| 198 |
} |
| 199 |
|
| 200 |
// effectiveTimeout returns a positive SMTP timeout. |
| 201 |
// |
| 202 |
// Non-positive values fall back to [defaultTimeout] so [context.WithTimeout] |
| 203 |
// always bounds the connection and session. |
| 204 |
// |
| 205 |
// Parameters: |
| 206 |
// - timeout: The configured session timeout. |
| 207 |
// |
| 208 |
// Returns: |
| 209 |
// - timeout when it is positive; otherwise [defaultTimeout]. |
| 210 |
func effectiveTimeout(timeout time.Duration) time.Duration { |
| 211 |
if timeout <= 0 { |
| 212 |
return defaultTimeout |
| 213 |
} |
| 214 |
|
| 215 |
return timeout |
| 216 |
} |