package smtp import ( "context" "net/url" "time" "github.com/nicholas-fedor/shoutrrr/pkg/format" "github.com/nicholas-fedor/shoutrrr/pkg/services/standard" "github.com/nicholas-fedor/shoutrrr/pkg/types" ) // Service sends notifications to given email addresses via SMTP. type Service struct { // Standard provides logging and other base service behavior. standard.Standard // Templater provides named message templates for plain and HTML parts. standard.Templater // Config holds SMTP server, authentication, and message options. Config *Config // propKeyResolver applies URL and send-parameter overrides to [Config]. propKeyResolver format.PropKeyResolver // dialContext, if non-nil, is used for the SMTP TCP dial. dialContext types.DialContextFunc } const ( // DefaultSMTPPort is the standard port for SMTP communication. DefaultSMTPPort = 25 // defaultTimeout is the default SMTP timeout. defaultTimeout = 10 * time.Second ) var ( _ types.ContextSender = (*Service)(nil) _ types.DialContextSetter = (*Service)(nil) ) // GetID returns the service identifier. // // Returns: // - The scheme name [Scheme]. func (s *Service) GetID() string { return Scheme } // Initialize loads [Config] from serviceURL and sets logger for this [Service]. // // It applies SMTP defaults, parses the configuration URL, and infers Auth as // [AuthPlain] when a username is present or [AuthNone] otherwise if // Auth is [AuthUnknown]. // // Parameters: // - serviceURL: The SMTP configuration URL. // - logger: Logger used for service output ([types.StdLogger]). // // Returns: // - An error if the configuration URL is invalid. func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { s.SetLogger(logger) s.Config = &Config{ Port: DefaultSMTPPort, ToAddresses: nil, Subject: "", Auth: AuthTypes.Unknown, UseStartTLS: true, UseHTML: false, Encryption: EncMethods.Auto, ClientHost: "localhost", Timeout: defaultTimeout, } pkr := format.NewPropKeyResolver(s.Config) if err := s.Config.setURL(&pkr, serviceURL); err != nil { return err } if s.Config.Auth == AuthTypes.Unknown { if s.Config.Username != "" { s.Config.Auth = AuthTypes.Plain } else { s.Config.Auth = AuthTypes.None } } s.propKeyResolver = pkr return nil } // Send sends a notification message to email recipients. // // It delegates to [Service.SendContext] with [context.Background]. // // Parameters: // - message: The notification body sent as the email message. // - params: Optional runtime overrides for configuration fields ([types.Params]). // // Returns: // - An error if configuration updates, connection, or delivery fail. func (s *Service) Send(message string, params *types.Params) error { return s.SendContext(context.Background(), message, params) } // SendContext sends a notification message to email recipients. // // It clones the service configuration, applies optional runtime params, opens // an SMTP client, and delivers the message. A timeout from [Config.Timeout] // bounds the connection and session, derived from ctx so caller cancellation // and deadlines propagate. Non-positive timeouts use [defaultTimeout]. // // Parameters: // - ctx: Parent context for cancellation and deadlines. // - message: The notification body sent as the email message. // - params: Optional runtime overrides for configuration fields ([types.Params]). // // Returns: // - An error if configuration updates, connection, or delivery fail. func (s *Service) SendContext(ctx context.Context, message string, params *types.Params) error { config := s.Config.Clone() if err := s.propKeyResolver.UpdateConfigFromParams(&config, params); err != nil { return fail(FailApplySendParams, err) } if config.SkipTLSVerify { s.Log("Warning: TLS verification is disabled, making connections insecure") } ctx, cancel := context.WithTimeout( ctx, effectiveTimeout(config.Timeout), ) defer cancel() client, err := dialClient(ctx, &config, s.dialContext) if err != nil { return fail(FailGetSMTPClient, err) } return (&session{ client: client, config: &config, svc: s, closed: false, }).run(message) } // SetDialContext sets a custom dial function for SMTP TCP connections. // // TLS wrapping for implicit TLS still happens after the TCP dial. // A nil dial restores the default [net.Dialer]. // // Parameters: // - dial: The dial function. Must be safe for concurrent use when non-nil. func (s *Service) SetDialContext(dial types.DialContextFunc) { s.dialContext = dial } // effectiveTimeout returns a positive SMTP timeout. // // Non-positive values fall back to [defaultTimeout] so [context.WithTimeout] // always bounds the connection and session. // // Parameters: // - timeout: The configured session timeout. // // Returns: // - timeout when it is positive; otherwise [defaultTimeout]. func effectiveTimeout(timeout time.Duration) time.Duration { if timeout <= 0 { return defaultTimeout } return timeout }