50 lines
1 KiB
Go
50 lines
1 KiB
Go
package smtp2shoutrrr
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"git.nakama.town/fmartingr/gotoolkit/model"
|
|
"github.com/emersion/go-smtp"
|
|
)
|
|
|
|
var _ model.Server = (*smtpServer)(nil)
|
|
|
|
type smtpServer struct {
|
|
backend *smtp.Server
|
|
config Config
|
|
}
|
|
|
|
func (s *smtpServer) IsEnabled() bool {
|
|
return true
|
|
}
|
|
|
|
func (s *smtpServer) Start(_ context.Context) error {
|
|
slog.Info("Started SMTP server", slog.String("addr", s.backend.Addr))
|
|
return s.backend.ListenAndServe()
|
|
}
|
|
|
|
func (s *smtpServer) Stop(ctx context.Context) error {
|
|
slog.Info("Stopping SMTP server")
|
|
return s.backend.Shutdown(ctx)
|
|
}
|
|
|
|
func NewSMTPServer(config *Config) model.Server {
|
|
be := &Backend{
|
|
config: config,
|
|
}
|
|
|
|
smtpBackend := smtp.NewServer(be)
|
|
smtpBackend.Addr = fmt.Sprintf(":%d", config.Port)
|
|
smtpBackend.WriteTimeout = 10 * time.Second
|
|
smtpBackend.ReadTimeout = 10 * time.Second
|
|
smtpBackend.MaxMessageBytes = 1024 * 1024
|
|
smtpBackend.MaxRecipients = 50
|
|
smtpBackend.AllowInsecureAuth = true
|
|
|
|
return &smtpServer{
|
|
backend: smtpBackend,
|
|
}
|
|
}
|