package main import ( "errors" "fmt" "log/slog" "net/smtp" "os" "git.nakama.town/fmartingr/smtp2shoutrrr" ) const ( configPath = "config.toml" // PlainAuth refuses to send credentials over an unencrypted connection // unless the host is a loopback address, which is what the development // server listens on. hostname = "localhost" ) func main() { if err := run(); err != nil { slog.Error("sendmail failed", slog.String("err", err.Error())) os.Exit(1) } } func run() error { config, err := smtp2shoutrrr.LoadConfig(configPath) if err != nil { return fmt.Errorf("loading configuration: %w", err) } slog.Info("Using first recipient configuration to send a test email") if len(config.Recipients) == 0 { return errors.New("no recipients found in configuration") } if len(config.Recipients[0].Addresses) == 0 { return errors.New("no email addresses found in first recipient configuration") } auth := smtp.PlainAuth("", config.Username, config.Password, hostname) recipients := []string{config.Recipients[0].Addresses[0]} msg := []byte("Subject: Test notification\r\n\r\nThis is a test notification") from := "hello@localhost" return smtp.SendMail(fmt.Sprintf("%s:%d", hostname, config.Port), auth, from, recipients, msg) }